Facebook PHP SDK 示例无法运行
我一直在为 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 示例代码(来自 此处 ):
<?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 经验的人知道为什么吗?
I've been creating my first App for Facebook, and one of the code samples doesn't seem to be working. I've got the latest PHP SDK:
this php and html code below
My code:
<?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>
The Facebook Sample Code (From 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"]);
}
?>
My code just constantly refreshes the page when I run it. Anybody with FB PHP SDK experience know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
画布 url (redirect_uri) 应指向您托管应用程序的位置,而不是 facebook url。即您在 Facebook 开发人员设置中为画布添加的相同 URL!
而且,这一行:
应该没有下划线:
罪过你稍后检查
$data
的内容以查看用户是否经过身份验证。The canvas url (redirect_uri) should be pointing towards where you host the application, and not to the facebook url. I.e the same url that you have added in the facebook developer settings for the canvas!
And also, this line:
Should be without the underscore:
Sins you later check the content of
$data
to see if the user is authenticated.问题是您在设置变量时将其定义为
$_data
,但在使用empty()
检查时将其引用为$data
。由于这将导致它始终返回 true,因此您的代码将始终输出 JavaScript 重定向。我猜您之前也已经验证过该应用程序。这意味着一旦您被重定向到 Facebook,您就会立即被重定向回来(因为您已经登录)。这样就无限循环了。
注意:
$
后面不应该加下划线,否则可能会造成混淆使用 PHP 的特殊变量之一。
似乎很多流传的例子都不是最新的
他们的 SDK 版本。
The problem is that you have defined the variable as
$_data
when setting it but refereed to it as$data
when checking it withempty()
. Since this will cause it to always return true, your code will always output the JavaScript redirect.I'm guessing you've also previously authenticated the aplication. Which means once you are redirected to Facebook, you are instantly redirected back (since you have already logged in). Thus, infinite loop.
Note:
$
or it could be confusedwith one of PHP's special variables.
seems a lot of the examples floating around are not for the latest
version of their SDK.