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?
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
$datato see if the user is authenticated.