[英]How can I find out what Page has installed my Facebook App / which page is loading my app
I have a canvas app (http://apps.facebook.com/myapp) and other pages (businesses, etc) can Add it to their page. 我有一个画布应用程序(http://apps.facebook.com/myapp)和其他页面(企业等)可以将其添加到他们的页面。 Within my app, how can I know which page I'm being called from? 在我的应用程序中,我怎么知道从哪个页面调用?
I'm using the PHP-SDK 我正在使用PHP-SDK
As documented in Facebook Page Tab Tutorial : 如Facebook页面选项卡教程中所述 :
When a user navigates to the Facebook Page, they will see your Page Tab added in the next available tab position. 当用户导航到Facebook页面时,他们会在下一个可用的标签位置看到您的页面标签。 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
. 当用户选择您的页面选项卡时,您将收到带有一个附加参数page
的signed_request
参数。 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). 此参数包含一个带有id(当前页面的页面ID)的jSON对象 ,admin(如果用户是该页面的管理员),并且喜欢(如果用户喜欢该页面)。 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. 与Canvas页面一样,在用户授权您的应用之前,您不会在signed_request中收到应用可访问的所有用户信息。
So one way to capture the page id would be: 因此,捕获页面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'];
}
?>
OR a solution without the PHP-SDK: 或者没有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, '-_', '+/'));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.