Batch requests API has been a very useful and performance-enhancing thing for application developers since it was announced by Facebook. If your application has multiple transactions done in one page, you could then stuff them into a single http request (called batch) and wait for the response, that’s all.

For instance, if your application had 10 Graph API queries, and if you executed them one by one, you would get a very long execution time since 10 HTTP requests has to be done sequentially. This kind of use brings a “unignorable” overhead. If you use batch requests, you give your queries to the batch and in milliseconds you will get the result.

There are two basic performance advantages. 1- Facebook executes your queries simultaneously unless they are dependent on each other. (And yes man, you could send dependent queries. For details read the documentation please http://developers.facebook.com/docs/reference/api/batch ) 2- You get rid of 9 HTTP communication times since you send 1 HTTP request and get 1 HTTP response.

For my own usage, I wrote a simple PHP class in order to make batch requests easily. If you are interested, you could surely use it without any permission :)

<?php

// Facebook Batch Requests PHP Class
// by Gokhan Ozturk @ Nov 17, 2011
// Original Article: http://blog.pclabs.com.tr/gokhanozturk/2011/11/17/facebook-batch-requests-php-class/

class facebook_batch {
   protected $requests = array();
   protected $responses = null;
   protected $cur = 1;
   protected $map = array();

   const MAX_NUMBER_OF_REQUESTS = 20;

   public function add($path, $method = 'GET', $params = array(), $extra = array()) {
      if(count($this->requests) > self::MAX_NUMBER_OF_REQUESTS) return false;

      $path = trim($path, '/ ');

      $body = http_build_query($params);
      $body = urldecode($body);

      if(strtolower($method) == 'get') {
         if(count($params) > 0) {
            $path .= "?" . $body;
            $body = "";
         }
      }

      $key = $this->cur++;
      $this->requests[$key] = array_merge(array('relative_url' => $path, 'method' => $method, 'body' => $body), $extra);

      return intval($key);
   }

   public function remove($key) {
      unset($this->requests[$key]);
   }

   public function execute() {
      global $facebook;

      $i = 0;
      foreach($this->requests as $k => $r) {
         $this->map[$k] = $i++;
      }

      $batch = json_encode(array_values($this->requests));
      $params = array('batch' => $batch);

      $this->responses = $facebook->api('/', 'POST', $params);
   }

   public function response($key) {
      if(! $this->responses) $this->execute();

      $rkey = $this->map[intval($key)];
      $resp = $this->responses[intval($rkey)];

      if($resp['code'] != 200) return false;
      return json_decode($resp['body'], true);
   }

   public function getRequests() {
      return $this->requests;
   }
}

?>

This class assumes a global $facebook variable which is of official Facebook class, is ready-to-use.

Usage :

1- Create an instance of class

$batch = new facebook_batch();

2- Add your queries one by one

$mybkey = $batch->add('/me/feed', 'get');

Note that “add” method returns a key (called bkey) corresponding to your query. Also note that “add” method is very similar to “api” method of Facebook class. They have the first 3 parameters same. “add” method has a fourth parameter called “extra” and if you like to add some extra parameters to your batch request (like “name” for dependent requests), you could then send them in an array through that parameter)

3- Execute your queries

$batch->execute();

4- Read the response using your bkey

$response = $batch->response($mybkey);

And use your response variable to do whatever you like.

Leave your comment