在我的索引页面上,我初始化了javascript sdk并完成了登录/授权流程。然后我使用AJAX将签名的请求传递给我的php页面进行解析。解析签名请求的代码直接从文档中复制,我没有改变任何东西。在该代码的最后,我尝试获取与签名请求在同一对象中返回的用户信息,但是当我尝试在AJAX成功回调中记录任何这些变量时,它们会出现“未定义”。
//HTML
function onCheckLoginStatus (response)
{
if (response.status != "connected")
{
//redirect to login page;
}
else
{
//CONNECTED, Get signed request from response object and pass it to PHP page via AJAX
$.ajax({
url : "http://XXXXXXX/bn/signedRequest.php",
type : 'POST',
data: {signed_request: response.authResponse.signedRequest},
success : function (result) {
console.log("success");
//THIS IS COMING UP UNDEFINED
console.log(result.uID);
},
error : function () {
alert("error");
}
});
//PHP
<?php
define('FACEBOOK_APP_ID', '27XXXXXX0&'); // Place your App Id here
define('FACEBOOK_SECRET', '8ea907XXXXXXX9e958'); // Place your App Secret Here
//GET THE SIGNED REQUEST
$signed_request = $_REQUEST['signed_request'];
function parse_signed_request($signed_request, $secret)
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig)
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST)
{
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
}
$uID = $response["user_id"];
$name = $response["registration"]["name"];
$city = $response["registration"]["location"]["name"];
echo json_encode($uID);
?>
答案 0 :(得分:1)
发送整个$response
或
将回波线更改为
echo($uID);
并将AJAX调用中的console.log行更改为
console.log(result);
您只需记录结果即可自行验证
$.ajax({
url : "http://XXXXXXX/bn/signedRequest.php",
type : 'POST',
data: {signed_request: response.authResponse.signedRequest},
success : function (result) {
console.log("success");
//THIS IS COMING UP UNDEFINED
console.log(result); // <----- Check whether result is undefined
},
error : function () {
alert("error");
}
});