jquery - Can't get user id from facebook signedRequest in PHP . -
on index page initialize javascript sdk , go through login/authorization flow. use ajax pass signed request php page parsed. code parsing signed request copied directly documentation, haven't changed anything. @ end of code try user info returned in same object signed request, when try log of variables in ajax success callback, come 'undefined'.
//html function oncheckloginstatus (response) { if (response.status != "connected") { //redirect login page; } else { //connected, signed request response object , pass 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 coming undefined console.log(result.uid); }, error : function () { alert("error"); } }); //php <?php define('facebook_app_id', '27xxxxxx0&'); // place app id here define('facebook_secret', '8ea907xxxxxxx9e958'); // place app secret here //get signed request $signed_request = $_request['signed_request']; function parse_signed_request($signed_request, $secret) { list($encoded_sig, $payload) = explode('.', $signed_request, 2); // decode 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); ?>
either send entire $response
or
change echo line
echo($uid);
and change console.log line in ajax call
console.log(result);
you can verify logging result
$.ajax({ url : "http://xxxxxxx/bn/signedrequest.php", type : 'post', data: {signed_request: response.authresponse.signedrequest}, success : function (result) { console.log("success"); //this coming undefined console.log(result); // <----- check whether result undefined }, error : function () { alert("error"); } });
Comments
Post a Comment