Facebook API,我没有电子邮件字段


Facebook API, no email field in /me

我使用的是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:打电话给我

 $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;
    }
 }

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

您不是在PHP中请求email权限,而是在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;
    }
}