Facebook的批量API的洞察力请求 [英] Facebook Batch API insight requests

查看:180
本文介绍了Facebook的批量API的洞察力请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我必须抓住一个页面的见解在Facebook的时间(如1 - 2年)长的时间。

我第一次尝试做一个请求,但事实证明,只要求

  / PAGE_ID /见解,因为= XXX和放大器;直到= XXX
 

不返回所有的数据我希望(它在某种程度上苏presses数据,就好像有一些限制答案的大小)。

我又试图分裂日期范围(如01.04.2011-01.04.2011 - > 01.04.2011-01.08.2011-01.12.2011-01.04.2011),它为好,不喜欢我的工作相当希望它。

我的下一个方法是只要求我所需要的洞察力值,比如page_stories,page_im pressions,......。该请求看起来像这样

  / PAGE_ID /洞察/ page_im pressions /天,因为= XXX和放大器;直到= XXX
 

这实际工作,但不与阿贾克斯。它有时似乎丢弃了一些要求(尤其是如果我改变了谷歌Chrome浏览器选项卡),我需要确保所有的请求返回一个答案。一个SYNCHRONUS的解决办法只是把太多的时间去考虑,一个请求需要至少2秒和2年的日期范围我可能有大约300单请求,这只是需要太长时间完成。

最后,我偶然发现了Facebook的能力做批量的要求,这正是我需要的。它可以收拾在一个呼叫,该呼叫显著降低了带宽的50个请求。而这里也正是我坚持。 Facebook的API提供了有关如何使用它的一些例子,但是当我在图形浏览器,并通过PHP Facebook的API,SDK测试,他们没有一次成功。我尝试收拾这个请求

  PAGE_ID /洞察/ page_fan_adds /天,因为= 1332486000&放大器;直到= 1333695600
 

到一个批次请求,但失败了。

看来这个API被窃听。它总是给我这个错误,当我使用一个问号?在relative_url字段。

  {
  错误: {
    消息:批参数必须是一个JSON数组,
    类型:GraphBatchException
  }
}
 

下面是我的尝试:

这些带来的必须是一个JSON数组错误:

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

这两个实际返回的数据,但他们忽略了参数:

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

和这个人告诉我,这是一个不支持的POST请求:

<$p$p><$c$c>?batch=[{"method":"POST","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000直到= 1333695600}]

有人能帮助?

解决方案

我终于找到了解决我的问题。这不是Facebook的文件中提及,但此请求

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

要正常工作,我们必须使用像

功能

  urlen code()
 

为en code JSON的一部分。这样,querys工作就像一个魅力。一个PHP的例子:

  $见解= $facebook->api('?batch=['.urlen$c$c('{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day?since=1332572400&until=1333782000"}').']'
    ,后,阵列('access_token'=&GT; $这个 - &GT; facebook-&GT; getAccessToken()));
 

这会导致这样的:

<$p$p><$c$c>?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]

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?

解决方案

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]

这篇关于Facebook的批量API的洞察力请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆