简体   繁体   English

如何使用 php 从 Facebook Graph Api 解析 JSON 格式的数据?

[英]How to parse JSON format DATA from Facebook Graph Api with php?

I wrote this php code to fetch data from an url with json format and It seemed like it worked but I dont get anything in the database我写了这个 php 代码来从一个 json 格式的 url 中获取数据,它似乎有效,但我在数据库中没有得到任何东西

      <?php 
     session_start();
     $con= mysqli_connect("localhost","root","") or die ("could not connect     to mysql"); 
    mysqli_select_db($con,"facebook_data") or die ("no database"); 
    $url = "https://graph.facebook.com/209024949216061/feed?fields=created_time,from,type,message&access_token=XXXXXXXX";
   $ch = curl_init($url);
   curl_setopt($ch,CURLOPT_HEADER, false);
   $curlResponse = curl_exec($ch);

   $data = json_decode($curlResponse,TRUE);
 if (is_array($data) || is_object($data)) {
 foreach($data as $row){

     $id=$row["id"];
     $created_time=$row["created_time"];
     $type=$row["type"];
     $message=$row["message"];
     $user_id=$row["from"]["id"];
     $username=$row["from"]["name"];

     $sql="INSERT INTO group_feed(id, username, created_time, user_id, type, message) VALUES('$id','$username','$created_time','$user_id', '$type', '$message')";
       if(!mysql_query($sql,$con))
       {
    die('Error : ');
       }
     }
   }

 ?>

I put the $url with the appropriate access_token when I open the link in my browser it displays me the JSON format DATA where the problem could be?当我在浏览器中打开链接时,我将 $url 与适当的 access_token 放在一起,它会向我显示 JSON 格式的数据,问题可能出在哪里?

$url = "http://graph.facebook.com/".$group_id."/feed/?access_token=".$token; $ch = curl_init($url); curl_setopt(CURLOPT_HEADER, false); $curlResponse = curl_exec($ch);

Check http://php.net/manual/en/book.curl.php for more information on curl()查看http://php.net/manual/en/book.curl.php了解有关curl()更多信息

Then you have to parse you curl response using $data = json_decode($curlResponse) .然后你必须使用$data = json_decode($curlResponse)解析你的 curl 响应。 You will get an associative array which can be iterated ( foreach , for , while ).您将获得一个可以迭代的关联数组( foreachforwhile )。 Only you know how to write your SQL queries.只有您知道如何编写 SQL 查询。

Note: If this doesn't work, take a look at the curl_setopt() function.注意:如果这不起作用,请查看curl_setopt()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM