我想获取JSON数据并将其输入到我的数据库中。输入JSON数据到MySQL
这里是我当前的代码:
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"
}
}
}
一切,我需要做的就是有detected
设置到networks
true
并使用变量$socials
将它们插入到我的数据库中。
例如,上面列出的JSON例子,我想下面输入数据库的socials
列:FaceBook, MySpace, Pinterest
如果没有networks
有detected
设置为true
,那么我想改为在该列中输入NONE FOUND
。
任何人都知道我可以如何使用JSON输出来获取到我的数据库?
[用PHP解析JSON文件]可能的重复(http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – miken32