আগামী ৩০ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Working with Facebook API Part-3: Facebook Query Language (FQL)

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

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:

 $result  = $facebook ->api(array(

"method"  => "fql.query" ,

"query"   => " SELECT friend_count FROM user WHERE uid =me()" ,

));

if($result) {

echo  "Number of friends: {$result [0]['friend_count']}" ;

}

See Full Functional PHP Example Below:

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

<?php

require  'facebook-php-sdk-master/src/facebook.php' ;

// Create the instance of the application, stating the appid and secret

$facebook = new Facebook(array(

'appId' => '236076976577688',

'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',

));

// Get the id of the User

$user = $facebook->getUser();

if ($user){ // User logged

try {

// Get User data logged

$user_profile  = $facebook->api('/me');

$result=$facebook->api(array(

"method"=>"fql.query" ,

"query"=>"SELECT friend_count FROM user WHERE uid =me()",

));

if($result) {

echo  "Number of friends: {$result[0]['friend_count']}" ;

}

} catch(FacebookApiException $e ) {

echo "<pre>";

var_dump($e);
echo "</pre>";
$user  = null;

}

} else  {

// User not logged in, request authentication

$loginUrl  = $facebook->getLoginUrl();

header ( "Location:$loginUrl");

}

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

Running through Javascript:

Example of how to run FQL via Javascript SDK:

<script>

FB.api ({

method: 'fql.query',

query:"SELECT friend_count FROM user WHERE uid =me()"

}, function (resp) {

if(resp.length) {

alert (resp[0][friend_count]);

}

});

</script>

See Full Functional JAVASCRIPT Example Below:

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Facebook API</title>

<script src="http://connect.facebook.net/pt_BR/all.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script>

FB.init({

appId : '236076976577688',

status: true, cookie: false, xfbml: false

});

var FBCountFriends = function() {

FB.api({

method: 'fql.query',

query:  'SELECT friend_count FROM user WHERE uid =me()'

}, function(resp) {

if (resp.length) {

alert("Your Total Friends "+resp[0].friend_count);

}

});

}

// onload

jQuery(document).ready(function() {

FB.getLoginStatus(function(response) {

if (response.status === 'connected') {

FBCountFriends();

} else {

FB.login(function(response) {

if (response.authResponse) {

FBCountFriends();

}

});

}

})

});

</script>

</head>

<body>

</body>

</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:

  • Select all the user’s friends who are using your application:
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:
SELECT uid FROM page_fan WHERE page_id = page_id AND uid = me()

--- Page_id change the id of the fanpage

  • Getting all images owned by the user:
SELECT  src, caption FROM  photo WHERE  owner = me()

  • Select id, name and picture of their friends, limiting the result in 25 records:
SELECT  uid, name , pic_square FROM  user  WHERE  uid IN  ( SELECT  UID2 FROM  friend WHERE  uid1 = me () LIMIT 25)

  • Gets details of user albums:
SELECT  aid, owner, name , object_id FROM  album WHERE  owner = me()

  • Gets user notifications that have not been read
SELECT notification_id, sender_id, app_id, icon_url, title_html, body_html, href

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 graduated in 2009 with a bachelor's degree in Engineering from State University Of Bangladesh, I'm also 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, CIDA and MAX Group where I worked on ERP software and web development., but now i'm a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services 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.
One comment on “Working with Facebook API Part-3: Facebook Query Language (FQL)

Leave a Reply

Your email address will not be published. Required fields are marked *