Grab the Picture of a Facebook Graph Object

Here's what you need to do to get to the actual location of a Facebook profile picture using PHP cURL (this comes in handy if you plan to store the picture somewhere else). In particular, we can grab the pictures of People, Events, Groups, Pages, Applications, and Photo Albums (these are called objects in Graph API).

You can access the Graph's documentation with regards to Pictures at

Let's begin.

STEP 1/2

You need to know the User Id to whom we're gonna grab the picture from. For this tutorial, let's try to use the UID of Google's FB page: 104958162837 .

Append the UID to the Graph API's base url like this: .

Then add the suffix /picture to the object URL like this: .

You can actually view the picture now by using this link:

It looks like this:

STEP 2/2

In the first step, we learned how to construct the URL for viewing an FB picture. And that by clicking on the URL, we are redirected to the actual location of the image.

What we're trying to achieve here is that we wanted to grab the image through PHP per FB Graph object. And the only way for us to locate the image is through the Object URL, the same thing we did in the first Step. That is, by passing the UID of the object to the URL.

Now the question is, how do we do that in PHP?

It's fairly simple. In our CurlHelper class, we use the CURLOPT_FOLLOWLOCATION option for us to follow Facebook's redirection towards the actual picture of the object.

The class has one function. And this takes care of downloading the file from the URL.

class CurlHelper
   * Downloads a file from a url and returns the temporary file path.
   * @param string $url
   * @return string The file path
  public static function downloadFile($url, $options = array())
    if (!is_array($options))
      $options = array();
    $options = array_merge(array(
        'connectionTimeout' => 5, // seconds
        'timeout' => 10, // seconds
        'sslVerifyPeer' => false,
        'followLocation' => false, // if true, limit recursive redirection by
        'maxRedirs' => 1, // setting value for "maxRedirs"
        ), $options);

    // create a temporary file (we are assuming that we can write to the system's temporary directory)
    $tempFileName = tempnam(sys_get_temp_dir(), '');
    $fh = fopen($tempFileName, 'w');

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FILE, $fh);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
    curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
    curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);


    return $tempFileName;

Use the class like this:

$url = '';
$sourceFilePath = CurlHelper::downloadFile($url, array(
  'followLocation' => true,
  'maxRedirs' => 5,

From here, you can do whatever you want to the image.

Related Posts:

Bookmark and Share


mick said...

Thank you, Thank you, Thank you...that should tell it all :)

Anonymous said...

Any way to know if this is not the user default picture from facebook??

bassgrinder said...

Check this out.

User with default avatar:

Link to profile page:

Application with default avatar:

Link to profile page:


User with personalized avatar:

Link to profile page:


So as you can see, users and apps with default avatars redirects to images with urls having "/static-ak/". I think you can use that to determine if the profile image is yet a default one.

Neha said...

its a perfect just one doubt..
i am having a database...where i have to capture..the facebook id and the profile can be for n users...

so..exactly what can i do..can u please help me in any ways?

bassgrinder said...

Use the code above to get a copy of the user's image, save the file somewhere in your server, then save the relative path of that image in your database for that user. You can use a VARCHAR data type for the field. It's up to you how long the names are going to be. You can use a hash or a combination of that and the user's id.

ex: "/images/users/123/hasdfjk123hksdf.jpg"

To use this, you can simply concatenate the base url of your site during runtime like so:

ex: ""

Pramod said...

getting this error:-
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/a3111484/public_html/fb/index.php on line 31

shashank vaishnav said...

can any one please tell me how to save the image from that temporary url i am getting from $sourceFilePath... please help me

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)