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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Here is the code: $userprofpicurl='graph.facebook.com/'.$userid.'/…'; $array = json_decode($userprofpicurl, true); echo $array['data']['url']; It doesn't seem to work! :(
You firstly have to get the output of that URL. file_get_contents will do the trick. See updated answer
I checked your URL and it returns the image, not the JSON. Check out stackoverflow.com/questions/2070603/… for ways to display binary data as image or just pass the URL in the SRC attribute in IMG tag
This is the image URL itself. Just try <img src='graph.facebook.com/SOMEUSERID/picture?width=180&height=220'>
what if i use this url instead: graph.facebook.com/100005862221464/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.