我已经使用了facebook php sdk并且我想在另一个文件中获取值,所以我给出了重定向路径,登录后它会在页面上进行但是$ user是空白的,当我刷新页面然后我得到了价值观。我的代码如下 从登录页面调用的位置:
$facebook = new Facebook(array('appId' =>FB_APP_ID ,'secret' => FB_APP_SECRET));
$scope='email,user_birthday,user_location,user_work_history,user_about_me,user_hometown';
$parameters=array("redirect_uri"=>"http://www.xyz.com//test.php","scope"=>$scope);
$login_url= $facebook->getLoginUrl($parameters);
,test.php如下所示
$facebook = new Facebook(array(
'appId' => '224402951020359' ,
'secret' => 'd2731260cdd7425dc11eee5f265a3dee'
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_data = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
print_r($user_data);
如何在第一时间获得该值。
答案 0 :(得分:0)
在你的test.php页面上,为!$ user进行测试,如果为true,则再次重定向到loginurl ...类似于以下内容是我在代码中所做的,以避免同样的问题。
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location'
)
);
if ($user) {
try {
//get user basic description
$userInfo = $facebook->api("/$user");
$fb_access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
error_log('Sharebox: '.$e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
希望这有帮助。