使用Facebook Graph API和PHP批量调用

Pau*_*ega 8 php batch-file facebook-graph-api

使用Facebook提供的PHP库的2.1.2版本,针对Graph API设计我的第一个应用程序.试图最大限度地提高性能等,并希望将几个调用一起打成一个调用,但在文档中找不到任何内容......我确信我必须错过一些简单的东西,但我很难过.

我想将这些调用(仅作为示例)转换为单个批处理调用:

$me     = $facebook->api('/me', $params);
$groups = $facebook->api('/me/groups', $params);
Run Code Online (Sandbox Code Playgroud)

buz*_*ord 50

只是新图形Batch API的更新:您也可以按如下方式执行:

// 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);
Run Code Online (Sandbox Code Playgroud)

这应该够了吧.

  • 这对我帮助很大.感谢您的发表! (2认同)
  • 对 - 它将批处理中的请求限制为50,而不是在您的应用中总共50个批处理请求.此外,如果您想获得创意,可以批量批量请求.你得到了一个非常慢的响应,所以我只在具有持久身份验证且需要最终响应更新数据的应用程序中完成.或者,一个cron工作.或任何大规模的操作真的.我从来没有遇到批量限制的问题,而且我已经用这个端点做了一些淫秽的事情,这会使你们所有人都厌恶地哭泣. (2认同)

Chr*_*rge 2

Facebook 建议为此使用FQL ;http://developers.facebook.com/docs/guides/performance 通过将您的请求合并到(嵌套)查询中。

他们自己的例子:

$friends_locations = $facebook->api_client->fql_query(
    'SELECT hometown_location from user where uid in ' .
    '(SELECT uid2 from friend where uid1=' . $user_id . ')');
Run Code Online (Sandbox Code Playgroud)

如果您的请求不相互依赖,您可以使用fql.multiquery