Talk:Using Batching API

From Facebook Developer Wiki

Jump to: navigation, search

Can this be used to make parallel calls to the same function? (ie. If I wanted to loop through thousands of users to update their profiles with profile_setFBML could it be done using this method?) Also is there a limit to the number of calls in one batch transaction?

I tried batching setFBML and it does work. The number of calls is limited to 20.

--

Is anyone using the batching API yet?

Whenever we try to use it, it appears to be making calls to our application from Facebook INTERNAL IP addresses (like 10.17.44.161). This is causing our app to throw the error:

Uncaught exception 'FacebookRestClientException' with message 'Unauthorized source IP address (ip was: 10.17.44.161)'

Can you only use the batching API in certain scenarios, or only with IFRAME apps or something? Or is this just a bug in a newly created FB tool?

[edit] Known Issue with IP Addresses

There is a known issue where an error returns an internal Facebook IP address. As a workaround, don't specify any IP addresses in the IP Addresses of Servers Making Requests field in your application settings.

[edit] Problems with php5 client lib

If one of the batched api calls returns an exception execute_server_side_batch throws the exception without populating the results for the rest of the calls and without setting the batch_queue to null. This will cause subsequent calls to the client lib to fail. So, here's how we changed execute_server_side_batch()

private function execute_server_side_batch() {

   $item_count = count($this->batch_queue);
   $method_feed = array();
   foreach($this->batch_queue as $batch_item) {
     $method_feed[] = $this->create_post_string($batch_item['m'], $batch_item['p']);
   }
   $method_feed_json = json_encode($method_feed);
   $serial_only = $this->batch_mode == FacebookRestClient::BATCH_MODE_SERIAL_ONLY ;
   $params = array('method_feed' => $method_feed_json, 'serial_only' => $serial_only);
   if ($this->call_as_apikey) {
     $params['call_as_apikey'] = $this->call_as_apikey;
   }
   $xml = $this->post_request('batch.run', $params);
   $result = $this->convert_xml_to_result($xml, 'batch.run', $params);
   if (is_array($result) && isset($result['error_code'])) {
        $this->batch_queue = null;
        throw new FacebookRestClientException($result['error_msg'], $result['error_code']);
   }
   for($i = 0; $i < $item_count; $i++) {
     $batch_item = $this->batch_queue[$i];
     $batch_item_result_xml = $result[$i];
     $batch_item_result = $this->convert_xml_to_result($batch_item_result_xml, $batch_item['m'], $batch_item['p']);
     if (is_array($batch_item_result) && isset($batch_item_result['error_code'])) {
         $lastException = new FacebookRestClientException($batch_item_result['error_msg'], $batch_item_result['error_code']);
     }
     else{
         $batch_item['r'] = $batch_item_result;
     }
   }
   $this->batch_queue = null;
   if (isset($lastException)) throw $lastException;
 }

[edit] Batch.run and session_key

It seems to me that if I include my session_key with the inner request, but not with the outer request, then the inner requests act as if I hadn't provided a session_key. However, if I provide a session_key on both all the inner requests, and the outer batch.run request, then everything works fine. I don't know if this is a bug or not, but I thought I should pass this along for anyone else trying to implement batching.

reference