Home » Php » php – Facebook Batch API insight requests

php – Facebook Batch API insight requests

Posted by: admin July 12, 2020 Leave a comment


For a project I have to grab the insights of a page over a long period of time (e.g. 1-2 years) of facebook.

I first tried to do a single request but it turned out that only requesting


doesn’t return all the data I want (it somehow supresses data as if there’s some limit to the size of the answer).

I then tried to split up the date range (e.g. 01.04.2011-01.04.2011 -> 01.04.2011-01.08.2011-01.12.2011-01.04.2011) which as well, didn’t quite work like I wanted it to.

My next approach was to request only the insight values I need, like ‘page_stories, page_impressions, …’. The requests looked like this


This actually worked but not with ajax. It sometimes seemed to drop some requests (especially if I changed the browser tab in google chrome) and I need to be sure that all requests return an answer. A synchronus solution would just take way too much time considering that one requests needs at least 2 seconds and with a date range of 2 years I may have about 300 single requests this just takes way too long to complete.

Lastly I stumbled over facebook ability to do batch requests which is exactly what I need. It can pack up to 50 requests in one call which significantly lowers the bandwith. And here’s where I’m stuck. The facebook api gives some examples on how to use it but none of them worked when I tested them in the Graph Explorer and via the php facebook api sdk. I tried to pack this request


into a batch request but failed.

It seems that the api is bugged. It’s always giving me this error when I’m using a question mark ‘?’ in the ‘relative_url’ field.

  "error": {
    "message": "batch parameter must be a JSON array", 
    "type": "GraphBatchException"

Here is what I tried:

These give the ‘must be a JSON array’ error:


These two actually return data but they are ignoring the parameters:

?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000 until=1333695600"}]

And this one tells me that this is an ‘Unsupported post request’:

?batch=[{"method":"POST","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000 until=1333695600"}]

Can someone help?

How to&Answers:

I finally found the solution to my problem. It’s not mentioned in the facebook documentation but for this request


to properly work we have to use a function like


to encode the json part. This way the querys work like a charm. A php example:

$insights = $facebook->api('?batch=['.urlencode('{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day?since=1332572400&until=1333782000"}').']'
    ,'post',array('access_token' => $this->facebook->getAccessToken()));

which results in this:



This example is for using an array of IDs to make a batch request with urlencoding.

$postIds = [

$queries = [];
foreach( $postIds as $postId ) {
    $queries[] = [
        'method'        => 'GET',
        'relative_url'  => '/' . $postId . '/comments?summary=1&filter=stream&order=reverse_chronological',

$requests = $facebook->post( '?batch=' . urlencode( json_encode( $queries ) ) )->getGraphNode();