0

我正在使用 javascript + PHP

            <!DOCTYPE html>
<html xmlns:fb='http://www.facebook.com/2008/fbml'>
  <body>
        <fb:login-button scope='email,user_photos'></fb:login-button>
        <div id='fb-root' ></div>
                <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '478449982166112',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>.
      </body>
</html>

如您所见,我正在请求电子邮件权限。

然后我从 PHP 调用 /me:

 $user = $facebook->getUser();

 if ($user) {
    try {
        // if user is logged on to facebook, the account details will be saved in $me variable
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        // if user is not logged on to facebook, set $user to null . 
        $user = null;
    }
 }

但它不包含电子邮件字段。

4

1 回答 1

0

您不是email在 PHP 中请求权限,而是在 Javascript 中请求权限。Javascript 在您的浏览器中运行,PHP 在服务器上运行。

要在 PHP 中生成登录 URL 并重定向到它,请尝试以下操作:

$user = $facebook->getUser();

if (!$user) {
    header('Location: ' . $facebook->getLoginUrl(array('scope' => 'email')));
    exit;
} else {
    try {
        // if user is logged on to facebook, the account details will be saved in $me variable
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        // if user is not logged on to facebook, set $user to null . 
        $user = null;
    }
}
于 2012-08-23T12:17:21.540 回答