我正在尝试获取一大群朋友的个人资料,我收到错误:
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;
}
答案 0 :(得分:2)
您没有在为API调用生成的JSON中使用$ chunk变量,但仍然是未经修改的原始$ requests。
快乐抨击: - )