問題描述
我有一個畫布應(yīng)用程序 (http://apps.facebook.com/myapp),其他頁面(企業(yè)等)可以將它添加到他們的頁面.在我的應(yīng)用中,我如何知道我是從哪個頁面被調(diào)用的?
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?
我使用的是 PHP-SDK
I'm using the PHP-SDK
推薦答案
如 Facebook 頁面中所述標(biāo)簽教程:
當(dāng)用戶導(dǎo)航到 Facebook 時頁面,他們會看到你的頁面標(biāo)簽添加在下一個可用選項(xiàng)卡中位置.從廣義上講,頁面選項(xiàng)卡是以完全相同的方式加載畫布頁面.當(dāng)用戶選擇您的頁面選項(xiàng)卡,您將收到signed_request
參數(shù)為 1附加參數(shù),page
.這參數(shù)包含一個 JSON 對象一個 id(當(dāng)前的頁面 id頁面),管理員(如果用戶是管理員頁面),并喜歡(如果用戶已喜歡該頁面).和畫布一樣頁面,您將不會收到所有您可以訪問的用戶信息應(yīng)用程序在signed_request 中,直到用戶授權(quán)您的應(yīng)用程序.
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.
因此,捕獲頁面 ID 的一種方法是:
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'];
}
?>
或者沒有 PHP-SDK 的解決方案:
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, '-_', '+/'));
}
這篇關(guān)于如何找出哪個主頁安裝了我的 Facebook 應(yīng)用程序/哪個頁面正在加載我的應(yīng)用程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!