Я получаю данные от конечной точки, которая возвращает список данных 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);
}
}
}
}