我正在尝试向喜欢我的网页的用户展示不同的页面, 我正在使用此代码,但似乎没有工作,任何人都可以帮忙吗?
基本上它应该将主index.html显示为登录页面,然后当人们点击“赞”按钮时,它会显示fans_only.html页面以欢迎用户喜欢该页面。
<?php
require_once 'facebook/facebook.php';
/*
* "appId" and "secret" values get from your Facebook application settings
*/
$config = array (
'appId' => 'xxx',
'secret' => 'xxx'
);
$facebook = new Facebook ($config);
{
$signed_request = $facebook->getSignedRequest();
if ($signed_request)
{
if (isset ($signed_request ['page'] ['liked']) && $signed_request ['page'] ['liked'])
{
//Place your secret content here
//echo '<p>This is a secret content.</p>';
require ("fans_only.html");
exit;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>intro</title>
<!--Adobe Edge Runtime-->
<script type="text/javascript" src="edge_includes/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="edge_includes/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="edge_includes/edge.0.1.3.min.js"></script>
<script type="text/javascript" src="edge_includes/edge.symbol.0.1.3.min.js"></script>
<script type="text/javascript" charset="utf-8" src="index_edge.js"></script>
<script type="text/javascript" charset="utf-8" src="index_edgeActions.js"></script>
<link rel="stylesheet" href="index_edge.css" />
<!--Adobe Edge Runtime End-->
<style type="text/css">
body {
background:url(images/preloader.gif) 255px 10px no-repeat #fff;
}
</style>
</head>
<body style="margin:0;padding:0;">
<div id="stage" class="EDGE-22385971">
</div>
</body>
</html>
答案 0 :(得分:1)
试试这段代码:
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
$signed_request = $_REQUEST["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, '-_', '+/'));
}
$data = parse_signed_request($_REQUEST["signed_request"], "APP_SECRET");
if ($data['page']['liked']){
//SHOW DATA FAN
}else{
//SHOW DATA NO FAN
}
答案 1 :(得分:0)
<?php
require_once './facebooksdk/src/facebook.php'; //download at https://github.com/facebook/php-sdk/downloads
$facebook = new Facebook(array(
'appId' => '174968482618267', // enter your App's ID
'secret' => '5710d00a9b4acec8849d8adeb08c15c4', // enter your App's Secret
'cookie' => true,
));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome Page</title>
</head>
<body>
<?php
// Did they like a page?
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["page"]["liked"])) {
// DISPLAY TO: those who didn't LIKE the page
?>
<!-- Didn't Like -->
You don't "Like" our page :-(
<!-- End of Didn't Like -->
<?
} else {
// DISPLAY TO: those who LIKED the page
?>
<!-- Liked -->
Thanks for "Liking" our page!
<!-- End of Liked -->
<? } ?>
<!-- follow @svolinsky on twitter -->
</body>
</html>