Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch calls with Facebook Graph API & PHP

Designing my first app against the Graph API, using version 2.1.2 of the Facebook supplied PHP library. Trying to maximize performance, etc out of the box and want to batch a few calls together into one call, but can't find anything in the documentation... I am sure I must be missing something simple, but am stumped.

I'd like to turn these calls (just an example) into a single batched call:

$me     = $facebook->api('/me', $params);
$groups = $facebook->api('/me/groups', $params);
like image 545
Unpossible Avatar asked Nov 05 '10 15:11

Unpossible


People also ask

Is Facebook Graph API deprecated?

Graph API Version Deprecations: August 3, 2021: Graph API v3. 3 will be deprecated and removed from the platform. November 2, 2021: Graph API v4. 0 will be deprecated and removed from the platform.

How can multiple requests be executed in a batch request?

Individual requests can be executed in a specified order by using the dependsOn property. This property is an array of strings that references the id of a different individual request.

Is Facebook Graph API a REST API?

As you suggested, the Graph API, just like the Legacy REST API, is in fact a RESTful API.

What is batch API call?

A batch request is a single standard HTTP request containing multiple Google Classroom API calls, using the multipart/mixed content type. Within that main HTTP request, each of the parts contains a nested HTTP request. Each part begins with its own Content-Type: application/http HTTP header.


1 Answers

Just an update for the new graph Batch API: You can also execute that as follows:

// Save your method calls into an array
$queries = array(
    array('method' => 'GET', 'relative_url' => '/me'),
    array('method' => 'GET', 'relative_url' => '/me/groups')
);
// POST your queries to the batch endpoint on the graph.
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');

// Return values are indexed in order of the original array, content is in ['body'] as a JSON
// string. Decode for use as a PHP array.

$user_profile = json_decode($batchResponse[0]['body'], true);
$user_groups = json_decode($batchResponse[1]['body'], true);

That should do the trick.

like image 83
buzzedword Avatar answered Sep 30 '22 12:09

buzzedword