Facebook signed_request returns NULL - facebook

I have create new iframe app on facebook and want to detect whether the user is a fan or not of the current page so I use the $_REQUEST['signed_request'] variable as described here: http://developers.facebook.com/docs/authentication/signed_request/. However $_REQUEST['signed_request'] variable always returns NULL, not what it should actually return. Has anyone ever encountered the same problem? I'm quite familiar with creating apps on facebook but can't figure out what could be the reason of this problem... Any ideas?

Make sure that in your Page tab url you specify the name of the page for example http://yourdomain.com/myapp/index.php instead of just http://yourdomain.com/myapp
The signed request only seems to get posted if you specify the exact page

You can use Facebook Query Language (FQL) to check if the current user is a Fan of your page.
function userIsFan()
{
global $page_id, $facebook, $access_token;
$fql = "SELECT uid from page_fan WHERE uid=me() AND page_id='".$page_id."'";
$param = array('method' => 'fql.query',
'access_token' => $access_token,
'query' => $fql,
'callback' => '');
$response = $facebook->api($param);
if (isset($response[0]))
{
return true;
}
return false;
}
Things to take note of...
me() is a method that automatically gets the user id of the current user.$page_id should be containing your Facebook Page id that you have specified in fbmain.php

Related

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Working with facebook Like button and FSQL

I have develop Facebook application and added page tab on my page.
I have read Facebook documentation for canvas application development and adding application on page tab.
I want to show different page for people who click on Like button on my page and different page to people who not click on like button.
After reading documentation I found that it is possible using FSQL but I am not getting any result from my sql queries
https://developers.facebook.com/docs/reference/fql/url_like/
https://developers.facebook.com/docs/reference/fql/
require_once 'facebook.php';
// *** Add your Facebook API Key, Secret Key here ***
$appapikey = '*********';
$appsecret = '**********';
$facebook = new Facebook($appapikey, $appsecret);
// $user_id = $facebook->require_login();
//$fql = "select name from user where uid=me()";
//$fql = "SELECT user_id FROM like WHERE object_id='122706168308'";
$fql = "SELECT user_id, object_id, post_id FROM like WHERE user_id=me()";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
echo"hi";
echo"<pre>";
print_r($fqlResult);
echo"</pre>";
Any idea how to make it working?
Thanks.
did you authorized app at first ?
PS: its not FSQL its fql i mean you are not able to use all sql commands in fql

Facebook stream publish to wall as App/Page

i wonder if it is possible to post to a users stream/wall by using stream_publish as a page or an app. Currently I've granted my app to stream_publish on my own wall. But when I post to my stream from the app the post is displayed like I would've posted it by myself instead of having the app posted it to my wall.
I want to have the post looks like the app posted it. I already used the uid-parameter in the stream_publish params and set it to the appId ... but the post still looks like an own post.
my code:
$postData = array( $feed_dir = '/'.$fb_uid.'/feed/',
$method = 'POST',
$msg_body = array( 'access_token' => $arrUserData['access_token'],
'message' => 'here goes the message',
'name' => '-name-',
'link' => 'http://some-url',
'description' => '-description-',
'actions' => ($actions),
'uid' => $iniHandler->getIniSetting('facebook.app.id')
)
);
try {
$res = $facebook->api($feed_dir, $method, $msg_body);
}
catch (Exception $e) {
$err_str = $e->getMessage();
}
Or is ther another way to post to a wall and let the post looks like a post by an app? My users can sign up for issues in my app and my app posts some news to the users wall ... thats my aim.
You also need manage_pages permissions. Once you have those, HTTP Get me/accounts and that will be a list of pages that the logged in user is admin of. In that list will be a unique page access_token. Use that token to http post a new wall/stream item.

Facebook iFrame app - how to fetch Preload FQL result using PHP SDK?

since few years I have an FBML app (a small Flash game) which I'm now trying to convert to an iFrame app. Unfortunately there aren't many docs for Facebook iFrame apps yet.
For my game I need the user's first name, picture, gender and the city.
In my old version I had this preload FQL (created once by a PHP script):
$fql = array('info' => array('pattern' => 'facebook',
'query' => 'SELECT first_name, sex, pic_big, current_location
FROM user WHERE uid={*user*}'));
$fb->api_client->admin_setAppProperties(
array('preload_fql' => json_encode($fql)));
and then my FBML app script had been as simple as:
<?php
require_once('facebook.php');
define('FB_API_ID', 'XXX');
define('FB_AUTH_SECRET', 'YYY');
$fb = new Facebook(FB_API_ID, FB_AUTH_SECRET);
$viewer_id = $fb->require_login();
$data = json_decode($fb->fb_params['info'], true);
$first_name = $data[0][0];
$last_name = $data[0][2];
$female = ($data[0][3] != 'male');
$avatar = $data[0][3];
$city = $data[0][4]['city'];
# and then I'd just construct flashvars attribute
# for the <fb:swf ...> tag and print it
?>
Does anybody please have hints on how to recreate the same script for the iFrame version - i.e. how can I fetch the result of Preload FQL by my iFrame app?
According to an older Facebook blog entry Preload FQL should be accessible by the iFrame apps.
Thank you!
Alex
My own answer after long searching is that Preload FQL results aren't sent to iframe Facebook apps.
That is why Facebook performance doc says:
"Preload FQL Query and Multiquery.
This section applies to FBML canvas pages, but not to websites or IFrame canvas pages."
As Facebook said for Preload FQL
"Facebook will send the result of these FQL queries as JSON-encoded POST parameters to your Canvas URL"
print_r your $_POST and see which variable is the "json-encoded results". You convert json into php object using json_decode
JSON looks like this: {"var":"val","var":"val"}
Also, Facebook already has great docs for iframes. Then you might have missed these great docs:
Facebook Docs Home
http://developers.facebook.com/docs/
Authentication
http://developers.facebook.com/docs/authentication/
Signed Request
http://developers.facebook.com/docs/authentication/signed_request/
iFrame Canvas Apps
http://developers.facebook.com/docs/guides/canvas/
PHP SDK
https://github.com/facebook/php-sdk
Graph API
http://developers.facebook.com/docs/reference/api/
You don't need to call any FQL for the information you are getting. For iFrame you just need to do following steps
Download the PHP SDK of graph api https://github.com/facebook/php-sdk/
Create the object and authorize the app from user
$fbconfig['appid' ] = "your application id";
$fbconfig['api' ] = "your application api key";
$fbconfig['secret'] = "your application secret key";
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
// User location extended permission allow you to get user's current location
$loginparams = array('canvas' => 1,'fbconnect' => 0,'req_perms' => 'user_location');
$loginUrl = $facebook->getLoginUrl($loginparams);
// We may or may not have this data based on a $_GET or $_COOKIE based session.
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
d($e);
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
// You can found all the data in this array.
print_r($fbme);
For more detail you can follow this tutorial http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/
Hope it works for you

Check if user is a fan of my Facebook app

How can I check thought the FB Javascript API if the current user is a fan of my app? I need to check this from within the page inserted into the FB's canvas IFRAME, throught the JS SDK...so that I can call a method of a local Flash, and do "something special" for that user.
In PHP, you can achieve this by using their PHP-SDK.
First you're gonna have to query their FQL database. Use the ff query (replace UID with the user's Id and PAGE_ID with your application's Id).
'SELECT uid, page_id FROM page_fan WHERE uid=UID AND page_id=PAGE_ID'
So your code might look like this:
$client = new Facebook(YOUR_PARAMS_HERE);
$result = $client->api(array(
'method' => 'fql.query',
'query' => 'SELECT uid, page_id, type FROM page_fan WHERE uid=1234 AND page_id=4321',
));
This returns an array similar to the ff:
array(
[0] = array(
[uid] = 1234
[page_id] = 4321
[type] = APPLICATION
)
)
FQL page_fan details here.