Facebook PHP SDK v4.0 FacebookJavaScriptLoginHelper返回状态为NULL [英] Facebook PHP SDK v4.0 FacebookJavaScriptLoginHelper return state NULL

查看:178
本文介绍了Facebook PHP SDK v4.0 FacebookJavaScriptLoginHelper返回状态为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FB PHP SDK 4.0在我的本地主机上工作。我可以使用JS SDK完成登录,现在我试图通过PHP SDK访问一些信息。



在每个页面上,我添加这个js和php代码。



JavaScipt / p>

  window.fbAsyncInit = function(){
FB.init({
appId:'app-id ',
xfbml:true,
version:'v2.1',
status:true,
cookie:true

});

FB.getLoginStatus(function(response){
statusChangeCallback(response);
});
};

(function(d,s,id){
var js,fjs = d.getElementsByTagName(s)[0];
if(d.getElementById(id) {return;}
js = d.createElement(s); js.id = id;
js.src =//connect.facebook.net/en_US/sdk.js;
fjs.parentNode.insertBefore(js,fjs);
}(document,'script','facebook-jssdk'));

PHP

 在session_start(); 
// NO COMPOSER
require'/path-to/autoload.php';
使用Facebook\FacebookSession;
使用Facebook\FacebookJavaScriptLoginHelper;

FacebookSession :: setDefaultApplication('app-id','app-secret');

$ helper = new FacebookJavaScriptLoginHelper();
try {
$ session = $ helper-> getSession();
} catch(FacebookRequestException $ ex){
//当Facebook返回错误
} catch(\Exception $ ex){
//当验证失败或其他本地问题
}
if($ session){
//登录。
}

在JavaScript调用FB.getLoginStatus()时,它会给我一个成功的响应。但是在PHP方面,只要转储 $ helper ,它将返回 [state] => NULL 。我确定我的应用程序匹配。



我以为他们通过 cookie 沟通,但是当我查看我的浏览器Cookie和网站数据我无法找到任何相关内容。或者我需要一个访问令牌来完成这个工作?

解决方案

当我在Firefox上测试时,它正常工作。这实际上是Chrome的问题: Google Chrome不支持本地文件(包括localhost)的Cookie,除非您使用--enable-file-cookies标志启动。但是,如果您使用本地IP地址127.0.0.1进行测试,则Chrome会支持Cookie。



请参阅 https://stackoverflow.com/a/10081969/1158909


Im working on my localhost, using FB PHP SDK 4.0. I was able to complete the login using JS SDK, now Im trying to access some info via PHP SDK.

On each page I add this js and php code.

JavaScipt

window.fbAsyncInit = function() {
  FB.init({
    appId      : 'app-id',
    xfbml      : true,
    version    : 'v2.1',        
    status     : true,
    cookie     : true

  });

  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });
};

(function(d, s, id){
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement(s); js.id = id;
   js.src = "//connect.facebook.net/en_US/sdk.js";
   fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

PHP

session_start();
//NO COMPOSER
require '/path-to/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookJavaScriptLoginHelper;

FacebookSession::setDefaultApplication('app-id', 'app-secret');

$helper = new FacebookJavaScriptLoginHelper();
try {
   $session = $helper->getSession();
} catch(FacebookRequestException $ex) {
  // When Facebook returns an error
} catch(\Exception $ex) {
  // When validation fails or other local issues
}
if ($session) {
  // Logged in.
}

On JavaScript whenever I call the FB.getLoginStatus() it gives me a success response. However on the PHP side whenever I dump the $helper it returns with ["state"]=> NULL. I made sure my app-id matches.

I thought that they communicate through a cookie but when I view my browser Cookies and Site data I cant find anything relevant. Or do I need an access token to get this done?

解决方案

It's working when I tested on Firefox. This is actually an issue with Chrome: Google Chrome does not support cookies for local files (including localhost) unless you start it with the --enable-file-cookies flag. Chrome does, however, support cookies if you use your local IP address—127.0.0.1— for testing.

See https://stackoverflow.com/a/10081969/1158909

这篇关于Facebook PHP SDK v4.0 FacebookJavaScriptLoginHelper返回状态为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆