I'm working on a CMS that fetches a user's profile image from their Facebook URL (that is, http://facebook.com/users_unique_url). How can I accomplish this? Is there a Faceboook API call that fetches a user's profile image URL without the user needing to Allow the application?
14 Answers
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.
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.
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
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:
Added this as a comment to accepted answer, but felt it deserved a longer explanation. Starting around April 2015 this will probably be raised a few times.
As of V2 of the graph api the accepted answer no longer works using a username. So now you need the userid first, and you can no longer use a username to get this. To further complicate matters, for privacy reasons, Facebook is now changing userid's per app (see https://developers.facebook.com/docs/graph-api/reference/v2.2/user/ and https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids ), so you will have to have some kind of proper authentication to retrieve a userid you can use. Technically the profile pic is still public and available at /userid/picture (see docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture and this example user: http://graph.facebook.com/v2.2/4/picture?redirect=0) however figuring out a user's standard userid seems impossible based just on their profile - your app would need to get them to approve interaction with the app which for my use case (just showing a profile pic next to their FB profile link) is overkill.
If someone has figured out a way to get the profile pic based on username, or alternatively, how to get a userid (even an alternating one) to use to retrieve a profile pic, please share! In the meantime, the old graph url still works until April 2015.
One way is to use the code Gamlet posted in his answer:
Save it as
curl.php
Then in your file:
require 'curl.php'; $photo="https://graph.facebook.com/me/picture?access_token=" . $session['access_token']; $sample = new sfFacebookPhoto; $thephotoURL = $sample->getRealUrl($photo); echo $thephotoURL;
I thought I would post this, because it took me a bit of time to figure out the particulars... Even though profile pictures are public, you still need to have an access token in there to get it when you curl it.
Working PHP (HTTP GET) solution from April 2015 (without PHP 5 SDK):
function get_facebook_user_avatar($fbId){
$json = file_get_contents('https://graph.facebook.com/v2.5/'.$fbId.'/picture?type=large&redirect=false');
$picture = json_decode($json, true);
return $picture['data']['url'];
}
You can change 'type' in parametr:
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.
There is way to do that ;)
Thanks to "http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers":
<?php
/**
* Facebook user photo downloader
*/
class sfFacebookPhoto {
private $useragent = 'Loximi sfFacebookPhoto PHP5 (cURL)';
private $curl = null;
private $response_meta_info = array();
private $header = array(
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Connection: close"
);
public function __construct() {
$this->curl = curl_init();
register_shutdown_function(array($this, 'shutdown'));
}
/**
* Get the real URL for the picture to use after
*/
public function getRealUrl($photoLink) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_URL, $photoLink);
//This assumes your code is into a class method, and
//uses $this->readHeader as the callback function.
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array(&$this, 'readHeader'));
$response = curl_exec($this->curl);
if (!curl_errno($this->curl)) {
$info = curl_getinfo($this->curl);
var_dump($info);
if ($info["http_code"] == 302) {
$headers = $this->getHeaders();
if (isset($headers['fileUrl'])) {
return $headers['fileUrl'];
}
}
}
return false;
}
/**
* Download Facebook user photo
*
*/
public function download($fileName) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_URL, $fileName);
$response = curl_exec($this->curl);
$return = false;
if (!curl_errno($this->curl)) {
$parts = explode('.', $fileName);
$ext = array_pop($parts);
$return = sfConfig::get('sf_upload_dir') . '/tmp/' . uniqid('fbphoto') . '.' . $ext;
file_put_contents($return, $response);
}
return $return;
}
/**
* cURL callback function for reading and processing headers.
* Override this for your needs.
*
* @param object $ch
* @param string $header
* @return integer
*/
private function readHeader($ch, $header) {
//Extracting example data: filename from header field Content-Disposition
$filename = $this->extractCustomHeader('Location: ', '\n', $header);
if ($filename) {
$this->response_meta_info['fileUrl'] = trim($filename);
}
return strlen($header);
}
private function extractCustomHeader($start, $end, $header) {
$pattern = '/'. $start .'(.*?)'. $end .'/';
if (preg_match($pattern, $header, $result)) {
return $result[1];
}
else {
return false;
}
}
public function getHeaders() {
return $this->response_meta_info;
}
/**
* Cleanup resources
*/
public function shutdown() {
if($this->curl) {
curl_close($this->curl);
}
}
}
I was thinking - maybe ID will be a useful tool. Every time a user creates a new account it should get a higher ID. I googled and found that there is a method to estimate the account creation date by ID and Massoud Seifi from metadatascience.com gathered some good data about it.
Read this article:
And here are some IDs to download:
The blog post Grab the Picture of a Facebook Graph Object might offer another form of solution. Use the code in the tutorial along with the Facebook's Graph API and its PHP SDK library.
... And try not to use file_get_contents (unless you're ready to face the consequences - see file_get_contents vs curl).
Are you concerned about the profile picture size? at the time of implementing login with Facebook using PHP. We’ll show you the simple way to get large size profile picture in Facebook PHP SDK. Also, you can get the custom size image of Facebook profile.
Set the profile picture dimension by using the following line of code.
$userProfile = $facebook->api('/me?fields=picture.width(400).height(400)');
check this post:http://www.codexworld.com/how-to-guides/get-large-size-profile-picture-in-facebook-php-sdk/
@NaturalBornCamper,
Nice code! Here is a clean-cut code function for such process!
function get_raw_facebook_avatar_url($uid)
{
$array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1);
return (isset($array['Location']) ? $array['Location'] : FALSE);
}
This will return the raw Facebook avatar image URL. Feel free to do whatever you want with it then!
URI = https://graph.facebook.com/{}/picture?width=500'.format(uid)
You can get the profile URI via online facebook id finder tool
You can also pass type param with possible values small, normal, large, square.
Refer the official documentation