我已经配置好一切,并且运行良好, 作为测试,我试图获取一些经过过滤的电子邮件并将其更新为已读。单个更新正在使用以下代码
$mods = new Google_Service_Gmail_ModifyMessageRequest();
$mods->setRemoveLabelIds('UNREAD');
foreach ($messages as $message) {
try {
$message = $service->users_messages->modify($userId, $messageId, $mods);
print 'Message with ID: ' . $messageId . ' successfully modified.';
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
print 'Message with ID: ' . $message->getId() . '<br/>';
}
我试图参考文档以了解如何设置参数以批量修改功能。但是没有任何细节。所以我阅读了Google客户端库中的源文件,发现它只接受以下三个参数。
类Google_Service_Gmail_Resource_UsersMessages扩展了Google_Service_Resource
public function batchModify($userId, Google_Service_Gmail_BatchModifyMessagesRequest $postBody, $optParams = array())
所以我尝试了以下代码
$mods = new Google_Service_Gmail_BatchModifyMessagesRequest();
$mods->setRemoveLabelIds(['UNREAD']);
$msgIdString = '';
foreach ($messages as $message) {
$id = $message->getId();
$msgIdString .= " $id";
}
echo $msgIdString;
try {
$message = $service->users_messages->batchModify($user, $mods);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
但这给我一个错误
An error occurred: { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "No message ids specified." } ], "code": 400, "message": "No message ids specified." } }
所以我的问题是我仍然找不到应该放置消息ID的位置。
答案 0 :(得分:0)
在浪费一些时间后,我找到了它。我在这里提到的是,因为它将对将来的某个人有所帮助,实际上,ID应该是数组而不是字符串。那是我以前做过的错误。所以我将所有ID推入$ msgIdArray并发送。现在它正在工作。
foreach ($messages as $message) {
$id = $message->getId();
$msgIdString .= " '$id', ";
array_push($msgIdArray, $id);
}
echo $msgIdString;
if (!empty($msgIdArray)) {
$mods = new Google_Service_Gmail_BatchModifyMessagesRequest();
$mods->setRemoveLabelIds(['UNREAD']);
$mods->setIds($msgIdArray);
try {
$message = $service->users_messages->batchModify($user, $mods);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}