Facebook json_decode POST消息征求意见

时间:2013-08-30 15:25:36

标签: facebook facebook-comments

我正在尝试处理来自facebook图表的帖子消息。

https://graph.facebook.com/comments/?ids=http://www.myawesomedomain.com/post_nr38.html

结果是:

{
   "http://www.myawesomedomain.com/post_nr38.html": {
      "comments": {
         "data": [
            {
               "id": "123381557832360_139652",
               "from": {
                  "name": "Zbarcea Ioan-Christian",
                  "id": "100001004884254"
               },
               "message": "test 3",
               "can_remove": false,
               "created_time": "2013-08-30T14:44:51+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "123381557832360_139651",
               "from": {
                  "name": "Zbarcea Ioan-Christian",
                  "id": "100001004884254"
               },
               "message": "test 2",
               "can_remove": false,
               "created_time": "2013-08-30T14:28:16+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "123381557832360_139650",
               "from": {
                  "name": "Zbarcea Ioan-Christian",
                  "id": "100001004884254"
               },
               "message": "test 1",
               "can_remove": false,
               "created_time": "2013-08-30T14:28:12+0000",
               "like_count": 0,
               "user_likes": false
            }
         ],
         "paging": {
            "cursors": {
               "after": "MQ==",
               "before": "Mw=="
            }
         }
      }
   }
}

代码:

$html = file_get_contents('https://graph.facebook.com/comments/?ids=http://www.myawesomedomain.com/post_nr38.html');
$json_data = json_decode($html);

foreach ($json_data as $data)
{
   echo "Name: ".$data['name']."<br />Message: ".$data['message'];
}

但它是空的,没有给出输出。我知道它与循环有关。我从未使用过JSON。

1 个答案:

答案 0 :(得分:0)

解决方案是解析数据的方式:

$url = 'http://www.yourawesomedomain.com/test_p38.html';
$html = file_get_contents('https://graph.facebook.com/comments/?ids='.$url);
$json_data = json_decode($html);
$object_array = $json_data->{$url}->{'comments'}->{'data'};

foreach ($object_array as $data)
{
   echo "Name: ".$data->from->name."<br />Message: ".$data->message."<hr />";
}

<强>输出:

  

姓名:Zbarcea Ioan-Christian

     

消息:测试1

     

姓名:Zbarcea Ioan-Christian

     

消息:测试2

     

姓名:Zbarcea Ioan-Christian

     

消息:测试3