title: Implementing full sync with gmail api
body: <p>I use google php library which works with <code>gmail api</code>. I want to get all messages from my gmail account.</p>
  
  <p>That's how I do that:</p>
  
  <pre><code>        do {
              $this-&gt;client-&gt;setUseBatch(false);
              $messagesResponse = $service-&gt;users_messages-&gt;listUsersMessages('me', compact('pageToken'));
  
              if ($messagesResponse-&gt;getMessages()) {
                  $batch = new Google_Http_Batch($this-&gt;client);
                  $this-&gt;client-&gt;setUseBatch(true);
  
                  foreach ($messagesResponse as $item) {
                      $request = $service-&gt;users_messages-&gt;get('me', $item-&gt;id, ['format' =&gt; 'raw']);
                      $batch-&gt;add($request);
                  }
  
                  $messages = array_merge($messages, $batch-&gt;execute());
                  $pageToken = $messagesResponse-&gt;getNextPageToken();
              }
          } while ($pageToken);
  </code></pre>
  
  <p>When I run this script from CLI I got the following error:</p>
  
  <blockquote>
    <p>[Google_IO_Exception]<br>
          Operation timed out after 100000 milliseconds with 57157107 bytes received</p>
  </blockquote>
  
  <p>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.</p>