2011-01-06 53 views
0

我讓來自Facebook的API使用PHP的查詢,當我的print_r的resalt,它返回:如何獲得通過Facebook API查詢圖片(PHP)

Array ([data] => Array ([0] => Array ([id] => 100001152853241_182911181726586 [from] => Array ([name] => Lista Milk [id] => 100001152853241) [message] => Sab 08/01 - Shake ♀♂ WHITE PARTY ♀♂ Lista MILK 327.6706344 - Vesti la tua notte di bianco..per un party al ritmo della musica di Moira Dj from Borgo Milano!! Per riduzioni e liste: MILK 327.6706344 - Indossa un capo bianco..e riceverai un omaggio!! - [picture] => http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs456.snc4/50513_142783445779515_241568_s.jpg [link] => http://www.facebook.com/event.php?eid=142783445779515 [name] => Shake ♀♂ WHITE PARTY ♀♂ Lista MILK 327.6706344 [properties] 

我用<img src="http://graph.facebook.com/<? echo $user[data][0][id]; ?>/picture?type=small" />,但沒有得到。如何獲得照片?謝謝。

+0

您可能需要報價指數(它總是鼓勵),如下所示:`$ user ['data'] [0] ['id']` – anq 2011-01-06 09:06:41

+0

@anq,我改成了' 2011-01-06 09:13:44

回答

1

首先,您正在查看錯誤的ID。用戶的id是$user['data'][0]['from']['id']。其次,據我所看到的,Facebook的給你已經鏈接該用戶的圖片:picture PARAM該數組;)

編輯:

該教程的第四步是錯誤的! 讓我們一步一步瞭解如何使用API​​給出的響應。當你在Facebook的搜索API的調用(如:https://graph.facebook.com/search?q=word)返回的JSON看起來是這樣的:

{ 
    "data": [ 
     { 
     "id": "731528743_161616260551729", 
     "from": { 
      "name": "Sammi Heil", 
      "id": "731528743" 
     }, 
     "message": "\"i always thought that when people said like, its a quarter to five it meant 25 minutes to five...thats how much a quarter is, so how does 15 minutes make sense?\" idiot. 15 minutes is a quarter of an hour, 25 cents is a quarter of a dollar. ugh.", 
     "type": "status", 
     "application": { 
      "name": "Mobile", 
      "id": "2915120374" 
     }, 
     "created_time": "2011-01-06T10:54:43+0000", 
     "updated_time": "2011-01-06T10:54:43+0000" 
     }, 
     { 
     "id": "100001056843950_154372714611375", 
     "from": { 
      "name": "Manoj Bharti", 
      "id": "100001056843950" 
     }, 
     "message": "Never argue with an idiot. They drag\nyou down to their level then beat you\nwith experience", 
     "type": "status", 
     "application": { 
      "name": "Mobile", 
      "id": "2915120374" 
     }, 
     "created_time": "2011-01-06T10:54:37+0000", 
     "updated_time": "2011-01-06T10:54:37+0000" 
     }, 
     { 
     "id": "727806581_130875896976657", 
     "from": { 
      "name": "Emran Bala", 
      "id": "727806581" 
     }, 
     "picture": "http://vthumb.ak.fbcdn.net/hvthumb-ak-snc4/hs1302.snc4/50953_154339931282736_154339771282752_21104_1284_t.jpg", 
     "link": "http://www.facebook.com/video/video.php?v=154339771282752&oid=124673824262095&comments", 
     "source": "http://video.ak.fbcdn.net/cfs-ak-snc6/78924/709/154339771282752_17935.mp4?__gda__=1294484092_1efde632f7fac98390cf08ef85861f2f", 
     "name": "Video - Crazy Idiot Lays on Train Tracks and Lets Train Run Over ", 
     "properties": [ 
      { 
       "name": "Length", 
       "text": "0:55" 
      } 
     ], 
     "icon": "http://b.static.ak.fbcdn.net/rsrc.php/yD/r/aS8ecmYRys0.gif", 
     "type": "video", 
     "application": { 
      "name": "Video", 
      "id": "2392950137" 
     }, 
     "created_time": "2011-01-06T10:54:34+0000", 
     "updated_time": "2011-01-06T10:54:34+0000" 
     } 
    ], 
    "paging": { 
     "previous": "https://graph.facebook.com/search?q=idiot&limit=25&since=2011-01-06T10%3A54%3A43%2B0000", 
     "next": "https://graph.facebook.com/search?q=idiot&limit=25&until=2011-01-06T10%3A45%3A22%2B0000" 
    } 
} 

有了這個響應,你需要它,以輕鬆地分析它解碼。你這樣做是這樣的:

$json = 'the string above';  
$results = json_decode($json, true); 

現在,我們可以通過這個數組這樣的循環:

// each result have a `from` array containing the `name` and `id` 
// of the person who posted that message 
foreach ($results['data'] as $result) { 
    echo '<img src="https://graph.facebook.com/'.$result['from']['id'].'/picture?type=small" alt="" /><br/>'; 
} 

現在你有一個顯示在屏幕上每個用戶的圖片。就那麼簡單!

如何在此處使用API​​的完整示例:http://pastie.org/1433642。將其複製粘貼到本地機器上並進行測試!您不需要訪問令牌。

EDIT2:

,如果你仍然想使用來自該教程的例子中,正確的用法是:這取決於你的PHP配置和版本

<img src="https://graph.facebook.com/<?php echo $users['data'][0]['from']['id']; ?>/picture?type=small" /> 
0

支持短標籤? 嘗試<?php ?>

+0

我已經嘗試過` 2011-01-06 09:10:38

相關問題