如何通过facebook api查询获取图片?(php)

时间:2011-01-06 09:03:53

标签: php facebook api

我从facebook api使用php进行查询,当我print_r重新执行时,它返回:

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" />,但没有得到。如何拍照?感谢。

2 个答案:

答案 0 :(得分:1)

首先,您正在查看错误的ID。用户的ID为$user['data'][0]['from']['id']。其次,据我所知,facebook已经为你提供了该用户图片的网址:picture该数组的参数;)

修改

该教程的第4步是错误的! 让我们一步一步地了解如何使用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:

如果您仍想使用该教程中的示例,请使用正确的用法:

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

答案 1 :(得分:0)

支持短标签? 试试<?php ?>