从Google mail API解码PHP中的邮件附件
我正在使用GoogleAPI客户端库和Gmail API来下载和处理一些电子邮件 我收到的信息没有任何问题:从Google mail API解码PHP中的邮件附件,php,google-api-php-client,gmail-api,Php,Google Api Php Client,Gmail Api,我正在使用GoogleAPI客户端库和Gmail API来下载和处理一些电子邮件 我收到的信息没有任何问题: $optParamsGet = []; $optParamsGet['format'] = 'full'; // Display message in payload $message = $service->users_messages->get('me',$id,$optParamsGet); $messagePayload = $message->getPaylo
$optParamsGet = [];
$optParamsGet['format'] = 'full'; // Display message in payload
$message = $service->users_messages->get('me',$id,$optParamsGet);
$messagePayload = $message->getPayload();
然后,我处理getParts()数组并找到第一个附件,它是一个没有名称的.eml文件
[modelData:protected] => Array
(
[headers] => Array
(
[0] => Array
(
[name] => Content-Type
[value] => message/rfc822
)
[1] => Array
(
[name] => Content-Disposition
[value] => attachment; creation-date="Fri, 24 Jul 2015 00:20:48 GMT"; modification-date="Fri, 24 Jul 2015 00:20:48 GMT"
)
)
[body] => Array
(
[attachmentId] => ANGjdJ_TfJlMTssCNackUcQfT...mpppKiT8sSg
[size] => 37099
)
)
我的问题是:如何取消“attachmentId”字符串的编码?
我以为这是base64编码,但是命令
echo base64_decode($parts[1]->body->attachmentId);
只是吐出二进制数据
我遗漏了什么?附件ID不是您附件的数据,而是附件的标识符。要获取附件,您需要使用messageId和attachmentId再执行一个请求,如下所示:
$attachmentData= $service->users_messages_attachments->get('me', $messageId, $attachmentId);
// Replace all '-' with '+' and '_' with '/' to make the url-safe
// base64 encoded data to regular base64.
$decodedData = strtr($attachmentData, array('-' => '+', '_' => '/'));