douzhaocheng4533 2011-01-06 09:03
浏览 33
已采纳

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

I make a query from facebook api use php, when I print_r the resalt, it return:

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]

I use <img src="http://graph.facebook.com/<? echo $user[data][0][id]; ?>/picture?type=small" />, but nothing get. how to get a picture? thanks.

  • 写回答

2条回答 默认 最新

  • dtu36380 2011-01-06 09:31
    关注

    First of all, you are looking at the wrong id. The id of the user is $user['data'][0]['from']['id']. Secondly, as far as I can see, facebook gives you already the url for that user picture: picture param of that array ;)

    edit:

    The 4th step of that tutorial is wrong! Lets take it step by step on how to use the response given by API. When you make a call to facebook search API (eg: https://graph.facebook.com/search?q=word) the returned json looks something like this:

    {
       "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
    you down to their level then beat you
    with 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"
       }
    }
    

    Having this response you need to decode it in order to easily parse it. You do this like this:

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

    Now we can iterate through this array like this:

    // 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/>';
    }
    

    Now you have the picture of each user showing on the screen. Simple as that!

    Full example of how to use the API here: http://pastie.org/1433642. Copy-paste it on your local machine and test it! You don't need an access token.

    edit2:

    and if you still want to use the example from that tutorial, the correct usage is:

    <img src="https://graph.facebook.com/<?php echo $users['data'][0]['from']['id']; ?>/picture?type=small" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计