How to use Facebook query language to build applications

By

April 10, 2011FBML2 Comments

Facebook query language[FQL] is the SQL styled query language used to fetch the data from the graph api which we can tweak to build an application.

Before beginning you need to know the basics of graph api and facebook platform which can be explained by this article.

Facebook ‘s FQL is the easy way to fetch the information needed by your application.The query language is easily understandable since it resembles the SQL.
facebook-fql
We are going to create a facebook application to make the clear understanding about the facebook query language.The application is simple we will create a page which shows

  1. how many friends are still single.
  2. how many friends are Engaged
  3. Which city has your maximum number of friends

See how to setup the basic facebook application

Setup the facebook application by providing application name,canvas url etc.After setting up the server location start editing the “index.php” file

Declare variables


< ?php>
    
    include_once "src/facebook.php";

	$app_id = 'APP_ID';
	$application_secret = 'APP SECRET';
	
	$facebook = new Facebook(array(
	'appId'  => $app_id,
	'secret' => $application_secret,
	'cookie' => true, // enable optional cookie support
	));
?>

Get access token

In this application we are fetching the information from your friends hence you need to authorize this application before doing so.We need to get access token to do querying.

< ?php
if ($facebook->getSession()) 
	{

		$access_token = $facebook->getAccessToken();
	
	}
//if the current user haven't authorized this application we need to redirect
else {
	$loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=APP_ID
	&redirect_uri=APP_URL
	&scope=user_photos,user_birthday,friends_location,friends_relationships,offline_access,publish_stream"; 
	
	echo '';  
	
	}
?>

once the user allows the application to access the information we can now fetch the data using FQL.

Begin FQL query

< ?php
$me = $facebook->api('/me/friends');
	
	$friends = $facebook->api(array(
		'method' => 'fql.query',
		'query' => 'SELECT name,relationship_status,current_location
         FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
	));

?>

In our application we need to get details like “current location,relationship status” of all the friends.For the purpose of querying we also need “user ID” of all the friends.An userid is a unique number for every facebook user.

< ?php
'query' => 'SELECT name,relationship_status,current_location
         FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'

?>

Facebook has all the information in separate tables all we need is fql to query the tables.In our case we have to fetch “user” table to get the personal information and “Friend” table to get the user id’s.You have to learn more about other tables according to your own application see table information reference

The results of the query is stored in the variable[$friends] in a array format.All that you need is to process the data for you desired application needs.

Complete code

< ?php
    
    include_once "src/facebook.php";

	$app_id = 'APP_ID';
	$application_secret = 'APP SECRET';
	
	$facebook = new Facebook(array(
	'appId'  => $app_id,
	'secret' => $application_secret,
	'cookie' => true, // enable optional cookie support
	));

	if ($facebook->getSession()) 
	{

		$uid = $facebook->getUser();
	
		$access_token = $facebook->getAccessToken();
	//echo $access_token;
	}

	if ($facebook->getSession()) {
	
	$user = $facebook->getUser();
	$me = $facebook->api('/me/friends');
	
	$friends = $facebook->api(array(
		'method' => 'fql.query',
		'query' => 'SELECT name,relationship_status,current_location
         FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
	));
	$sc=0;
	$cc=0;
	$ec=0;
	$errc=0;
	$mc=0;
	$city=array();
	for($i=0;$iMax friends:".max($city)." in ".$maxcityname[0]."
Single ".$sc."
Commited: ".$cc."
Complicated: ".$cc."
Married: ".$mc."
Not shared ".$errc."
".sizeof($friends).""; } else { $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=APP_ID &redirect_uri=http://apps.facebook.com/naenbenda/ &scope=user_photos,user_birthday,email,friends_location,friends_birthday,friends_relationships,friends_work_history,friends_education_history,offline_access,publish_stream"; echo ''; } ?>

Write your Comment if you have any issues in creating FQL applications
Application DEMO

You can follow us on Twitter or join our Facebook Fan Page for more Facebook apps.

2 Responses to “How to use Facebook query language to build applications”
  1. Manoj Kabdal

    Is there any query to fetch offline friends of facebook in iphone app.
    Pls help me. I want it in my app.

  2. jimmy

    hi , i have tried out this code. It doesnt work. I suspect there is some problem around the for loop. Would you mind clarifying it?

Leave a Reply

*