Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Batch API insight requests

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

/PAGE_ID/insights?since=xxx&until=xxx

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

/PAGE_ID/insights/page_impressions/day?since=xxx&until=xxx

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

PAGE_ID/insights/page_fan_adds/day?since=1332486000&until=1333695600

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:

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

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"}]
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000,until=1333695600"}]
?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?

like image 355
Ke Vin Avatar asked Apr 07 '12 16:04

Ke Vin


2 Answers

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

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

to properly work we have to use a function like

urlencode()

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:

?batch=[%7B%22method%22%3A%22GET%22%2C%22relative_url%22%3A%22%2FPAGE_ID%2Finsights%2Fpage_fan_adds%2Fday%3Fsince%3D1300086000%26until%3D1307862000%22%7D]
like image 180
Ke Vin Avatar answered Sep 26 '22 01:09

Ke Vin


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

$postIds = [
    'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
    'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
    'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
    'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
    'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
];

$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();
like image 38
William Isted Avatar answered Sep 23 '22 01:09

William Isted