我有一个网站,首先使用Facebook Javascript API来登录用户。但是,当用户向我的PHP脚本之一发送xmlhttp请求时,我想再次检查该用户是否通过PHP登录(为了安全起见)。
我有一个工作系统,但在一个网站改造后,似乎有一个bug。当我在facebook.php脚本中返回$html时,我得到这个错误:{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException"}}
.
/* request.js */
var postData = "id=" + id;
sendRequest('assets/php/believe.php',function(req) {
console.log(req.responseText);
},postData);
/* believe.php */
<?php
include("facebook.php");
$id = intval($_POST["id"]);
if($id == '') {
//Stuff
} else {
if($cookie) {
echo "hey";
}
}
?>
/* facebook.php */
<?php
define('YOUR_APP_ID', 'xxxxxx');
define('YOUR_APP_SECRET', 'xxxxxxxxxxxx');
function curl_get_contents($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$html = curl_exec($curl);
curl_close($curl);
echo "$html";
return $html;
}
function get_facebook_cookie($app_id, $app_secret){
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '''"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value){
if ($key != 'sig'){
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']){
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$user = json_decode(curl_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));
?>
我认为你的代码很好。用户访问令牌过期。如果用户更新它,代码将工作。确保在cookie