简体   繁体   中英

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 
     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 = "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()

Then you have to parse you curl response using $data = json_decode($curlResponse) . You will get an associative array which can be iterated ( foreach , for , while ). Only you know how to write your SQL queries.

Note: If this doesn't work, take a look at the curl_setopt() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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