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(
2  
3 "method"  => "fql.query" ,
4  
5 "query"   => " SELECT friend_count FROM user WHERE uid =me()" ,
6  
7 ));
8  
9 if($result) {
10  
11 echo  "Number of friends: {$result [0]['friend_count']}" ;
12  
13 }

See Full Functional PHP Example Below:

Complete working example: (Just only parameterize the appId and secret)

1 <?php
2  
3 require  'facebook-php-sdk-master/src/facebook.php' ;
4  
5 // Create the instance of the application, stating the appid and secret
6  
7 $facebook new Facebook(array(
8  
9 'appId' => '236076976577688',
10  
11 'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',
12  
13 ));
14  
15 // Get the id of the User
16  
17 $user $facebook->getUser();
18  
19 if ($user){ // User logged
20  
21 try {
22  
23 // Get User data logged
24  
25 $user_profile  $facebook->api('/me');
26  
27 $result=$facebook->api(array(
28  
29 "method"=>"fql.query" ,
30  
31 "query"=>"SELECT friend_count FROM user WHERE uid =me()",
32  
33 ));
34  
35 if($result) {
36  
37 echo  "Number of friends: {$result[0]['friend_count']}" ;
38  
39 }
40  
41 } catch(FacebookApiException $e ) {
42  
43 echo "<pre>";
44  
45 var_dump($e);
46 echo "</pre>";
47 $user  = null;
48  
49 }
50  
51 else  {
52  
53 // User not logged in, request authentication
54  
55 $loginUrl  $facebook->getLoginUrl();
56  
57 header ( "Location:$loginUrl");
58  
59 }

 Output:  Number of friends: 1000 (Your  total friends)

Running through Javascript:

Example of how to run FQL via Javascript SDK:

1 <script>
2  
3 FB.api ({
4  
5 method: 'fql.query',
6  
7 query:"SELECT friend_count FROM user WHERE uid =me()"
8  
9 }, function (resp) {
10  
11 if(resp.length) {
12  
13 alert (resp[0][friend_count]);
14  
15 }
16  
17 });
18  
19 </script>

See Full Functional JAVASCRIPT Example Below:

1 <!doctype html>
2  
3 <html>
4  
5 <head>
6  
7 <meta charset="utf-8">
8  
9 <title>Facebook API</title>
10  
11 <script src="http://connect.facebook.net/pt_BR/all.js"></script>
12  
13 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
14  
15 <script>
16  
17 FB.init({
18  
19 appId : '236076976577688',
20  
21 status: true, cookie: false, xfbml: false
22  
23 });
24  
25 var FBCountFriends = function() {
26  
27 FB.api({
28  
29 method: 'fql.query',
30  
31 query:  'SELECT friend_count FROM user WHERE uid =me()'
32  
33 }, function(resp) {
34  
35 if (resp.length) {
36  
37 alert("Your Total Friends "+resp[0].friend_count);
38  
39 }
40  
41 });
42  
43 }
44  
45 // onload
46  
47 jQuery(document).ready(function() {
48  
49 FB.getLoginStatus(function(response) {
50  
51 if (response.status === 'connected') {
52  
53 FBCountFriends();
54  
55 else {
56  
57 FB.login(function(response) {
58  
59 if (response.authResponse) {
60  
61 FBCountFriends();
62  
63 }
64  
65 });
66  
67 }
68  
69 })
70  
71 });
72  
73 </script>
74  
75 </head>
76  
77 <body>
78  
79 </body>
80  
81 </html>

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

Testing FQL queries using the Graph API 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/

Facebook Api Permissions

After clicking the Get Access Token you’re already able to test their FQLs. See example below:

Get Facebook API Access Token

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:

1 SELECT  uid, name  FROM  user  WHERE  uid IN SELECT  UID2 FROM  friend WHERE  uid1 = me ()) AND  is_app_user = 1

 

1 SELECT uid FROM page_fan WHERE page_id = page_id AND uid = me()
2  
3 --- Page_id change the id of the fanpage
1 SELECT  src, caption FROM  photo WHERE  owner = me()
1 SELECT  uid, name , pic_square FROM  user  WHERE  uid IN  SELECT  UID2 FROM  friend WHERE  uid1 = me () LIMIT 25)
1 SELECT  aid, owner, name , object_id FROM  album WHERE  owner = me()
1 SELECT notification_id, sender_id, app_id, icon_url, title_html, body_html, href
2  
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:

 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.