So, I just got another hundred points and awards on a stackoverflow answer I gave nearly 3 years ago and thought I would dig this up out of the pits so people can see it better. I mean, honestly it's shocking me how difficult this still is for some people, but then again Facebook's SDK documentation is notoriously lackluster.
SO user Paul Mennega once asked how to batch FB-SDK calls together in PHP right around when the 2.x->3.0 conversions were going on. At the time, my company was doing some really heavy FB tab work, so I was just living on Stack, searching everything related to XFBML/FBJS and the transitional state they were in. This question rolls across my desk, and I figured, "Hey, you know what would be a great way to give back to Stack for all the help? Let's port this answer to the 3.0 solution," because everyone and their mother had created a library for this exact problem.
The solution is surprisingly simple:
// 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['body'], true); $user_groups = json_decode($batchResponse['body'], true);
What's been tripping everyone up over the years? Well, for one, batch queries are
POST requests. What happens to all the
GET routes, then? Easy, the first array the batch method takes, you define what
HTTP method is used,
GET|POST|PUT/PATCH|DELETE, and the relative URL from the API root. Really, that's it. Except, there's not really any documentation on it. For nearly 3 years. Seriously, take a look at the Facebook man pages for batch. The abstract CURL uses is slightly mangled when using PHP, and there really isn't a clear response for which is which. Forget about piping from one to another. Some day soon, when I've had enough to drink in my while I'm behind the computer, I'll figure out how to do that. But I digress - that's it. That's all there is to Batch calls with the Facebook Open Graph API using PHP.