Reputation: 55
i'm trying to prepare an application for Facebook. application software is created. "localhost" tests without any problems but links within the Facebook application is not working... e.g. "page.php?page=test"... application, using the fan page. the first page is opened, UserID and PageID data is seamlessly but i click the link to the sample, the data is blank. i tried to target="_top" but not work...
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => false,
));
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
if(empty($_GET['sayfa'])){
...working here USERID and PAGEID...
}
if($_GET['sayfa'] == 'guncelle'){
...does not work here USERID and PAGEID...
}
i wonder what's wrong? thank you...
basic solved:
<?php
require 'facebook.php';
$signed_request = $_REQUEST["signed_request"];
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => false,
));
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
setcookie("facebook_user_id", $m2_facebook_kullanici_id);
setcookie("facebook_page_id", $m2_facebook_sayfa_id);
?>
<!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>Title</title>
</head>
<body>
<?php
if(empty($_GET['sayfa'])){
echo '<a href="index.php?sayfa=guncelle">Güncelle</a><br />';
echo '<h3>Home Page</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
if($_GET['sayfa'] == 'guncelle'){
$m2_facebook_kullanici_id = $_COOKIE["facebook_user_id"];
$m2_facebook_sayfa_id = $_COOKIE["facebook_page_id"];
echo '<a href="index.php">Back</a><br />';
echo '<h3>Settings Page</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
?>
</body>
</html>
Upvotes: 2
Views: 414
Reputation: 55
SOLUTION:
just add the following code to the end of links
?signed_request=' . $_REQUEST['signed_request'] . '
or
&signed_request=' . $_REQUEST['signed_request'] . '
a simple example of the application
<?php
require 'facebook.php';
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$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;
}
$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'], 'a5aaf146542d50c11f55e001c0fb8f31');
$m2_facebook_kullanici_id = $data['user_id'];
$m2_facebook_sayfa_id = $data['page']['id'];
?>
<!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>Title</title>
</head>
<body>
<?php
if(empty($_GET['sayfa'])){
echo '<a href="app_fan_page.php?sayfa=guncelle&signed_request='.$_REQUEST['signed_request'].'">Settings</a><br />';
echo '<h3>Home</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
if($_GET['sayfa'] == 'guncelle'){
echo '<a href="app_fan_page.php?signed_request=' . $_REQUEST['signed_request'] . '">Home</a><br />';
echo '<h3>Settings</h3>';
echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />';
echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id;
}
?>
</body>
</html>
special thank: moguzalp and Mesut Eyrice
Upvotes: 2