2016-01-23 66 views
2

我想获取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设置到networkstrue并使用变量$socials将它们插入到我的数据库中。

例如,上面列出的JSON例子,我想下面输入数据库的socials列:FaceBook, MySpace, Pinterest

如果没有networksdetected设置为true,那么我想改为在该列中输入NONE FOUND

任何人都知道我可以如何使用JSON输出来获取到我的数据库?

+0

[用PHP解析JSON文件]可能的重复(http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – miken32

回答

1
Check the code to get the desired data that you want to update:- 

<?php 
$link = '{ 
    "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" 
     } 
    } 
}'; // suppose json variable is $link 
$dataArr = json_decode($link); 
echo "<pre/>";print_r($dataArr);// print the variable to see how json data looks when it converted to php array 
$socials = ''; 
foreach($dataArr->networks as $key=>$dataA){ 
    if($dataA->detected == 1){ 
     $socials .= $key.','; 
    } 

} 
$socials = trim($socials,','); 
echo $socials; 
?> 

输出: - https://eval.in/506693

现在把你的更新代码,仅此而已。

+0

已解决,谢谢! – user10848

+0

@ user10848.很高兴为您效劳。 –

1

让它更好地处理json_decode($json, true)

echo '<pre>'; 
print_r(json_decode($json, true)); 
echo '</pre>'; 

数组你会得到一个stdClass的对象这样

stdClass Object 
(
    [resource] => random data 
    [tests] => 100 
    [total] => 250 
    [networks] => stdClass Object 
     (
      [FaceBook] => stdClass Object 
       (
        [detected] => 1 
        [result] => ignore 
       ) 

      [Twitter Inc] => stdClass Object 
       (
        [detected] => 
        [result] => ignore 
       ) 

      [MySpace] => stdClass Object 
       (
        [detected] => 1 
        [result] => ignore 
       ) 

      [Pinterest] => stdClass Object 
       (
        [detected] => 1 
        [result] => ignore 
       ) 

      [Instagram] => stdClass Object 
       (
        [detected] => 
        [result] => ignore 
       ) 

     ) 

) 

然后执行阵列上的循环。我希望你知道其余的。你现在应该做什么。

+1

使它成为一个数组,以便更好地处理'json_decode($ json, true)' –