我从端点获取数据,该端点返回这样的JSON数据列表
***API DATA
{"addresses": [
{
"address": "address1",
"total_sent": 000000000000000000,
"total_received": 00000000000000,
"final_balance": 000000000000000,
"n_tx": 0000
}],
"txs": [{
"hash": "hhhdhhdhgggdhhdjjjjdkkdkkdjjdjjjdjjj",
"confirmations": 0000,
"change": 000000000000000,
"time_utc": "2018-04-30T02:59:43Z"
},{
"hash": "hhhdhhdhgggdhhdjjjjdkkdkkdjjdjjjdjjj",
"confirmations": 0000,
"change": 000000000000000,
"time_utc": "2018-04-30T02:59:43Z"
}],
"addresses": [
{
"address": "address2",
"total_sent": 000000000000000000,
"total_received": 00000000000000,
"final_balance": 000000000000000,
"n_tx": 0000
}],
"txs": [{
"hash": "hhhdhhdhgggdhhdjjjjdkkdkkdjjdjjjdjjj",
"confirmations": 0000,
"change": 000000000000000,
"time_utc": "2018-04-30T02:59:43Z"
},{
"hash": "hhhdhhdhgggdhhdjjjjdkkdkkdjjdjjjdjjj",
"confirmations": 0000,
"change": 000000000000000,
"time_utc": "2018-04-30T02:59:43Z"
}]
}
因此,此JSON输出在其下打印出地址和不同的事务。我要做的是将这些数据保存在数据库中,并在地址及其事务(txs)之间创建关系。我创建了两个表,一个用于地址和交易。
***ADRESS TB****
CREATE TABLE `address` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`address` varchar(100) DEFAULT NULL,
`total_sent` bigint(50) NOT NULL,
`total_received` bigint(50) NOT NULL,
`final_balance` bigint(50) NOT NULL,
`n_tx` int(10) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
*****Transactions DB*******
CREATE TABLE `transactions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`hash_key` varchar(100) DEFAULT NULL,
`confirmations` bigint(20) NOT NULL,
`change_hash` bigint(50) NOT NULL,
`time_utc` datetime DEFAULT NULL,
`address_id` bigint(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`address_id`) REFERENCES `address` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
这是我尝试过的,我不打算从这里做什么
$decoded =json_decode( $getresponse, true );//decoding json data
if (is_array($decoded) || is_object($decoded)){
foreach ($decoded['addresses'] as $addresseschunk){
foreach ($decoded['addresses'] as $addresseschunk){
$address =$addresseschunk['address'];
$totalsent =$addresseschunk['total_sent'];
$totalreceived =$addresseschunk['total_received'];
$final_balance =$addresseschunk['final_balance'];
$n_tx =$addre
$hash =$txschunk['hash'];
$confirmations =$txschunk['confirmations'];
$change =$txschunk['change'];
$timeutc =$txschunk['time_utc'];
#$n =$txschunk['n'];
$newtime =date('Y-m-d h:i:s', strtotime($timeutc));
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$stmt = $conn->prepare("SELECT * FROM address WHERE address=?");
$stmt->execute([$address]);
$curencyAddress = $stmt->fetch();
if ($curencyAddress) {
echo "user exist already";
} else {
$sql = "INSERT INTO address (address, total_sent, total_received,final_balance,n_tx,currency_id)
VALUES ('$address','$totalsent','$totalreceived','$final_balance','$n_tx','$curencyID')";
$conn->exec($sql);
$new_id = $conn->lastInsertId();
$sql2 = "INSERT INTO hashkey (hash_key, confirmations, change_hash,time_utc,address_id)
VALUES ('$hash','$confirmations','$change','$newtime','$new_id')";
$conn->exec($sql2);
}
}
}
}
答案 0 :(得分:0)
我读得不太远,因为我的“内部” php解释器在此处给出了错误消息:
@lends
由于$hash=$txschunk['hash'];
未定义,因此应该给出“未定义”。
备份,这看起来很粗略,也许还有错字(或复制/粘贴嘘声):
$txschunk
您可能想在脚本中turn on all php errors and warnings,或者至少检查php错误日志。解决该问题应该将您带到另一个地方,这并不是说程序可以运行,而只是说您应该取得进展。