【发布时间】:2016-04-29 20:20:29
【问题描述】:
我正在尝试获取 JSON 数据并将其输入到我的数据库中。
这是我当前的代码:
while($row= mysqli_fetch_assoc($query)){
$id = $row["id"];
$domain = $row["domain"];
$time = date('Y-m-d H:i:s');
$ch = curl_init();
$url = "https://www.example.com/";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "apikey=123&test=$domain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
print $output;
// CODE SHOULD BE ADDED HERE I THINK
curl_close ($ch);
$sql_to_update = "UPDATE monitoring SET socials='$socials', update_time='$time' WHERE id='$id'";
mysqli_query($conn,$sql_to_update);
}
这是我得到的 JSON 结果示例:
{
"resource":"random data",
"tests":100,
"total":250,
"networks":{
"FaceBook":{
"detected":true,
"result":"ignore"
},
"Twitter Inc":{
"detected":false,
"result":"ignore"
},
"MySpace":{
"detected":true,
"result":"ignore"
},
"Pinterest":{
"detected":true,
"result":"ignore"
},
"Instagram":{
"detected":false,
"result":"ignore"
}
}
}
我需要做的就是将networks 的detected 设置为true,然后使用变量$socials 将它们插入我的数据库中。
例如,对于上面列出的 JSON 示例,我希望在数据库的 socials 列中输入以下内容:FaceBook, MySpace, Pinterest
如果没有将detected 设置为true 的networks,那么我想在该列中输入NONE FOUND。
有谁知道我如何使用 JSON 输出将其输入我的数据库?
【问题讨论】: