How to get facebook total likes for a facebook application? - facebook

I know you can get the total fan count for a facebook page via the facebook graph api. But I cannot seem to be able to get the total likes (or fan count) of a facebook application. Take for example, graph.facebook.com/castleage, notice that the fan count is missing...any clues on how to do so? Thanks.

Try luck with something like this:
$data = array();
try {
$data = $facebook->api(array(
'query' => 'SELECT uid FROM page_fan WHERE page_id=' . $fanpageId,
'method' => 'fql.query'
));
} catch(Exception $e) {
print_r($e);
}
$fanCount = count($data);
print_r($fanCount);

Related

upload photos got by fql query on my server

Hello guys i'm trying to understand how to upload the image from facebook.
function for do fql query. Is useful for future occasions.
<?php
// connection facebook api key...
function fb_fql($fql) {
$this->ci =& get_instance();
// Get User Details
$this->ci->load->model('facebook_model');
$this->ci->load->library('user');
// $fb_id = $this->ci->user->get_facebook_id($this->ci->session->userdata('user_id'));
// User is found
// Get Facebook User profile
$user_profile = $this->ci->facebook->getUser();
$result = false;
if ($user_profile) { // if profile exist
try{
$accessToken = $this->ci->facebook->getAccessToken();
$fb_id = $this->ci->facebook->api('/me?access_token='.$accessToken);
if( isset($user_profile) )
{
$fql_run = array(
'method' => 'fql.query',
'query' => $fql,
);
$result = $this->ci->facebook->api($fql_run); // run fql
} else { $result = false; }
}catch(FacebookApiException $e){
$result = null;
}
}
return $result;
}
?>
Here i get the photos by this query and the result of the array show correctly 10 photos of facebook. Now i have to upload it on my server but how can i do that?
<?php
// fql query
$fql = "SELECT pid
FROM photo
WHERE aid IN (
SELECT aid
FROM album
WHERE owner=me() LIMIT 10
) ";
// run query
$photos = $this->facebook_action->fb_fql($fql);
if ($photos != false) {
foreach ($photos as $photo) {
echo print_r($photos); // show the array with the data taken
// here i would like upload the photos got from the query.
}
} else {
echo "no photo";
}
?>
You will need to get the src or src_big field from the photo table and then download this using CURL, file_put_contents or similar. There are quite a few StackOverflow answers for grabbing an image and saving it to a file - e.g Saving image from PHP URL
SELECT src_big FROM photo
WHERE aid IN (SELECT aid FROM album WHERE owner=me() LIMIT 10)
Since you will be downloading user's data to your own server, I would advise you to check that your application does not violate the Facebook Platform Policy.

Why is no. of facebook friends I see through Graph API unequal to no. of friends displayed on my profile?

I am using the following code to get list of friends in my friend list on Facebook :
//get the user's access token
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions',
'GET',
array(
'access_token' => $access_token
)
);
$frnds= $facebook->api('/me/friends');
echo sizeof($frnds['data']).'<br>';
foreach($frnds['data'] as $frn)
{
echo $frn['name'].'<br>';
}
The no. of friends i.e. sizeof($frnds['data']) comes out to be less then the friends actually displayed on my facebook profile. I wonder WHY?

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 signed_request returns NULL

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

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