1

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?

1 Answer 1

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);

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.