How to use Facebook query language to build applications
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.
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
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
- how many friends are still single.
- how many friends are Engaged
- Which city has your maximum number of friends
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 '<fb:redirect url="' . $loginUrl . '">';
- }
- ?>
- </fb:redirect>
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;$i<sizeof ($friends);$i++)="" {="" if($friends[$i]['current_location']['city']="=""){}" else="" array_push($city,$friends[$i]['current_location']['city']);="" if($friends[$i]['relationship_status']="="Single")" $sc++;="" }="" $ec++;="" complicated")="" $cc++;="" $mc++;="" $errc++;="" $city="array_count_values($city);" $maxcityname="array_keys($city,max($city));" echo="" "<font="" size="\"22px\"">Max friends:".max($city)." in ".$maxcityname[0]."<br>Single ".$sc."<br> Commited: ".$cc." <br> Complicated: ".$cc."<br> Married: ".$mc."<br> Not shared ".$errc."<br>".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 '<fb:redirect url="' . $loginUrl . '">';
- }
- ?>
- </fb:redirect></sizeof>
Nice Article......see this also for start up with SSl....Facebook App with free SSl--part 2
ReplyDeleteFacebook App with free SSl--part 1