使用gmail api实现完全同步

I use google php library which works with gmail api. I want to get all messages from my gmail account.

That's how I do that:

        do {
            $this->client->setUseBatch(false);
            $messagesResponse = $service->users_messages->listUsersMessages('me', compact('pageToken'));

            if ($messagesResponse->getMessages()) {
                $batch = new Google_Http_Batch($this->client);
                $this->client->setUseBatch(true);

                foreach ($messagesResponse as $item) {
                    $request = $service->users_messages->get('me', $item->id, ['format' => 'raw']);
                    $batch->add($request);
                }

                $messages = array_merge($messages, $batch->execute());
                $pageToken = $messagesResponse->getNextPageToken();
            }
        } while ($pageToken);

When I run this script from CLI I got the following error:

[Google_IO_Exception]
Operation timed out after 100000 milliseconds with 57157107 bytes received

If I comment the line with retrieving new token then I got first 100 messages and it works. But I can't fetch all of them. PHP doesn't have timeout, this is google_io_exception, so I don't know how to solve the problem. Plus any optimization tips would be great, since the only thing I'm aware of is batch requests, but I use them.

This question is still relevant, and as it took me lot of time to have a clear answer I'll share what I've found:

You're limited to 100 calls in a single batch request. If you need to make more calls than that, use multiple batch requests.

https://developers.google.com/gmail/api/guides/

As you can see in, based on the documentation you cannot have more than 100 calls in a single batch request. You will have therefore to count and execute below this limit.

If you maintain this limit of 100 requests, all should be working fine.