Search code examples
phpfacebookfacebook-graph-apifacebook-comments

get comments of post on facebook in php via graph


i want to get facebook comments on public post via graph.facebook i want to get the name, the content and the id of the commenter example of the graph

http://graph.facebook.com/comments/?ids=391265991032089

mycode so far

<?php 
   $data = file_get_contents('http://graph.facebook.com/comments/?ids=391265991032089');
   $json = $data;
    $obj = json_decode($json);
    $comm_no = $obj->391265991032089->{'data'};
    echo("<pre>");
   var_dump($comm_no);
   ?>     

wich gives me

array(25) {
 [0]=>
 object(stdClass)#437 (7) {
  ["id"]=>
  string(31) "337379989797698_337380613130969"
  ["from"]=>
  object(stdClass)#433 (2) {
    ["id"]=>
  string(15) "100002978598053"
  ["name"]=>
    string(28) "Àñä Båñòtá Bãskõtã"
  }
  ["message"]=>
  string(38) "سقفه لاخوكو ابو جبل :D"
  ["can_remove"]=>
  bool(false)
    ["created_time"]=>
     string(24) "2014-12-15T18:58:24+0000"
   ["like_count"]=>
   int(10)
   ["user_likes"]=>
   bool(false)
    }

how to stip it down to just show only name , message and id

isearched alot and didnt find any thing


Solution

  • If there is more than an answer then use the following code:

    <?php 
       $data = file_get_contents('http://graph.facebook.com/comments/?ids=391265991032089');
       $json = $data;
        $obj = json_decode($json);
        $comm_no = $obj->391265991032089->{'data'};
        foreach($comm_no as $answer_id => $v) {
        echo $v->from->id . '<br />';
        echo $v->from->name. '<br />';
        echo $v->message. '<br />';
       }
       ?>