您要做的是将数据写入一个文件,然后输出一个包含该文件链接的HTML页面。不用这样做,只需输出一个指向PHP页面的链接,该页面将输出该文件的数据。
所以你的链接看起来像这样:
<a href='download.php?messageId=42&attachment_id=69' download='picture.jpg'>Download</a>
download.php
是这样的:
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="picture.jpg"');
$attachment = $service->users_messages_attachments->get($userId, $_GET["messageId"], $_GET["attachment_id"]);
$data = $attachment->getData();
echo strtr($data, array('-' => '+', '_' => '/'));