0

I have just followed this guide from facebook: https://developers.facebook.com/docs/php/gettingstarted

I am upgrading from an older version of the facebook php sdk.

Having successfully upgraded and authenticating a user with very little pain, i seem to have hit a brick wall.

As per their instructions I have set the $permissions to an array to include only the 1 string, email. however, the email is not returned. I now only get the facebook id and the users name.

How can I get the users email from the new facebook php sdk v2.5?

This is the code before the redirect which works fine:

$facebook = new \Facebook\Facebook($config);
$helper = $facebook->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl($_WEBROOT .'/authenticate_facebook.php', ['email']);

This is the code that handles the response, which works except i cannot see how to get the email:

$facebook = new  \Facebook\Facebook($config);
$helper = $facebook->getRedirectLoginHelper();

$accessToken = false;

try {
    $accessToken = $helper->getAccessToken();
} catch( \Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch( \Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

//if we don't have an access token redirect the user to the login page
if ( !$accessToken ) {
    \Controller::redirectTo('/login?facebook-login-error');
}

// Sets the default fallback access token so we don't have to pass it to each request
$facebook->setDefaultAccessToken( $accessToken );

try {
    $response = $facebook->get('/me');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    \Controller::redirectTo('/login?facebook-login-error');
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    \Controller::redirectTo('/login?facebook-login-error');
}

print_r( $response->getDecodedBody() );
die();

The result of the very last print_r is just:

Array ( [name] => Bob Smith [id] => 5995599559 )

The current application this is in requires the email address :/ and it used to get it in v2.0

1 Answers1

1
        $response = $facebook->get('/me?fields=email,name');

OK. So the guide on facebook does not mention this anywhere that I could see.

But by adding ?fields=email,name to the end of the get url worked.

The response body now contains the required fields

  • Mentioned in the changelog, https://developers.facebook.com/docs/apps/changelog#v2_4_changes, and the chapter on basic API usage, https://developers.facebook.com/docs/graph-api/using-graph-api/#fields – CBroe Aug 10 '16 at 08:28
  • yea sure it mentions it here... but nowhere in the php sdk example did it mention it. –  Aug 10 '16 at 14:39