我一直在创建我的第一个Facebook应用程序,其中一个代码示例似乎不起作用。我有最新的PHP SDK: 这个php和html代码在下面
我的代码:
<?php
require("facebook-php-sdk/src/facebook.php");
$app_id = "257964450938655";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode("http://apps.facebook.com/orangepicker/") . "&scope=email";
$signed_request = $_REQUEST["signed_request"];
list($_encoded_sig, $_payload) = explode('.', $signed_request, 2);
$_data = json_decode(base64_decode(strtr($_payload, '-_', '+/')), true);
?>
<!DOCTYPE html>
<html lang="en" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset="utf-8" />
<title>Orange Picker</title>
</head>
<body>
<?php if (empty($data["user_id"])):
echo("<script>top.location.href='" . $auth_url . "'</script>");
echo("<div align=\"center\"><a href=\"" . $auth_url . "\" target=\"blank\">Authorize This Application</a></div>");
echo($data["user_id"]);?>
<?php else: ?>
<noscript><div align="center">You need JavaScript enabled to use this app.</div></noscript>
<div align="center"><img src="Resources/tree.png" usemap="#treemap" /></div>
<map name="treemap">
<area shape="circle" coords="22,29,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="37,26,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="55,22,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="75,14,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="93,16,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="112,13,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="128,12,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="146,17,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="166,26,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="182,33,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="194,42,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="208,57,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="222,70,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="234,88,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="236,110,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="223,123,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="208,134,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="193,146,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="177,155,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="161,165,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="144,171,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="125,171,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="106,166,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="84,162,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="70,158,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="55,151,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="42,136,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="29,123,10" onclick="OrangeClick()" alt="Orange" />
<area shape="circle" coords="20,107,10" onclick="OrangeClick()" alt="Orange" />
</map>
<div align="center" style="font-size:large;"><span id="waitTime"></span> <span id="TotalOranges"></span> <span id="TotalCarts"></span></div>
<script type="text/javascript">
function OrangeClick() {
}
</script>
<div align="center">Copyright© Neil Flodin <?php echo date("Y"); ?></div>
<?php endif; ?>
</body>
</html>
Facebook示例代码(来自here):
<?php
$app_id = "YOUR_APP_ID";
$canvas_page = "YOUR_CANVAS_PAGE_URL";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_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["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data["user_id"]);
}
?>
我的代码只是在运行时不断刷新页面。任何有FB PHP SDK经验的人都知道为什么?
答案 0 :(得分:1)
画布网址(redirect_uri)应指向您托管应用程序的位置,而不是指向facebook网址。即您在画布的facebook开发人员设置中添加的相同网址!
而且,这一行:
$_data = json_decode(base64_decode(strtr($_payload, '-_', '+/')), true);
应该没有下划线:
$data = json_decode(base64_decode(strtr($_payload, '-_', '+/')), true);
稍后您会检查$data
的内容,看看用户是否经过身份验证。
答案 1 :(得分:1)
问题是您在设置变量时已将变量定义为$_data
,但在使用$data
进行检查时将其称为empty()
。由于这将导致它始终返回true,因此您的代码将始终输出JavaScript重定向。
我猜你以前也验证了这个应用程序。这意味着一旦您被重定向到Facebook,您立即被重定向回来(因为您已经登录)。因此,无限循环。
注意:
$
之后加下划线,否则可能会混淆
使用PHP的一个特殊变量。