I am working on getting the Gmail List API setup, but for some reason, it returns all 5,000 messages in my inbox even though I have set maxResults to 100. Here is my current code:
$pageToken = NULL;
$messages = array();
$opt_param = array();
do {
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
$opt_param['maxResults'] = 100;
$opt_param['labelIds'] = 'INBOX';
}
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
if ($messagesResponse->getMessages()) {
$messages = array_merge($messages, $messagesResponse->getMessages());
$pageToken = $messagesResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
}
return $messages;
I'm not sure where I am going wrong, but I need to be able to filter down my inbox to the last 100 messages sent.
So I just realized, you are getting all messages because even though you are getting them by slices of 100, you are, in the end, doing that in a loop until you get all messages in your inbox...
If you only want the first 100, try this instead:
$messages = array();
try {
$opt_param = array(
'maxResults' => 100,
'labelIds' => 'INBOX'
);
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
$messages = $messagesResponse->getMessages();
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
if(!empty($messages)) {
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
}
}
return $messages;
So I removed the do/while loop, basically...
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With