拜托,我需要帮助。我正在处理这个问题1个月!
我想使用 PHP 和 php-sdk 3.1.1 实现 facebook 连接登录到我的网站。简而言之,我的代码离线工作(在本地主机上)但不能在线工作,这导致“重定向循环过多(在 Chrome 上)”:错误 310(net::ERR_TOO_MANY_REDIRECTS):重定向过多。
这是我的代码:
1/ 我加载 facebook 连接 SDK 并初始化它:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '209633612480053',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
请注意,我在 facebook-developper 页面上创建了两个应用程序,一个用于离线测试,另一个用于在线测试。而且我肯定在测试时在两对appId/secret(在线和离线)之间正确切换。所以不是facebbok-connect init坏的问题。
2/ 我尝试获取用户信息:
$uid = $facebook->getUser();
if($uid)
{
/*
* Get user information.
*/
$user = $facebook->api('me/');
print_r($user); // Display user info.
}
else
{
/*
* Redirect to FB login URL to allow access.
*/
$loginURL = $facebook->getLoginURL();
echo '<script> top.location.href=\''.$loginURL.'\'</script>';
}
It's as simple as that: If user ic connected to facebook, so display it's information, else, redirect to facebook login page to allow access.
IT WORKS PERFECTLY OFFLINE, but online, I get a chrome error:
This webpage has a redirect loop
The webpage at https://www.facebook.com/dialog/oauth?client_id=209633612480053&redirect_uri=http%3A%2F%2Fwww.bluward.com%2Foauth%2Ffacebook&state=551f60cd4be6cd8ed1622f8168a5219a#_=_ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
Some additional information: Online, I use 1and1 host provider, and to be sure to have the same server configuration than offline server (which is a MAMP Pro), I uploaded the sams php.ini file.
Please, if someone has an idea, or get a similar problem, I'll be glad to have help.
Thank you in advance for all help.
UPDATE:
I updated my code to focus on the problematic line, so instead of redirecting to facebook login page, I display the redirect URL, so I just have to click to login:
$uid = $facebook->getUser();
if($uid)
{
/*
* Get user information.
*/
$user = $facebook->api('me/');
print_r($user); // Display user info.
}
else
{
/*
* Redirect to FB login URL to allow access.
*/
$loginURL = $facebook->getLoginURL();
echo $loginURL; // <-- HERE I CHANGED THE CODE TO DISPLAY LOGIN URL
}
What I noticed is that facebook is infinitely redirecting to my script page. Only code
parameter on URL bar changes.
So, why facebbok is redirecting to my script page without giving me user info?
Please, any idea?