简体   繁体   中英

Permission to get user email with Facebook PHP SDK

I have a working integration with facebok api via PHP SDK and working login. Now I tried to add the permission for email as well with the following code, but when trying to connect/login to the app the email is not requested.

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'req_perms' => 'email',
  ));
}

The error must be in this code since everything else works fine. Thanks!

$loginUrl = $facebook->getLoginUrl(array(
   'scope' => 'email'
));

This should be login url

<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>

also additional permission must be passed to scope variable

  $loginUrl = $facebook->getLoginUrl(array(
   'scope' => 'email, user_activities '
 ));

for additional permissions please visit:

http://developers.facebook.com/docs/authentication/permissions/#user_friends_perms

This should work with the PHP SDK:

header('Location:'.$facebook->getLoginUrl(array(
    'scope' => 'email'
)));
exit;

Remove the comma you have after 'email' and redirect the browser to $loginUrl, like this:

$loginUrl = $facebook->getLoginUrl(
  array(
    'req_perms' => 'email'
  )
);

echo '<script>top.location="'.$loginUrl.'";</script>';
die();

EDIT

This was how it was done back in the day, now facebook have changed their API:s. For a more current solution, look at the other answers on this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM