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 'Güncelle<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 'Back<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>
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 'Settings<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 'Home<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
Related
I have the following problem:
I'm trying to do my first application for Facebook and after Facebook connection and install the application not automatically redirects me to the url but outside of Facebook. This only happens the first time, once it has been installed if the url is recharged and stays within Facebook.
Any ideas to fix it?
I do not know if the problem is the code or application configuration:(
// PHP SDK v.3.0.0 de Facebook
require 'includes/facebook.php';
$facebook = new Facebook(array(
'appId' => '*******',
'secret' => '*************',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status'
));
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk 3.0.0</title>
<style typ="text/css">
html, body { width: 520px;}
</style>
</head>
<body>
<h1>php-sdk</h1>
<h3>PHP Session</h3>
<?php foreach($_SESSION as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
<h3>Tu</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Tus datos (/me)</h3>
<?php foreach($user_profile as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
</body>
</html>
You didn't specify a redirect_uri in your getLoginUrl method and hence the SDK constructed one for you that is based on your current URL.
So all you need to do is just set the canvas URL as your redirect_uri:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status',
'redirect_uri' => 'CANVAS_URL'
));
P.S: $user_profile and $me holds the same value, just use one of them instead of making two Graph API calls.
I have an app on facebook, I am using only pagetabs, everything is going all right. I click like, I allow application, request permissions, I allow, but now it comes back to page and there is an error, it reloads over and over... The page cannot load because browser reloads it always, I don't know how to fix it...
Thanks very much
Here is my code:
<?php
require_once "sdk/facebook.php";
$app_id = "MY_APPID";
$app_secret = "MY_SECRET";
$is_fan = false;
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get and decode signed request.
$signed_request = $facebook->getSignedRequest();
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode(
'.', $_REQUEST['signed_request'], 2
);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(
base64_decode(strtr($payload, '-_', '+/'), true)
);
$signed_request = $data;
}
else {
$signed_request = false;
}
// Determine if we have a fan request.
if($signed_request) {
if($signed_request->page->liked) {
$is_fan = true;
}
}
// for fans
if ($is_fan) { ?>
<!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" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script>
var oauth_url = 'https://www.facebook.com/dialog/oauth/';
oauth_url += '?client_id=414328901957674';
oauth_url += '&redirect_uri=' + encodeURIComponent('https://www.facebook.com/pages/null/167838393340757/?sk=app_414328901957674');
oauth_url += '&scope=user_birthday,user_likes,photo_upload,publish_stream,user_about_me,user_photos,user_hometown,user_location'
window.top.location = oauth_url;
</script>
</head>
<body background="images/fanda.jpg" style="overflow:hidden;"">
</body>
<?php }
// for non-fans
else { ?>
<!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" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body background="images/klikni-like.jpg" style="overflow:hidden;">
</body>
<?php } ?>
When someone is a fan your script is always doing:
window.top.location = oauth_url;
This will cause the page to reload infinitely if someone is a fan.
I have found the solution to your problem. Update this when you check if($is_fan):
$user_id = $facebook->getUser();
if ($is_fan) {
if($user_id)
{
code after authentication and page Liked
}
}
else {
<script>
var oauth_url = 'https://www.facebook.com/dialog/oauth/';
................ Next 4 lines same as the above code ..............
</script>
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP SCRIPT issue with passing the uploaded image to facebook->api function
I have this code and its working awsomely fine just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo
echo 'Only jpg, png and gif image types are supported!';
i even remove the check on image type and try to get the image from
$img = realpath($_FILES["pic"]["tmp_name"]);
but $img gets nothing in it and uploads a by default empty image on facebook as my page
Kindly check my following code and let me know what should is wrong with my code so i'll become able to upload images
<?
require 'src/facebook.php';
$app_id = "364900470214655";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 194458563914948){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
if (isset($_FILES) && !empty($_FILES))
{
if( !in_array($_FILES['pic']['type'], $valid_files ) )
{
echo 'Only jpg, png and gif image types are supported!';
}
else{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => '#' . $img,
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/194458563914948/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
}
}
?>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
Creating a New Album and Adding a Photo
This is the scenario where you upload a photo to the ALBUM_ID/photos Graph API endpoint. The user interface for this example allows the user to select a photo and add a caption before submitting the new photo.
Using PHP:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
?>
<?php
define('YOUR_APP_ID', 'x');
define('YOUR_APP_SECRET', 'x');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$url = 'https://graph.facebook.com/me?access_token=' . $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$user = json_decode($response);
print_r($user);
?>
<html>
<body>
<?php if ($cookie) { ?>
Welcome <?php ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
Does anyone know why I am getting this exception? print_r of $user is yielding
stdClass Object ( [error] => stdClass Object ( [type] => OAuthException [message] => An active access token must be used to query information about the current user. ) )
Why is this happening?
well for one thing $access_token isn't set before it's used?
<?php
include_once 'config.inc.php';
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(174346302604624, be273568bf3e3b6c194fdcc09448201d);
//catch the exception that gets thrown if the cookie has an invalid session_key in it
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));
?>
//above is MY facebook script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
above the code is in my head
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js"
type="text/javascript"></script>
above code is in body tag
<div><fb:profile-pic uid='<?php echo $user->id; ?>' ></fb:profile-pic></div>
<?php } ?>
<div id="fb-root"></div>
<script>
FB.init({appId: '<?= 174346302604624 ?>',status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login',function(response) {
window.location.reload();
});
</script>
above code is just before </body> tag can any body tell me what is fault,i want to show profile pic
for fbml tags in iframe app you can try this one
<fb:serverFbml>
<script type="text/fbml">
<fb:fbml>
<div><fb:profile-pic uid=""></fb:profile-pic></div>
</fb:fbml>
</script>
</fb:serverFbml>