简体   繁体   English

如何找出我的Facebook App /哪个页面正在加载我的应用程序的页面

[英]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 . 当用户选择您的页面选项卡时,您将收到带有一个附加参数pagesigned_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.

相关问题 如何确定用户是否喜欢我的 facebook 应用程序? (不是粉丝专页) - How do i find out if a user has liked my facebook app? (not a fan page) 我如何允许人们订阅我的 Facebook 应用以访问他们的主页? - How can I allow people to subscribe to my Facebook app to their Page? 在Facebook应用中发布为我的页面? - Post in a facebook app as my page? 如何检测Facebook应用程序正在加载页面以更改CSS? - How do I detect that facebook app is loading page to change CSS? 如何将我的网站变成单个页面的应用程序? - How can I turn my website into a single page app like? 如何获取我的页面的facebook page_id? - how can i get facebook page_id for my page? PHP SDK-如何知道Facebook上的人是否在Facebook上安装并授权了我的应用程序? - PHP SDK-How to know whether or not person from Facebook has installed and authorized my app on Facebook? 即使用户不在我的应用程序网页上,如何将其发布到用户的Facebook墙 - How to post to users' facebook wall even if they are not on my app web page Facebook App画布页面未加载 - Facebook App canvas Page not loading 如何获取应用程序的访问令牌,以便可以将页面发布为页面? - How can I get the access token of my app so I can post on a page as a page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM