gpt4 book ai didi

php - Facebook PHP 应用程序,如何检测它处于 Canvas 或 Page 模式?

转载 作者:可可西里 更新时间:2023-10-31 23:07:43 24 4
gpt4 key购买 nike

在iframe .php 应用中,如何检测自己是Page模式还是Canvas模式?谢谢!

最佳答案

阅读documentation :

  1. Facebook 将始终发送一个signed_request(用于 Canvas 和页面 url)
  2. 如果是页面,Facebook 会添加一个额外的参数,称为page

因此,基于此,您可以执行以下操作:

<?php
if( isset($_REQUEST['signed_request']) ) {
// We are in Canvas or Page now

// Let's extract the data from the signed_request
// to check if we are inside a Facebook Page
$app_secret = "APP_SECRET";
$data = parse_signed_request($_REQUEST["signed_request"], $app_secret);

if( isset($data["page"]) ) {
echo "Page";
} else {
echo "Canvas";
}
} else {
echo "None, or something went wrong!";
}

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 PHP 应用程序,如何检测它处于 Canvas 或 Page 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com