Introduction
The Facebook through FQL, enables you to query data from the Graph API using the near to SQL style known. In this article we will learn how to perform, prepare and test queries on the Facebook (FQL) platform using the SDK for PHP or Javascript.
Let’s quote some pretty interesting examples of FQL as well as how to use the tool support for developers called Graph API Explorer provided by facebook to develop and test FQL queries
We will also learn about Leadpages Alternatives, more deeply into online ad campaigns which are being directed to the landing pages the web visitors could arrive. Landing pages can be used with Facebook Lead ads as well. It may sound confusing but there are easy alternative ways on how to do it. See for Best Leadpages Alternatives.
Running FQL with JAVASCRIPT and PHP SDK
In this topic we will learn how to perform an FQL both through the PHP SDK and the JAVASCRIPT SDK . As support, we use the following FQL query:
SELECT FROM user WHERE uid = FRIEND_COUNT me ()
(Simply query the amount of friends you have facebook)
Running through PHP:
Example of how to run FQL through the PHP SDK:
1 |
$result = $facebook ->api( array ( |
3 |
"method" => "fql.query" , |
5 |
"query" => " SELECT friend_count FROM user WHERE uid =me()" , |
11 |
echo "Number of friends: {$result [0]['friend_count']}" ; |
See Full Functional PHP Example Below:
Complete working example: (Just only parameterize the appId and secret)
3 |
require 'facebook-php-sdk-master/src/facebook.php' ; |
7 |
$facebook = new Facebook( array ( |
9 |
'appId' => '236076976577688' , |
11 |
'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6' , |
17 |
$user = $facebook ->getUser(); |
25 |
$user_profile = $facebook ->api( '/me' ); |
27 |
$result = $facebook ->api( array ( |
29 |
"method" => "fql.query" , |
31 |
"query" => "SELECT friend_count FROM user WHERE uid =me()" , |
37 |
echo "Number of friends: {$result[0]['friend_count']}" ; |
41 |
} catch(FacebookApiException $e ) { |
55 |
$loginUrl = $facebook ->getLoginUrl(); |
57 |
header ( "Location:$loginUrl" ); |
Output: Number of friends: 1000 (Your total friends)
Running through Javascript:
Example of how to run FQL via Javascript SDK:
7 |
query: "SELECT friend_count FROM user WHERE uid =me()" |
13 |
alert (resp[0][friend_count]); |
See Full Functional JAVASCRIPT Example Below:
9 |
<title>Facebook API</title> |
11 |
<script src= "http://connect.facebook.net/pt_BR/all.js" ></script> |
13 |
<script src= "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> |
19 |
appId : '236076976577688' , |
21 |
status: true , cookie: false , xfbml: false |
25 |
var FBCountFriends = function () { |
31 |
query: 'SELECT friend_count FROM user WHERE uid =me()' |
37 |
alert( "Your Total Friends " +resp[0].friend_count); |
47 |
jQuery(document).ready( function () { |
49 |
FB.getLoginStatus( function (response) { |
51 |
if (response.status === 'connected' ) { |
57 |
FB.login( function (response) { |
59 |
if (response.authResponse) { |
Output: Your Total Friends: 1000
Testing FQL queries using the Graph API Explorer
Facebook provides a tool to support developers very helpful, called Graph API Explorer . Through it you can test and develop their FQLs. Through this tool you can enable / disable permissions online form during testing of FQL. Recalling that access to some information need user permission so that your application can access them.
To test / debug their FQLs, open the Graph Explorer and click the FQL Query . Before inserting / draw your FQL, click the Get Access Token and select the permissions your app needs to request that your FQL reach the goal of getting the information you need.
http://developers.facebook.com/tools/explorer
Then select the desired permissions. See the figure below, we choose you to perform some interesting some nice permissions tests: (mail, user_likes, user_photos, user_about_me, user_birthday, user_games_activity).
Remember that access to some information need certain permissions from the user. For example, to access the photos and albums published by the user, you will need it to approve permissionuser_photos .
For a complete reference on the possible permissions, access the link:http://developers.facebook.com/docs/reference/login/
After clicking the Get Access Token you’re already able to test their FQLs. See example below:
Interesting examples of FQL
Selected below some examples of FQL so you can get an idea of what is possible. Depending on the information it needs, advise you to consult the link that contains the reference of tables to query the Graph API or FQL ready to look now by clicking on google. Let’s look at examples:
- Select all the user’s friends who are using your application:
1 |
SELECT uid, name FROM user WHERE uid IN ( SELECT UID2 FROM friend WHERE uid1 = me ()) AND is_app_user = 1 |
- Checking if certain user likes fan page:
1 |
SELECT uid FROM page_fan WHERE page_id = page_id AND uid = me() |
- Getting all images owned by the user:
1 |
SELECT src, caption FROM photo WHERE owner = me() |
- Select id, name and picture of their friends, limiting the result in 25 records:
1 |
SELECT uid, name , pic_square FROM user WHERE uid IN ( SELECT UID2 FROM friend WHERE uid1 = me () LIMIT 25) |
- Gets details of user albums:
1 |
SELECT aid, owner, name , object_id FROM album WHERE owner = me() |
- Gets user notifications that have not been read
1 |
SELECT notification_id, sender_id, app_id, icon_url, title_html, body_html, href |
3 |
FROM notification WHERE recipient_id = me () AND is_unread = 1 AND 0 = is_hidden |
Limitations of FQL
Selected below some limitations present in FQL and that usually generate questions for developers who are starting to develop FQLs:
- Unable to create queries with JOINs (An alternative is to use sub-selects)
- * Can not Use:
SELECT * (You have to specify all the columns you want to get the table)
- You can not use basic functions like sum () , count () , etc..
Working with Facebook API Part-3: Facebook Query Language (FQL)
Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.