skip to Main Content

So here I was trying to decode the post and comments in php but since I don’t know much on JSON, I stuck here and don’t know what else to code to decode the URL from the Graph API I’ve made.

   $group_id ="1xxxxxxxxxxx3"; 
   $access_token ="xxxxxxxxxxxxxx";
   $url = "https://graph.facebook.com/v2.11/" . $group_id . "/feed 
   fields=comments{comments{message,from},message,from},message,from
   &access_token=".$access_token;

   $json= file_get_contents($url);
   $obj = json_decode($json, true);

 ?>

I really need your help and maybe give me some explanation to the problem I’m suffering right now.

The result should be showing like this.
This is the result from the facebook page itself, I need these to come out in my own localhost pages

4

Answers


  1. To display the whole JSON object:

    print_r($obj);
    

    Then you analyse its structure, and loop accordingly, using “foreach”.

    Login or Signup to reply.
  2. If I understood correctly then all you want to do next is determine if it decoded OK and display it? You can try using the json_last_error() like this

       $group_id ="1xxxxxxxxxxx3"; 
       $access_token ="xxxxxxxxxxxxxx";
       $url = "https://graph.facebook.com/v2.11/" . $group_id . "/feed 
       fields=comments{comments{message,from},message,from},message,from
       &access_token=".$access_token;
    
       $json= file_get_contents($url);
       $obj = json_decode($json, true);
    
       if( json_last_error()==JSON_ERROR_NONE ){
        echo '<pre>',print_r( $obj, true ),'</pre>';
       } else {
        echo 'json error: '.json_last_error();
       }
    
    Login or Signup to reply.
  3. I think you have not executed the URL. You need to first execute the URL in order to get the response from the Facebook graph API. Something like

            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, $url); 
            curl_setopt($ch, CURLOPT_HEADER, TRUE); 
            curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
            $head = curl_exec($ch); 
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
            curl_close($ch); 
            var_dump($head);
    
    Login or Signup to reply.
  4. If you can be more elaborate with your question. Have you decoded the Json ? If yes then you can simply use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript object, and use the object in your page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search