0

我正在尝试获取一大群朋友的个人资料,但出现错误:

Too many requests in batch message. Maximum batch size is 50

来自 API。现在我理解了错误消息,但我认为我构建了函数来减轻这个错误。我专门以 50 个块为单位进行调用。我不会在任何调用它的方法中更改 $chunk_size,所以我真的不知道这里发生了什么。

这是吐出错误的函数:

protected function getFacebookProfiles($ids, array $fields = array('name', 'picture'), $chunk_size = 50)
{
    $facebook = App::make('Facebook');
    $fields = implode(',', $fields);

    $requests = array();
    foreach ($ids as $id) {
        $requests[] = array('method' => 'GET', 'relative_url' => "{$id}?fields={$fields}");
    }

    $responses = array();
    $chunks = array_chunk($requests, $chunk_size);
    foreach ($chunks as $chunk) {
        $batch = json_encode($requests);
        $response = $facebook->api("?batch={$batch}", 'POST');

        foreach ($response as &$profile) {
            $profile = json_decode($profile['body']);

            if (empty($profile->picture->data)) {
                // something has gone REALLY wrong, this should never happen but if it does we'll have more debug information
                if (empty($profile->error->message)) {
                    throw new Exception('Unexpected error when retrieving user information for IDs:' . implode(', ', $ids));
                }

                $profile->error = (array) $profile->error;
                throw new FacebookApiException((array) $profile);
            }

            $profile->picture = $profile->picture->data;
        }

        $responses = array_merge($responses, $response);
    }

    return $responses;
}
4

1 回答 1

2

您没有在为 API 调用生成的 JSON 中使用 $chunk 变量,但仍然是原始的、未修改的 $requests。

快乐的砰砰声:-)

于 2013-09-24T07:51:14.073 回答