0

我正在尝试处理来自 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。

4

1 回答 1

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

于 2013-08-30T15:48:11.143 回答