2013-05-20 100 views
1

我正在試圖爲每個循環的用戶的字段名稱像。問題是我只能得到錯誤或根本沒有結果。有人能告訴我解析這些數據的正確方法嗎?對於每個循環Facebook圖形api

我想從下面的代碼字段:

{ 
    "data": [ 
     { 
     "category": "Tv show", 
     "name": "South Park", 
     "id": "6708787004", 
     "created_time": "2013-05-19T14:53:15+0000" 
     }, 
     { 
     "category": "Tv show", 
     "name": "Futurama", 
     "id": "9588466619", 
     "created_time": "2013-05-19T14:53:03+0000" 
     }, 
     { 
     "category": "Product/service", 
     "name": "ROCK ZOTTEGEM", 
     "id": "79367311142", 
     "created_time": "2013-05-06T15:23:41+0000" 
     }, 
     { 
     "category": "Musician/band", 
     "name": "netsky", 
     "id": "85506412028", 
     "created_time": "2013-05-06T15:21:24+0000" 
     }, 
     { 
     "category": "Media/news/publishing", 
     "name": "Tasty Network", 
     "id": "186383034758939", 
     "created_time": "2013-05-06T15:21:10+0000" 
     }, 
     { 
     "category": "Musician/band", 
     "name": "UKF Drum & Bass", 
     "id": "274197690441", 
     "created_time": "2013-05-06T15:20:25+0000" 
     }, 
     { 
     "category": "Musician/band", 
     "name": "Savant", 
     "id": "204538756277738", 
     "created_time": "2013-05-06T15:20:04+0000" 
     } 
    ], 
    "paging": { 
     "next": "https://graph.facebook.com/100005892006494/likes?access_token=AWfibl9UD01FGmIlwGPABZArZBucGjBZBp2XMt1fj3X5f3uHNvZBhPMqjHyXc6NZB8t0EcGOkTY9KcZBEZAbbuSnwrFqVNhvQl0MOuqqAlDsr8geNezi6IOGAsI64VnlHEY9rLZAK8y0WbgZDZD&limit=5000&offset=5000&__after_id=204538756277738" 
    } 
} 

我的PHP代碼:

$facebookRequest="https://graph.facebook.com/".$user."/likes?access_token=".$access_token.""; 
    $likes = json_decode(file_get_contents($facebookRequest)); 
    //$requests = file_get_contents($facebookRequest); 

      //print_r($likes); 

      foreach($likes as $like => $item) 
     { 
      echo $item->name; 
     } 

回答

1

您需要訪問數組data並從每個搶name場它的孩子。你目前正在做的是試圖從數組中獲取name字段,該字段不存在。

嘗試改變這一點:

foreach($likes as $like => $item) 

這樣:

foreach($likes->data as $like => $item) 
+0

它也不起作用:它提供了以下錯誤:無法使用類型爲stdClass的對象作爲數組 – user2400821

+0

對不起,編輯我的回答並測試它,它現在可以工作。 –

+0

是的,它現在的作品,謝謝! – user2400821