【问题标题】:How to parse JSON format DATA from Facebook Graph Api with php?如何使用 php 解析来自 Facebook Graph Api 的 JSON 格式数据?
【发布时间】:2016-04-05 11:47:19
【问题描述】:

我编写了这个 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 : ');
       }
     }
   }

 ?>

当我在浏览器中打开链接时,我将 $url 与适当的 access_token 放在一起,它会向我显示 JSON 格式的数据,问题可能出在哪里?

【问题讨论】:

    标签: php json parsing facebook-graph-api


    【解决方案1】:

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

    查看http://php.net/manual/en/book.curl.php以获取有关curl()的更多信息

    然后你必须使用$data = json_decode($curlResponse) 解析你的 curl 响应。您将获得一个可以迭代的关联数组(foreachforwhile)。 只有您知道如何编写 SQL 查询。

    注意:如果这不起作用,请查看curl_setopt() 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多