第一次加载index.php时,调用时得到值0
$facebook->getUser();//returning 0 instead of id on first page load
该调用在随后的任何页面加载中都能正常工作。
这是我的代码:
$facebook = new Facebook(array(
'appId' => $FB_APP_ID,
'secret' => $FB_APP_SECRET
));
$user_id = $facebook->getUser();//Returns 0 on first load.
其他人经历过这种情况吗?如果$facebook->getUser();
没有返回有效用户,也许我应该自动重新加载页面?
可能不是第一次对其进行身份验证,您可以这样做:
<?php
$facebook = new Facebook(array(
'appId' => xxx,
'secret' => xxx
));
$user_id = $facebook->getUser();
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$me = $facebook->api('/me','GET');
echo "Name: " . $me['name'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll redirect and
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} else {
// No user, redirect for the user to login
$login_url = $facebook->getLoginUrl();
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
编辑:
如果错误仍然存在,请尝试此编辑,只需更改您的此行:
$me = $facebook->api('/me','GET');
到此:
$me= $facebook->api('/'.$user_id, 'GET');
希望这能解决你的问题。