输出facebook url评论json_decode

时间:2012-06-26 05:35:30

标签: php facebook json

我想在PHP中输出facebook页面的评论。例如:

http://graph.facebook.com/comments/?ids=http://www.example.com

有人可以解释如何正确解码,以便我可以在页面上显示所有评论吗?

我尝试了几个不同的脚本/示例,但它们似乎都没有返回任何结果或错误,所以我想也许图API已经改变了。

    //get the json string from URL
        $data = file_get_contents("http://www.example.com");

        //transform json to associative array
        $data = json_decode($data, true);

        //use only the comments array
        $comments = $data['http://www.example.com']['comments']['data'];

        foreach($comments as $comment) { 
            echo $comment['from']['name'];
            echo $comment['message'];
        }

1 个答案:

答案 0 :(得分:3)

//get the json string from URL
$data = file_get_contents("http://graph.facebook.com/comments/?ids=http://www.example.com");

//transform json to associative array
$data = json_decode($data, true);

//use only the comments array
$comments = $data['http://www.example.com']['comments']['data'];

//you should only see the comments array printed
echo "<pre>"; print_r($comments); echo "</pre>";