PHP Requires Chilkat v11.0.0+
PHP
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;
use Chilkat2\StringTable;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$token = new JsonObject();
try {
$token->LoadFile('gmail-token.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not read gmail-token.json from the private working directory.\n";
exit;
}
if (($token->JsonTypeOf('access_token') != 1) || ($token->StringOfEquals('access_token', '', true))) {
echo "An OAuth access token is required.\n";
exit;
}
$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $token->StringOf('access_token');
$resp = new HttpResponse();
$reply = new JsonObject();
try {
$http->HttpNoBody('GET', 'https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5', $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($resp->StatusCode == 0) {
echo "Gmail did not return an HTTP response.\n";
exit;
}
if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
echo 'Gmail HTTP ', $resp->StatusCode, "\n";
try {
$reply->Load($resp->BodyStr);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($success) {
echo $reply->StringOf('error.message'), "\n";
}
exit;
}
try {
$reply->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Expected a Gmail JSON response.\n";
exit;
}
$ids = new StringTable();
$count = $reply->SizeOfArray('messages');
$i = 0;
while ($i < $count) {
$reply->I = $i;
try {
$ids->Append($reply->StringOf('messages[i].id'));
} catch (\Chilkat2\Exception $e) {
echo "Could not retain a message ID.\n";
exit;
}
$i = $i + 1;
}
$url = new StringBuilder();
$i = 0;
while ($i < $ids->Count) {
$url->SetString('https://gmail.googleapis.com/gmail/v1/users/me/messages/');
$url->Append($ids->StringAt($i));
$url->Append('?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date');
try {
$http->HttpNoBody('GET', $url->GetAsString(), $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($resp->StatusCode == 0) {
echo "Gmail did not return an HTTP response.\n";
exit;
}
if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
echo 'Gmail HTTP ', $resp->StatusCode, "\n";
try {
$reply->Load($resp->BodyStr);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($success) {
echo $reply->StringOf('error.message'), "\n";
}
exit;
}
try {
$reply->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Expected a Gmail JSON response.\n";
exit;
}
echo 'Message: ', $ids->StringAt($i), "\n";
$h = 0;
while ($h < $reply->SizeOfArray('payload.headers')) {
$reply->I = $h;
echo $reply->StringOf('payload.headers[i].name'), ': ', $reply->StringOf('payload.headers[i].value'), "\n";
$h = $h + 1;
}
$i = $i + 1;
}
echo 'Messages inspected: ', $ids->Count, "\n";