我从未使用过Facebook SDK 4,我在这里看了很多答案,但都没有成功。
我正试图使用api的facebook登录部分,但每次我从facebook直接返回时,都没有存储会话。。这是代码:
<?php
// I am using autoload just to make sure it was including the requested classes, it makes no difference if i include them separately the outcome is the same.
function __autoload($classname) {
$filename = "../Facebook/". end(explode("''",$classname)) .".php";
include_once($filename);
}
use Facebook'FacebookSession;
use Facebook'FacebookRedirectLoginHelper;
use Facebook'FacebookRequest;
use Facebook'FacebookResponse;
use Facebook'FacebookSDKException;
use Facebook'FacebookRequestException;
use Facebook'FacebookOtherException;
use Facebook'FacebookAuthorizationException;
use Facebook'GraphObject;
use Facebook'GraphSessionInfo;
// start session
session_start();
// init app with app id and secret -
FacebookSession::setDefaultApplication( '****','****' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://dev.mydomain.co.uk/fb/index.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch('Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
print('loggedin');
} else {
print('no session');
$loginUrl = $helper->getLoginUrl();
?>
<a href="<?=$loginUrl?>">login</a>
<?php
}
我已经替换了应用程序id和机密,重定向uri是正确的,但我只是在这里替换了域。
它返回到我的登录页面,只是总是打印"无会话"。所以看起来$session = $helper->getSessionFromRedirect();
没有做我需要做的事情。
我从facebook开发者网站上得到了这个代码。
我尝试过的东西:
- 单独包含类文件,而不是自动加载
- 将整个内容移到一个单独的目录中,index.php文件中没有其他内容
- 删除了我的.htaccess文件,以确保它不会干扰GET请求
- 我还尝试了一些不同的教程,即http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/完全使用他们的代码(但替换app_id、app_sec和url位(-但我仍然没有返回任何会话
我有点不知所措,所以如果有人有这方面的经验,这将对有帮助
试试这个,我遇到了和你一样的问题:http://www.codeproject.com/Tips/777252/Integrating-Facebook-Account-into-a-Log-in-Session