开发者

Referring page to app

I have an application added to several fan pages. Ideally, the application should work custom depending on the referring page. How can I detect which page referred to t开发者_运维百科he app.

Developing a Facebook Iframe app, Using PHP.

(Question posted on Facebook's dev forum as well: http://forum.developers.facebook.net/viewtopic.php?id=108409)

Thx, Oren.


As explained in the Page Tab Tutorial

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.


With the http referer, you will have the the Facebook proxy url.

In your case, I think you have to use the id of the page (passed in the signed request).


The following PHP snippet will output the signed_request received on the page tab. You will find the page ID needed in your case.

<?php
$appsecret = 'Your App Secret';

$signed_request = $_REQUEST['signed_request'];
$request = $_REQUEST;

$signed_request = parse_signed_request($signed_request, $appsecret);

print_r($signed_request);

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, '-_', '+/'));
}
?>


Using the new php-sdk, there is a quicker way to find out the referring page. $facebook->getSignedRequest() will return an array with the signed request, authorization token, page and user basic info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜