从Facebook PHP SDK获取用户的电子邮件ID

时间:2012-04-19 10:44:50

标签: php facebook facebook-php-sdk

从Facebook PHP SDK获取用户的电子邮件ID

我尝试使用以下代码

第一

$user_profile = $facebook->api('/me');  // or $facebook->api('/me?fields=email')
print_r($user_profile);

第二

$user = $facebook->getUser();
$url = "https://api.facebook.com/method/fql.query";
    $url .= "?access_token=" . $access_token;
    $url .= "&query=SELECT email FROM user WHERE uid={$user}";
    $userData = simplexml_load_file($url);
    $userEmail = $userData->user->email;
    print_r($userEmail);

第三

    $user = $facebook->getUser();
$fql    =   "select name, hometown_location, sex, pic_square, email from user where uid='$user'";
        $param  =   array(
           'method'     => 'fql.query',
            'query'     => $fql,
            'access_token' =>$access_token ,
          'callback'    => ''
        );

        $fqlResult2   =   $facebook->api($param);

    print_r($fqlResult2);

这些都不会获得登录用户的电子邮件ID。如何检索?

3 个答案:

答案 0 :(得分:2)

FBML (这将很快成为history

<fb:login-button autologoutlink="true" perms="email"></fb:login-button>

<强> PHP

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

<强> JS

FB.login(function(response) {
    if (response.authResponse) {
       // conosle.log('User logged in');
    } else {
       // console.log('User cancelled login or did not fully authorize.');
    }
}, {
    scope: 'email'
});​

使用图表api

获取email
FB.api('/me', function(user) {
    if (user) {
        User.email = user.email;
    }
});​

答案 1 :(得分:0)

您需要有权访问用户的电子邮件地址。因此,您的申请必须要求“电子邮件” permission

This site有一些指南如何从用户那里获得这些权限(或者更确切地要求他们)。

根据JS SDK documentation,您需要执行以下操作:

FB.login(function(response) {
   // handle the response
}, {scope: 'email,user_likes,...'});

答案 2 :(得分:0)

更改您的代码,如下所示:

$facebook->api('/me?locale=en_US&fields=name,email' );

getLoginUrl( array(
    'scope' => 'email'
));