加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在Facebook画布应用程序中,一旦我在登录中捕获它,我将在

发布时间:2020-12-13 17:45:20 所属栏目:PHP教程 来源:网络整理
导读:我正在使用 javascript sdk.在文档中,它表示您从响应对象获得FB.getLoginStatus()在用户状态=已连接时返回的签名请求,但现在我需要解析已签名的请求.如何将它发送到php页面我有解析代码?我是否在我的画布应用程序索引页面上包含php代码,然后将signedRequest
我正在使用 javascript sdk.在文档中,它表示您从响应对象获得FB.getLoginStatus()在用户状态=已连接时返回的签名请求,但现在我需要解析已签名的请求.如何将它发送到php页面我有解析代码?我是否在我的画布应用程序索引页面上包含php代码,然后将signedRequest发送到同一页面上的代码?或者将代码保存在单独的页面上并传递SR.

第一个代码块在我的index.html页面上.它检查登录状态并从响应对象获取签名的请求参数.

第二个块是php代码,用于解析签名请求,当你通过registratiton插件捕获它时,但当你提供url作为参数时,插件会自动将SR发送到这个页面.在画布应用程序中,我必须自己传递它.我怎么做?

JavaScript的

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {

    // the user is logged in and has authenticated your
    // app,and response.authResponse supplies
    // the user's ID,a valid access token,a signed
    // request,and the time the access token 
    // and signed request each expire

    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;    
    var signedRequest = response.authResponse.signedRequest;

  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook,} else {
    // the user isn't logged in to Facebook.
  }
});

PHP页面

<?php
define('FACEBOOK_APP_ID','3*****88&'); // Place your App Id here
define('FACEBOOK_SECRET','1345*****eb4f2da'); // 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;
    }
    // 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"];
$uID = $response["user_id"];

?>

解决方法

在 Clay’s Answer上扩展你可以用 jQuery.get()实现这样的东西:

var signed_request = getSignedRequest(),// however you do this..
    url = "http://yoursite.com/phppage.php",// wherever your php code is
    ajax = $.get(url,{signed_request: signed_request}); // initiate ajax call

// ajax success handler
ajax.done(function(ajaxResponse){ 
   console.log(ajaxResponse); 
});

当然,您需要在PHP文件中获取该值

$signed_request = $_GET['signed_request'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读