Wednesday, October 10, 2012

How to use Facebook query language to build applications


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.
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
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
  1. < ?php>  
  2.       
  3.     include_once "src/facebook.php";  
  4.   
  5.     $app_id = 'APP_ID';  
  6.     $application_secret = 'APP SECRET';  
  7.       
  8.     $facebook = new Facebook(array(  
  9.     'appId'  => $app_id,  
  10.     'secret' => $application_secret,  
  11.     'cookie' => true, // enable optional cookie support  
  12.     ));  
  13. ?>  
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.
  1. < ?php  
  2. if ($facebook->getSession())   
  3.     {  
  4.   
  5.         $access_token = $facebook->getAccessToken();  
  6.       
  7.     }  
  8. //if the current user haven't authorized this application we need to redirect  
  9. else {  
  10.     $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=APP_ID 
  11.     &redirect_uri=APP_URL 
  12.     &scope=user_photos,user_birthday,friends_location,friends_relationships,offline_access,publish_stream";   
  13.       
  14.     echo '<fb:redirect url="' . $loginUrl . '">';    
  15.       
  16.     }  
  17. ?>  
  18. </fb:redirect>  
once the user allows the application to access the information we can now fetch the data using FQL.
Begin FQL query
  1. < ?php  
  2. $me = $facebook->api('/me/friends');  
  3.       
  4.     $friends = $facebook->api(array(  
  5.         'method' => 'fql.query',  
  6.         'query' => 'SELECT name,relationship_status,current_location 
  7.          FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'  
  8.     ));  
  9.   
  10. ?>  
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.
  1. < ?php  
  2. 'query' => 'SELECT name,relationship_status,current_location 
  3.          FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'  
  4.   
  5. ?>  
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
  1. < ?php  
  2.       
  3.     include_once "src/facebook.php";  
  4.   
  5.     $app_id = 'APP_ID';  
  6.     $application_secret = 'APP SECRET';  
  7.       
  8.     $facebook = new Facebook(array(  
  9.     'appId'  => $app_id,  
  10.     'secret' => $application_secret,  
  11.     'cookie' => true, // enable optional cookie support  
  12.     ));  
  13.   
  14.     if ($facebook->getSession())   
  15.     {  
  16.   
  17.         $uid = $facebook->getUser();  
  18.       
  19.         $access_token = $facebook->getAccessToken();  
  20.     //echo $access_token;  
  21.     }  
  22.   
  23.     if ($facebook->getSession()) {  
  24.       
  25.     $user = $facebook->getUser();  
  26.     $me = $facebook->api('/me/friends');  
  27.       
  28.     $friends = $facebook->api(array(  
  29.         'method' => 'fql.query',  
  30.         'query' => 'SELECT name,relationship_status,current_location 
  31.          FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'  
  32.     ));  
  33.     $sc=0;  
  34.     $cc=0;  
  35.     $ec=0;  
  36.     $errc=0;  
  37.     $mc=0;  
  38.     $city=array();  
  39.     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).""; 
  40.  
  41.     } 
  42.     else { 
  43.     $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=APP_ID  
  44.     &redirect_uri=http://apps.facebook.com/naenbenda/  
  45.     &scope=user_photos,user_birthday,email,friends_location,friends_birthday,friends_relationships,friends_work_history,friends_education_history,offline_access,publish_stream";  
  46.      
  47.     echo '<fb:redirect url="' . $loginUrl . '">';    
  48.       
  49.     }  
  50.   
  51.   
  52.   
  53. ?>  
  54. </fb:redirect></sizeof>  

1 comment: