Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do batch request of fql queries in graph api?

I am having 4-5 fql queries in single function. Each of them taking 2-4 seconds to execute. Total 14-15 seconds are required to execute that whole function. User required to wait for long time. So I want to reduce that processing time. ( There is not well supported multi-threading concept in PHP.)

I have heard of batch request concept in graph api. And I have googled a lot but didn't understand how to use batch request for fql queries in graph api.

  • Can anyone give explanation with example for using batch request of fql queries ?
  • By what time query processing time will reduce?
  • Is there any another method to reduce time of many fql queries?
like image 462
Somnath Muluk Avatar asked Dec 13 '25 01:12

Somnath Muluk


2 Answers

Updated:

https://graph.facebook.com/?batch=[{"method":"GET","relative_url":"me"},{"method":"GET","relative_url":"me/friends?limit=50"}]&access_token=ACCESS_TOKEN&method=post

More info here: http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-graph-api/

like image 120
Alexander Nenkov Avatar answered Dec 15 '25 16:12

Alexander Nenkov


//$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.

like image 21
Somnath Muluk Avatar answered Dec 15 '25 15:12

Somnath Muluk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!