Facebook API - How do I get a Facebook user's profile image through the Facebook API (without requiring the user to "Allow" the application)
Solution 1:
Simply fetch the data through this URL:
http://graph.facebook.com/userid_here/picture
Replace userid_here
with id of the user you want to get the photo of. You can also use HTTPS as well.
You can use the PHP's file_get_contents
function to read that URL and process the retrieved data.
Resource:
http://developers.facebook.com/docs/api
Note: In php.ini
, you need to make sure that the OpenSSL extension is enabled to use thefile_get_contents
function of PHP to read that URL.
Solution 2:
To show:
50x50 pixels
<img src="//graph.facebook.com/{{fid}}/picture">
200 pixels width
<img src="//graph.facebook.com/{{fid}}/picture?type=large">
To save (using PHP)
NOTE: Don't use this. See @Foreever's comment below.
$img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
$file = dirname(__file__).'/avatar/'.$fid.'.jpg';
file_put_contents($file, $img);
Where $fid is your user id on Facebook.
NOTE: In case of images marked as "18+" you will need a valid access_token from a 18+ user:
<img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">
UPDATE 2015:
Graph API v2.0 can't be queried using usernames, you should use userId
always.
Solution 3:
UPDATE:
Starting end August 2012, the API has been updated to allow you to retrieve user's profile pictures in varying sizes. Add the optional width and height fields as URL parameters:
https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
where WIDTH
and HEIGHT
are your requested dimension values.
This will return a profile picture with a minimum size of WIDTH
x HEIGHT
while trying to preserve the aspect ratio. For example,
https://graph.facebook.com/redbull/picture?width=140&height=110
returns
{
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
"width": 148,
"height": 117,
"is_silhouette": false
}
}
END UPDATE
To get a user's profile picture, call
https://graph.facebook.com/USER_ID/picture
where USER_ID
can be the user id number or the user name.
To get a user profile picture of a specific size, call
https://graph.facebook.com/USER_ID/picture?type=SIZE
where SIZE
should be replaced with one of the words
square
small
normal
large
depending on the size you want.
This call will return a URL to a single image with its size based on your chosen type parameter.
For example:
https://graph.facebook.com/USER_ID/picture?type=small
returns a URL to a small version of the image.
The API only specifies the maximum size for profile images, not the actual size.
Square:
maximum width and height of 50 pixels.
Small
maximum width of 50 pixels and a maximum height of 150 pixels.
Normal
maximum width of 100 pixels and a maximum height of 300 pixels.
Large
maximum width of 200 pixels and a maximum height of 600 pixels.
If you call the default USER_ID/picture you get the square type.
CLARIFICATION
If you call (as per above example)
https://graph.facebook.com/redbull/picture?width=140&height=110
it will return a JSON response if you're using one of the Facebook SDKs request methods. Otherwise it will return the image itself. To always retrieve the JSON, add:
&redirect=false
like so:
https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
Solution 4:
To get the image URL, NOT binary content:
$url = "http://graph.facebook.com/$fbId/picture?type=$size";
$headers = get_headers($url, 1);
if( isset($headers['Location']) )
echo $headers['Location']; // string
else
echo "ERROR";
You must use your FACEBOOK ID, NOT USERNAME. You can get your facebook id there:
http://findmyfbid.com/