我如何找到什么页面已经安装了我的Facebook应用程序/哪个页面正在加载我的应用程序 [英] How can I find out what Page has installed my Facebook App / which page is loading my app

查看:132
本文介绍了我如何找到什么页面已经安装了我的Facebook应用程序/哪个页面正在加载我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个画布应用程序(http://apps.facebook.com/myapp)和其他页面(企业等)可以将其添加到他们的页面。在我的应用程序中,我如何知道我正在调用哪个页面?



我正在使用PHP-SDK

解决方案

Facebook页面选项卡教程


当用户导航到Facebook
页面时,他们将看到您的页面Tab
添加到下一个可用的选项卡
位置。一般来说,页面选项卡的
的加载方式与
Canvas页面完全相同。 当用户选择
Page Tab时,您将收到
signed_request 参数,其中一个
附加参数。此
参数包含一个JSON对象,
一个id(当前
页面的页面ID)
,admin(如果用户是该页面的admin
),并喜欢(如果用户
喜欢的页面)。与Canvas
页面一样,在
用户授权您的应用程序之前,您将不会在signed_request中收到
应用程序的所有
用户信息。


所以捕捉页面ID的一种方法是:

 <?php 
// PATH TO FB-PHP-SDK
require'../../src/facebook.php';

$ facebook = new Facebook(array(
'appId'=>'APP_ID',
'secret'=>'APP_SECRET',
'cookie '=> true,
));
$ signed_request = $ facebook-> getSignedRequest();
if($ page = $ signed_request ['page']){
echo $ page ['id'];
}
?>

或没有PHP-SDK的解决方案:

 <?php 
if(!empty($ _ REQUEST [signed_request])){
$ app_secret =APP_SECRET;
$ data = parse_signed_request($ _ REQUEST [signed_request],$ app_secret);

if(isset($ data [page])){
echo $ data [page] [id];
} else {
echo不在页面;
}
}

函数parse_signed_request($ signed_request,$ secret){
list($ encoded_sig,$ payload)= explode('。',$ signed_request, 2);

//解码数据
$ sig = base64_url_decode($ encoded_sig);
$ data = json_decode(base64_url_decode($ payload),true);

if(strtoupper($ data ['algorithm'])!=='HMAC-SHA256'){
error_log('未知算法,预期的HMAC-SHA256');
返回null;
}

//检查sig
$ expected_sig = hash_hmac('sha256',$ payload,$ secret,$ raw = true);
if($ sig!== $ expected_sig){
error_log('Bad Signed JSON signature!');
返回null;
}

return $ data;
}

函数base64_url_decode($ input){
返回base64_decode(strtr($ input,'-_','+ /'));
}


I have a canvas app (http://apps.facebook.com/myapp) and other pages (businesses, etc) can Add it to their page. Within my app, how can I know which page I'm being called from?

I'm using the PHP-SDK

解决方案

As documented in Facebook Page Tab Tutorial:

When a user navigates to the Facebook Page, they will see your Page Tab added in the next available tab position. Broadly, a Page Tab is loaded in exactly the same way as a Canvas Page. When a user selects your Page Tab, you will received the signed_request parameter with one additional parameter, page. This parameter contains a JSON object with an id (the page id of the current page), admin (if the user is a admin of the page), and liked (if the user has liked the page). As with a Canvas Page, you will not receive all the user information accessible to your app in the signed_request until the user authorizes your app.

So one way to capture the page id would be:

<?php
// PATH TO FB-PHP-SDK
require '../../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
if( $page = $signed_request['page'] ) {
    echo $page['id'];
}
?>

OR a solution without the PHP-SDK:

<?php
if(!empty($_REQUEST["signed_request"])) {
    $app_secret = "APP_SECRET";
    $data = parse_signed_request($_REQUEST["signed_request"], $app_secret);

    if (isset($data["page"])) {
        echo $data["page"]["id"];
    } else {
        echo "Not in a page";
    }
}

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, '-_', '+/'));
}

这篇关于我如何找到什么页面已经安装了我的Facebook应用程序/哪个页面正在加载我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆