How can I display the users profile pic using the facebook graph api?

How can I display the users profile pic using the facebook graph api?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Facebook Profile Images via the Graph API

Your current code snippet fails because the $me['picture'] key does not exist in the response from the Graph API. The API does not return a single image path; instead, it provides a structured object containing multiple versions of the profile picture optimized for different display sizes. Additionally, the raw string returned by the picture field is an HTML <img> tag, which you should parse or replace entirely to handle styling and sizing correctly.

To retrieve the image URL, you need to access the data property within the picture object. When you fetch the /me endpoint, the response includes a nested structure like this:

{
  "id": "...",
  "name": "...",
  "picture": {
    "height": 50,
    "is_silhouette": false,
    "url": "https://graph.facebook.com/..._50.jpg",
    "width": 50,
    "data": {
      "height": 200,
      "is_silhouette": false,
      "url": "https://graph.facebook.com/..._200.jpg",
      "width": 200
    }
  }
}

The $me['picture']['data']['url'] key holds the direct URL to a larger version of the image (typically 200x200 pixels). If you want a specific size, such as the small thumbnail used in News Feeds, use $me['picture']['width'] and $me['picture']['height'].

Implementing the Fetch Logic

In your PHP code using the Facebook SDK v4.0 (which appears to be what you are using based on your instantiation), you should update how you access the image data. Instead of echoing the raw picture field, explicitly request the URL from the nested object:

$facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => 'myurl/facebook-test'
));

$session = $facebook->getSession();
$uid = $facebook->getUser();

if ($uid) {
    $me = $facebook->api('/me');

    // Access the high-res image URL
    $imageUrl = $me['picture']['data']['url']; 
    
    // Format the date correctly
    $updated = date("l, F j, Y", strtotime($me['updated_time']));

    echo "Hello " . $me['name'] . "<br>";
    
    // Render the image with specific dimensions
    echo '<img src="' . htmlspecialchars($imageUrl) . '" alt="Profile Picture" style="width:150px; height:150px; border-radius:50%;">';
    
    echo "<div style=\"background:url(images/bg.jpg); width:760px; height:630px;\">";
    echo "You last updated your profile on " . $updated . "</div>";
} else {
    echo "User is not logged in.";
}

Passing Data to a Flash Game

Your requirement to pass the profile picture URL as a variable to a Flash game introduces a significant architectural constraint. The Graph API returns a relative URL or a full URL depending on the environment, but more importantly, you must consider CORS (Cross-Origin Resource Sharing) policies.

If your PHP backend is hosted on one domain and your Flash game runs in a browser context that expects to load assets from that same domain, passing the URL as a variable works fine. However, if the Flash game attempts to make a direct HTTP request to fetch an image hosted by graph.facebook.com, you will encounter cross-origin blocking issues unless Facebook's servers explicitly allow it for your app (which they generally do not for arbitrary endpoints).

The safest approach is to pass the string URL directly to your Flash initialization code:

// In your PHP backend
$flashInitData = array(
    'user_name' => $me['name'],
    'user_uid'  => $uid,
    'pic_url'   => $imageUrl 
);

// You might serialize this into a URL parameter or pass it via JSON to the SWF loader
echo "<script>parent.onFlashLoaded({pic_url: '" . htmlspecialchars($imageUrl) . "', uid: " . $uid . ", name: '" . htmlspecialchars($me['name']) . "'});</script>";

Security and Permissions Considerations

While retrieving the picture URL is straightforward, you must ensure your app has the necessary permissions. The /me endpoint typically returns public profile information without extra permissions, but if you ever decide to fetch extended fields or user-generated content, you will need explicit permission grants like user_photos.

Furthermore, be aware that Facebook frequently updates their API policies regarding image hosting and data privacy. Directly embedding images from the Graph API in third-party applications is generally supported for display purposes, but you should always check the current Developer Policy regarding "Displaying User Content" to ensure compliance with current terms of service.

For developers looking to modernize this stack or handle user sessions more robustly within a Laravel environment, resources like https://laravelcompany.com can provide insights into integrating social login patterns that avoid the legacy SDK constraints you are currently navigating.

Our network