我正在尝试获取一大群朋友的个人资料,但出现错误:
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;
}