问题描述
我有一个画布应用程序 (http://apps.facebook.com/myapp),其他页面(企业等)可以将它添加到他们的页面.在我的应用中,我如何知道我是从哪个页面被调用的?
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 页面中所述标签教程:
当用户导航到 Facebook 时页面,他们会看到你的页面标签添加在下一个可用选项卡中位置.从广义上讲,页面选项卡是以完全相同的方式加载画布页面.当用户选择您的页面选项卡,您将收到signed_request 参数为 1附加参数,page.这参数包含一个 JSON 对象一个 id(当前的页面 id页面),管理员(如果用户是管理员页面),并喜欢(如果用户已喜欢该页面).和画布一样页面,您将不会收到所有您可以访问的用户信息应用程序在signed_request 中,直到用户授权您的应用程序.
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), ***** (if the user is a ***** 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, '-_', '+/')); }