rich97
rich97

Reputation: 2879

Facebook PHP SDK "Too many requests in batch message. Maximum batch size is 50"

I'm trying to get the profiles of a large group of friends and I'm getting the error:

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

From the API. Now I understand the error message but I thought I built the function to mitigate this error. I specifically make the calls in chunks of 50. I don't change $chunk_size in any of the methods that call it so I don't really know what is going on here.

This is the function that is spitting out the error:

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;
}

Upvotes: 0

Views: 533

Answers (1)

C3roe
C3roe

Reputation: 96455

You are not using your $chunk variable in the JSON you generate for your API call, but still the original, unmodified $requests.

Happy slamming :-)

Upvotes: 2

Related Questions