How can I execute a FQL query with Facebook Graph API

81,301

Solution 1

Here's an example of how to do a FQL query using the Graph API and JavaScript

FB.api(
        {
            method: 'fql.query',
            query: 'SELECT uid, first_name, last_name FROM user WHERE uid = ' + someUid
        },
        function(data) {
            //    do something with the response
        }
);

This assumes you've already setup your page according to the Facebook guidelines as shown here - http://developers.facebook.com/docs/reference/javascript/

Solution 2

PHP Solution:

$data = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT columns FROM table...' ));

Solution 3

Using the Javascript SDK, you can accomplish this using the following:

 FB.api('fql', { q: 'query here' }, function (response)
{
 //Logic here
};

No legacy REST API required. I see a lot of confusion on this and Facebook hasn't made it very clear.

Solution 4

This is another way to execute multiple fql queries in short span.

//$current_user=facebook id

 $query1="SELECT uid, name FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user)";
 $query2="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user )";
 $query3="SELECT uid, name, work, education FROM user WHERE uid = $current_user";
 $queries = array(
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query1)),
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query2)),
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query3))
            );

            $objs = $facebook->api('/?batch='.json_encode($queries), 'POST');

$objs gets json array of whole result of thre queries.

And it is saving time a lot. This 3 queries individually takes total 9 seconds. With multiquery it takes 7 seconds. And with batch request it takes 3.6 seconds.

Share:
81,301
user63898
Author by

user63898

developer

Updated on October 16, 2020

Comments

  • user63898
    user63898 over 3 years

    I'm looking without any success for a way to execute a FQL(facebook query language) query with the new Open Graph API.

    Does anyone know how I can do this?

    Found the answer here with this excellent example: http://code.google.com/p/facebook-cpp-graph-api/