php - 我如何找出哪个页面安装了我的 Facebook 应用程序/哪个页面正在加载我的应用程序

标签 php facebook facebook-graph-api facebook-php-sdk

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

我正在使用 PHP-SDK

最佳答案

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.

因此,捕获页面 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 "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, '-_', '+/'));
}

关于php - 我如何找出哪个页面安装了我的 Facebook 应用程序/哪个页面正在加载我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5587784/

相关文章:

Facebook 对象调试器 - 无法将主机名解析为有效的 IP 地址

facebook - 如何获取 Facebook 页面的事件?

facebook - as3-fb-api : how is it possible to have an accesstoken BEFORE Facebook. init()

facebook-graph-api - MVC 4 oAuth With Facebook 不返回电子邮件地址

php - 如何在 php 中获取 session ID 或用户名?

php - 为什么我应该在 Laravel 中使用事件和监听器

PHP/图形 API : Why my facebook session doesn't work's?

php - 如何检查数据库列中存在的字符串?

php - CodeIgniter:数据映射器 ORM delete() 一个关系;

地址栏中的Javascript,我该如何破译?