Using Facebook API for Batch Requests

| | 1 min read

Using Facebook API we can pass instructions for several operations in a single HTTP request. Facebook API process operations as sequentially if it is dependent otherwise it will process as parallel. After completing all the operation it will send a consolidate response to you.

Now we can look on to the simple example

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
$session = new FacebookSession('facebook_access_token');
try {
  $response = (new FacebookRequest($session, 'GET', '/me'))->execute();
  $params = [
    [ 
      "method"  => "GET",
      "relative_url"  => "/me/groups"
    ],

    [
      "method"  => "GET",
      "relative_url"  => "me/likes"
    ],
  $response = (new FacebookRequest($session, 'POST', '?batch='.json_encode($params) ))->execute();
  $objects = $response->getGraphObject();
  foreach($objects->asArray() as $object){
    $response_body = json_decode($object->body, 1);
    print_r($response_body);
  }
} catch(FacebookRequestException $e) {
print_($e);
}   

Limitation

Facebook limit the number of requests is 50 in each batch. Please feel free to share your thoughts and doubts regarding thishere.