2013-08-30 148 views
0

我想處理來自facebook圖的帖子消息。Facebook json_decode發表評論POST消息

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==" 
      } 
     } 
     } 
    } 
} 

代碼:

​​

,但它是空的,沒有輸出給出。我知道這是循環的東西。我從來沒有使用過JSON。

回答

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伊萬基督教

消息:測試1

名稱: Zbarcea Ioan-Christian

消息:試驗2

名稱:Zbarcea伊萬基督教

消息:試驗3