Php Facebook connect突然无法正确重定向
Facebook是不是在过去几天里随机改变了他们的API?我的网站与Facebook的API配合得很好,但现在突然之间它根本不起作用了。不,我没有更改任何内容,它昨天刚刚决定不再重定向…它似乎只是尝试连接几次,然后显示此页面: 页面未正确重定向 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求。 *此问题有时可能是由于禁用或拒绝接受导致的 饼干 不管怎样,这里有一些代码可以让你头脑清醒:是的,我确实有我真正的应用程序id之类的东西 这是fbLogin_member.php文件,单击登录链接后,您将直接访问该文件:Php Facebook connect突然无法正确重定向,php,facebook,facebook-graph-api,Php,Facebook,Facebook Graph Api,Facebook是不是在过去几天里随机改变了他们的API?我的网站与Facebook的API配合得很好,但现在突然之间它根本不起作用了。不,我没有更改任何内容,它昨天刚刚决定不再重定向…它似乎只是尝试连接几次,然后显示此页面: 页面未正确重定向 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求。 *此问题有时可能是由于禁用或拒绝接受导致的 饼干 不管怎样,这里有一些代码可以让你头脑清醒:是的,我确实有我真正的应用程序id之类的东西 这是fbLogin_member.ph
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
if (empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url) ."&scope=email";
echo "<script> top.location.href='". $dialog_url ."'</script>";
}
$code = $_REQUEST['code'];
以下是confirmlogin\u fb\u member.php文件:
//if user denies access to your website, take him to your manual login page
// if ($_GET['error']) {
// header("Location: memberlogin.php");
// exit;
// }
require_once('config/dbconfig.php');
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
$code = $_GET['code'];
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
. $app_secret . "&code=" . $code;
// request access token
//use curl and not file_get_contents()
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$access_token = curl_exec($ch);
curl_close($ch);
$graph_url = "https://graph.facebook.com/me?" . $access_token;
// request user data using the access token
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$temp_user = curl_exec($ch);
curl_close($ch);
//decode the json array to get user data
$user = json_decode($temp_user);
//store user data
$u = $user->name;
$e = $user->email;
$fb_id = $user->id;
$username = $user->username;
$picture = 'https://graph.facebook.com/'. $fb_id .'/picture';
//check if user has already signed up before
$insert = true;
$result = mysql_query("SELECT * FROM members") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//if username already exists, do not insert
if (($row['name'] == $u) && ($row['userType'] == "facebook_user")) {
$insert = false;
}
}
// Random Password Generator
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
// end generator
// if new user, insert user details in your mysql table
if ($insert) {
mysql_query("INSERT INTO members(name, fb_username, password, email, profile_pic, userType) VALUES('$u', '$username', '$pass' , '$e', '$picture', 'facebook_user')") or die(mysql_error());
}
//login user
if (!session_start()) session_start();
$_SESSION['in'] = true;
$_SESSION['userName'] = $u;
$_SESSION['userType'] = "facebook_user";
$_SESSION['userEmail'] = $e;
//take user to his/her homepage
header("Location: layout.php");
最后,这里是layout.php的顶部,其中调用了Facebook API会话:
session_start();
require_once('config/dbconfig.php');
require_once('facebook/facebook.php');
if (isset($_REQUEST['logout'])) {
unset($_SESSION['in']);
unset($_SESSION['userName']);
unset($_SESSION['userType']);
unset($_SESSION['userEmail']);
session_destroy();
header("Location: layout.php");
}
$session = $_SESSION['in'];
if (!$session) {
$login = '<a href="fbLogin_member.php" class="fbLogin">Login with Facebook</a>';
$tooltipMsg = '<p>You must <strong>Log in</strong> to vote.</p>';
} else {
$sessionUser = $_SESSION['userName'];
$result = mysql_query("SELECT * FROM `members` WHERE name = '$sessionUser'") or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if ($result) {
$sessionRow = mysql_fetch_array($result);
$sessionUserid = $sessionRow['memberid'];
}
if ($sessionRow['userType'] == "facebook_user") {
$facebook = new Facebook(array(
'appId' => 'my app id #',
'secret' => 'my secret id',
'cookie' => true
));
// $session = $facebook->getSession();
$user = $facebook->getUser();
$me = null;
// Session based API call.
if ($user) {
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
// error_log($e);
}
}
}
这让我难以置信,它已经好几个星期了,现在当我离开家一天半的时候,它就不起作用了。如果您有任何帮助,我们将不胜感激,即使您在我的编码中看到其他一些错误,这是我第一次尝试使用facebook api:听起来您可能有无限重定向问题 如果要在分配$code之前检查是否设置了它,请尝试翻转这两条语句的顺序: $code=$_请求['code'] 如果$code为空{ $dialog\u url=http://www.facebook.com/dialog/oauth?client_id=. $app\u id.&redirect\u uri=.urlencode$my\u url.&scope=email; echo top.location.href='.$dialog_url';
}听起来您可能有无限重定向问题 如果要在分配$code之前检查是否设置了它,请尝试翻转这两条语句的顺序: $code=$_请求['code'] 如果$code为空{ $dialog\u url=http://www.facebook.com/dialog/oauth?client_id=. $app\u id.&redirect\u uri=.urlencode$my\u url.&scope=email; echo top.location.href='.$dialog_url';
}您可能需要在base\u facebook.php中的受保护函数getCode中将$\u REQUEST更改为$\u GET
protected function getCode() {
if (isset($_GET['code'])) {
if ($this->state !== null &&
isset($_GET['state']) &&
$this->state === $_GET['state']) {
// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $_GET['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}
您可能需要在base_facebook.php中的受保护函数getCode中将$\u REQUEST更改为$\u GET
protected function getCode() {
if (isset($_GET['code'])) {
if ($this->state !== null &&
isset($_GET['state']) &&
$this->state === $_GET['state']) {
// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $_GET['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}
API昨天更改了…更多信息@你应该订阅。为什么你不能使用..?啊,算了吧…谢谢!我将重新考虑修复它:@Mike我看不到API中有任何会破坏其功能的变化。你指的是什么?我没有看到SDK的任何更新,所以我不确定是什么问题…API昨天更改了…更多信息@你应该订阅。为什么你不能使用..?啊,想想看…谢谢!我将重新考虑修复它:@Mike我看不到API中有任何会破坏其功能的变化。你指的是什么?我没有看到SDK的任何更新,所以我不确定是什么错了。。。