Search code examples
phpfacebookfacebook-graph-api

Retrieving URL of Facebook profile picture


I have the following code that has a correct output:

$request = new FacebookRequest( $session, 'GET', '/me?fields=id,first_name,email,gender,birthday,picture.url' );
$response = $request->execute();

OUTPUT:

Facebook\GraphObject Object ( [backingData:protected] => Array ( [id] => 1412361249046879 [first_name] => Do [email] => [email protected] [gender] => male [birthday] => 06/30/1984 [picture] => stdClass Object ( [data] => stdClass Object ( [url] => https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/t1.0-1/p50x50/10406555_1412406989042185_8978818317524743009_t.jpg ) ) ) )

Then I set variables to call later in my html:

  $uid = $response->getGraphObject()->getProperty('id');
  $name = $response->getGraphObject()->getProperty('first_name');
  $email = $response->getGraphObject()->getProperty('email');
  **$pic = $response->getGraphObject()->getProperty('picture.data.url');**
  $sex = $response->getGraphObject()->getProperty('gender');
  $bday = $response->getGraphObject()->getProperty('birthday');

The problem is the $pic variable doesn't work. I don't understand how to call the variable that is further down the GraphObject tree (ie. picture->data->url)


Solution

  • $request = new FacebookRequest( $session, 'GET', '/me?fields=id,first_name,email,gender,birthday,picture.url' );
    $response = $request->execute();      
    $array = $response->getResponse();
    $pic = $array->picture->data->url;
    

    Here is what ended up solving it. I had to stop using the getGraphObject() function and just use the standard getResponse() so that I could treat it more like a regular array. Hope this helps somebody out.