2015-05-12 68 views
0

我有这个代码,它工作好几个月。突然,生产环境停止工作,其中$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的最新版本。

回答

1

经过长期诊断,我发现有差异。自从我使用Cloudflare的SSL功能以来,原始$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');请求使用http:80进行,但第二个$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');使用https:443进行......这会向Facebook返回一个无匹配类型的签名,这会导致$session变量。

TL; DR在向fb发出请求时不会混合使用http和https。

+0

谢谢你,这不完全是我的问题,但它给了我一个主意。我访问的网站没有www和facebook重定向到www。不知道它重要 – s3nzoM

相关问题