1

I am using the graph api to get the profile picture of my facebook app user to a specified width and height.

The url: http://graph.facebook.com/'.$userid.'/picture?width=180&height=220

This will return something in json like { "data": { "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/c0.0.553.676/s320x320/998591_136374463234627_573810314_n.jpg", "width": 262, "height": 320, "is_silhouette": false } }

I wish to know how to decode that in php and most appropriately how to get the 'url' in the json string returned. Thanks for helping. Note: I'll store the url in a variable in php, then use the url to imagecreatefromjpeg(GD library) and then use the image and merge it with another image.

user2599068
  • 21
  • 2
  • 6
  • PHP's decode command is `json_decode`. Once you've done that, the result is a standard array. From there, you can pick out the url. Use this as a reference: http://stackoverflow.com/questions/8447084/get-score-from-json-array – DevlshOne Jul 19 '13 at 10:45

1 Answers1

4

Use json_decode function (http://php.net/manual/en/function.json-decode.php)

It accepts the json string as a parameter and returns either array or an object

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$obj = json_decode($json);
$array = json_decode($json, true);

Then, access values like

echo $obj->a;
echo $array['a'];

Both will output

1

In your case you can access the url this way

$obj = json_decode($your_fb_result);
echo $obj->data->url;

OR

$array = json_decode($your_fb_result, true);
echo $array['data']['url'];

Specific to your situation,

$response = file_get_contents('http://graph.facebook.com/'.$userid.'/picture?width=180&height=220&redirect=false');
$array = json_decode($response, true);
echo $array['data']['url'];

See http://php.net/manual/en/function.file-get-contents.php

Vladimir Hraban
  • 3,543
  • 4
  • 26
  • 46
  • Here is the code: $userprofpicurl='http://graph.facebook.com/'.$userid.'/picture?width=180&height=220'; $array = json_decode($userprofpicurl, true); echo $array['data']['url']; It doesn't seem to work! :( – user2599068 Jul 19 '13 at 11:03
  • You firstly have to get the output of that URL. file_get_contents will do the trick. See updated answer – Vladimir Hraban Jul 19 '13 at 11:09
  • I checked your URL and it returns the image, not the JSON. Check out http://stackoverflow.com/questions/2070603/php-recreate-and-display-an-image-from-binary-data for ways to display binary data as image or just pass the URL in the SRC attribute in IMG tag – Vladimir Hraban Jul 19 '13 at 11:23
  • This is the image URL itself. Just try – Vladimir Hraban Jul 19 '13 at 11:31
  • what if i use this url instead: http://graph.facebook.com/100005862221464/picture?width=180&height=220&redirect=false – user2599068 Jul 19 '13 at 11:37
  • Works perfectly for me if &redirect=false is added. I updated the last code sample in my response – Vladimir Hraban Jul 19 '13 at 11:48
  • Ok so now it is working, thanks a lot bro! it was not working because i was getting the userid in a variable $user_id whereas in the url i was appending userid as $userid! Thanks again! :) – user2599068 Jul 19 '13 at 11:53