我试图从解码数据中获取user_id。
如下所示,使用algorithm
完成同样的事情
问题是,在注册时,数据库中的user_id字段为空,因此它不起作用。
我做错了什么?
Facebook链接:https://developers.facebook.com/docs/plugins/registration/
这是我的进程php。
<?php
define('FACEBOOK_APP_ID', ''); // Place your App Id here
define('FACEBOOK_SECRET', ''); // Place your App Secret Here
// No need to change the function body
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;
}
$user_fbid = $data['user_id'];
// 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);
$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$password = $response["registration"]["password"];
$gender = $response["registration"]["gender"];
$dob = $response["registration"]["birthday"];
// Connecting to database
mysql_connect("", "", "")or die("cannot connect");
mysql_select_db("")or die("cannot select DB");
// Inserting into users table
$result = mysql_query("INSERT INTO `users` (`fullname`, `email`, `password`, `gender`, `dob`, `user_id`) VALUES ('$name', '$email', '$password', '$gender', '$dob', '$user_fbid')");
if($result){
// User successfully stored
}
else
{
// Error in storing
}
}
else
{
echo '$_REQUEST is empty';
}
?>
答案 0 :(得分:1)
您在$user_fbid
- 函数中定义parse_signed_request()
,然后在函数不在范围内时将其输出到函数外部:)
但是使用$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
,您将user_id所在的$data
- 数组返回到$response
,然后您可以从那里访问它。