FQL: a powerful way to query Facebook’s Graph API

Today a post on FQL (Facebook Query Language). I will give you some examples that hopefully show the power of this SQL-like querying interface of the Facebook API.


So in short: FQL lets you query Facebook’s Graph API (what is the Graph API?).

Some use cases:

1. get all comments for a specific URL

For example to see the comments on this book that was added to My Reading List: http://www.bobbelderbos.com/books/?googleID=UgrUeIwsS60C

Query:

SELECT post_fbid, fromid, object_id, text, time FROM comment
WHERE object_id IN (SELECT comments_fbid FROM link_stat WHERE url =
'http://www.bobbelderbos.com/books/?googleID=UgrUeIwsS60C')

This returns:

[
   {"post_fbid": "10150238413182479",
      "fromid": 628517118,
      "object_id": 10150208342007479,
      "text": "worth every penny, one of the best css titles I have read.",
      "time": 1310773947
   }
]

(more info on comments and replies per comment)

2. get all members of my BobsBlog Facebook Group

Query:

SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (select uid from group_member where gid = 118232854902671)
order by name

3. get all the music your friends like – I used this one in friendsjukebox.com

(requires extended permission: friends_likes)

Query:

SELECT uid, name, pic_square,music FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name

!! Note the batching of multiple queries into a single call !!

Snippet output:


… you can do the same for movies – query:

SELECT uid,name,pic_small,movies FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name

4. get all birthdays of my friends of this month

(requires extended permission: friends_birthday)

Query:

SELECT uid, name,pic_square, birthday_date FROM user
WHERE (substr(birthday_date, 0, 2) = "07") AND uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) order by name

!! Note the use of substr in this query !!
Other functions that are available are now(), strlen(), substr() and strpos().

Fetch URL to execute the query

XML and JSON are supported formats. I am using JSON for this example.
$oauth_token is obtained by the Facebook’s Authentication: you need to create a Facebook App first and get the user to login and grant permission to your app (I will do a followup post …)

$query = "https://api.facebook.com/method/fql.query?query=";
$query .= urlencode("<above query>"); 

$query .= "&access_token=".$oauth_token."&format=json";
$out = file_get_contents($query);
$response = json_decode($out, true);

echo '<pre>';print_r($response);echo '</pre>';

FQL Test Console

There is a great test console where you can play with the queries. With another user account FQL console gave me “Something went wrong. We’re working on getting it fixed as soon as we can.”  It seems you need to have an Application setup and the test user might need to have granted the extra permissions him/herself.

A handy trick is to click on the URL in the FQL console, it will open a new tab with the URL to fetch. For example 1 that would be:

FQL Tables

Of course a lot more is possible, see all Tables. The only limitation is that you can only query indexable properties (columns)

What’s next?

In the next post I will iterate over example 4. (birthday calendar) to show you how to use FQL as a workhorse for the input of your Facebook app. I will also cover some more aspects of the Facebook API like authorization and dialogs …

From here on …

You can subscribe to this blog, join my Facebook group, comment on this post below, or find me on twitter @bbelderbos.

By the way, if you have interesting topics you would like to see covers, just drop me a message, I am open to anything web-related.


You might also like:



Enjoyed this article?

Share on Google+

This entry was posted in APIs, Programming and tagged , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • Rafael Pellon

    Hi
    Great post. I am looking for a complex FQL queries to get all the comments done by the user in the post of his friends. Any idea?
    Thanks

    • Anonymous

      Thanks Rafael!
      Do you have the userid and info (url) regarding the post? Nesting statements it might be possible

      • Rafael Pellon

        Hi
        I explain my enviroment. I have a facebook user with permissions to access his data (access_offline, stream_read and friends_about_me).
        I want to get the following data:
        - All the posts sent by USERID
        - The post in the USERID’s wall

        SELECT post_id, viewer_id,app_id,source_id,updated_time,filter_key,attribution,actor_id,target_id,message,app_data,action_links,attachment,impressions,comments,likes,privacy,permalink FROM stream WHERE source_id=me() LIMIT 500″

        I can use limit parameter, and conditiosn in update_time to get more records.

        - The comments made in other walls.

        My first idea was:
        SELECT
            xid
        ,    object_id
        ,    post_id
        ,    time
        ,    text
        FROM
            comment
        WHERE
            fromid = me() AND
            post_id IN (SELECT post_id FROM stream WHERE filter_key=’others’)
        LIMIT 500

        Could you help me or give me any advices?

        • Anonymous

          yep I will look into this and answer your by email

          • Anonymous

            Rafael, 

            It might be easier to do with the graph API :
            a. it is really the way FB goes forward with this stuffb. they have a great way to experiment with permissions with their explorer page: https://developers.facebook.com/tools/explorer/ a. get access token b. put in a https://graph.facebook.com/../.. url and see if it gives any json output :)1. read status updates: -> Friends Data Permission -> activate friends_status results: https://graph.facebook.com/fbID/statuses2. get all user’s posts-> Extended Permissions -> read_streamresults: https://graph.facebook.com/fbID/postsSee also https://developers.facebook.com/docs/reference/api/permissions/) This seems pretty much the thing you need. HTH and let me know where you will implement this ! Bob

        • http://bobbelderbos.com Bob

          bit busy, but I hope to be able to look at this

        • Anonymous

          Rafael, 

          It might be easier to do with the graph API :
          a. it is really the way FB goes forward with this stuffb. they have a great way to experiment with permissions with their explorer page: https://developers.facebook.com/tools/explorer/ a. get access token b. put in a https://graph.facebook.com/../.. url and see if it gives any json output :)1. read status updates: -> Friends Data Permission -> activate friends_status results: https://graph.facebook.com/fbID/statuses2. get all user’s posts-> Extended Permissions -> read_streamresults: https://graph.facebook.com/fbID/postsSee also https://developers.facebook.com/docs/reference/api/permissions/) This seems pretty much the thing you need. HTH and let me know where you will implement this ! Bob

  • Khenidm

    NIce job

    • Anonymous

      Thank you, nice to hear

  • http://bobbelderbos.com/ Bob Belderbos

    I need to check this out. I will get back. Did you check the documentation and tried with the FQL console I linked to from in my post?

    • FrostyTop

      I did.. it seems that in FQL the documentation says movies is a string column on the user table, so doesn’t seem that this type of query will work. 

      I found this link on stackoverflow which seems promising, but there’s seems to be some limit and this doesn’t expose all the results. http://stackoverflow.com/questions/5006999/in-facebook-graph-api-how-to-find-which-all-of-your-friends-like-a-particular-bo/5008666#5008666

      Using the graph API,  can kick off 1 request for each friend and get their movies with movie IDs etc, but obviously perf is terrible. 

      • http://bobbelderbos.com/ Bob Belderbos

        Hopefully it is possible with graph somehow, I will look into this sometime later. Would be cool if we get this working …

  • http://bobbelderbos.com/ Bob Belderbos

    thanks

  • My Links

  • Post categories

  • Bob’s Reading List

  • Some of my Apps

    hi folks, what are you reading?

    sharemovi.es: tell your friends about your favorite movies

    Friends Jukebox | Create a Jukebox based on the Music your Friends like!

    get to know the world ...

    keep the tweets you care about ...