32

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/

raevilman
  • 3,169
  • 2
  • 17
  • 29
user63898
  • 29,839
  • 85
  • 272
  • 514

5 Answers5

31

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/

mjallday
  • 9,796
  • 9
  • 51
  • 71
  • @happyhardik Its a graph api example using javascript with facebook's python-sdk library. [FB.api docs](http://developers.facebook.com/docs/reference/javascript/fb.api/) – Derek Dahmer Feb 21 '11 at 18:12
  • 2
    this is not Graph API. Just FQL query layer under Graph API and REST API. – Raptor Jun 10 '11 at 09:13
  • 2
    This doesn't result in a GraphAPI call. FB.api used in this way will make a call to the deprecated REST API. There is a difference, for example, the GraphAPI calls support ETags so you can cache API call results on the client. – Giscard Biamby Sep 27 '12 at 13:58
17

PHP Solution:

$data = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT columns FROM table...' ));
Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
12

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.

Gaff
  • 5,507
  • 3
  • 38
  • 49
  • Hi ROFLwTIME, can you expose the resource of your answer? I can't find such example on facebook developers guides. There nothing written about fql and javascript sdk... i would like to get official doc link, thanks ! – arty Nov 11 '12 at 13:52
  • 1
    @arty, there really isn't an official doc that I am aware of. I got this from the Facebook developer's comments on some of their blogs from users asking questions about the javascript sdk and FQL. This solution has worked fine for me for over a year now. I know you are probably looking for something official so you don't develop something that breaks on down the line. As far as I know, the JavaScript SDK and FQL aren't going anywhere, so I think this will be fine for now, but there are no guarantees unfortunately. – Gaff Nov 11 '12 at 14:37
  • 1
    ROFLwTIME, thanks for your input and share of experience. your answer helped me very much when i found it. shame is that facebook doesn't consistent with their documentation... one can implement the same in 100 various ways according to their examples/guides/tutorials... i was new to their api when i asked the official link... i already understood that it's was a too much to expect for ) facebook guys, please put more efforts to maintain a strict documentation, your are not su company anymore... – arty Nov 28 '12 at 22:22
1

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.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
0

FQL with PHP here I show how to use FQL. It is very simple if you take a careful look at the current facebook api documentation. Sometimes it is better to not read articles about facebook api issues and look straight at the documentation.

Ziagl
  • 492
  • 2
  • 8
  • 22