我有这个代码,它工作好几个月。突然,生产环境停止工作,其中$helper->getSessionFromRedirect()
现在总是返回NULL
的值。
登录表单页面(的index.php)
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication(123456, 'a1b2c3d4e5');
$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
$scope = array('email');
$loginUrl = $helper->getLoginUrl($scope);
echo '<a href="'.$loginUrl.'">Login</a>';
登录处理(login.php中)
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication(123456,'a1b2c3d4e5');
$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
try {
# success
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
# when Facebook returns an error
} catch(\Exception $ex) {
# when validation fails or other local issues
}
if($session) {
# do something with user data
$me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
# redirect to user profile page
header('Location: http://domain.com/profile.php');
exit();
} else {
# $session is always NULL with this code :(
}
这是在我的测试应用程序的工作,但它不工作与我的生产应用程序ID。这两者之间的唯一区别当然是App ID和Secret ID值。我已经确定我正在关注Facebook instructions。另外,我正在使用Facebook PHP SDK 4的最新版本。
谢谢你,这不完全是我的问题,但它给了我一个主意。我访问的网站没有www和facebook重定向到www。不知道它重要 – s3nzoM