yuli chika
yuli chika

Reputation: 9221

How to get picture through facebook api query?(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.

Upvotes: 0

Views: 5400

Answers (2)

misterjinx
misterjinx

Reputation: 2616

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

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" />

Upvotes: 1

sshet
sshet

Reputation: 1160

supports short tags? try <?php ?>

Upvotes: 0

Related Questions