2013-07-25 94 views
0

在我的索引頁上,我初始化javascript sdk並完成登錄/授權流程。然後我使用AJAX將簽名的請求傳遞給我的php頁面進行解析。解析簽名請求的代碼直接從文檔中複製,我沒有改變任何東西。在代碼結束時,我嘗試獲取與簽名請求相同的對象中返回的用戶信息,但是當我嘗試在AJAX成功回調中記錄任何這些變量時,它們會出現「未定義」。無法在PHP中從facebook signedRequest獲取用戶標識。

//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); 



?> 

回答

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"); 
     } 
    }); 
相關問題