我正在使用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;
}
}
但它不包含电子邮件字段。
答案 0 :(得分:0)
您不是在PHP中请求email
权限,仅在Javascript中。 Javascript在您的浏览器中运行,PHP在服务器上运行。
要生成登录URL并在PHP中重定向到它,请尝试以下操作:
$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;
}
}