Gmail API downloading attachment in PHP
<p>I've been trying to download the file attached from this message using Gmail API. So far, I can get this info:</p>

<pre><code>Google_Service_Gmail_Message Object
(
[collection_key:protected] =&gt; labelIds
[internal_gapi_mappings:protected] =&gt; Array
    (
    )

[historyId] =&gt; 1571
[id] =&gt; 155e27d1fe295bbd
[internalDate] =&gt; 1468383698000
[labelIds] =&gt; Array
    (
        [0] =&gt; SENT
        [1] =&gt; INBOX
        [2] =&gt; IMPORTANT
    )

[payloadType:protected] =&gt; Google_Service_Gmail_MessagePart
[payloadDataType:protected] =&gt; 
[raw] =&gt; 
[sizeEstimate] =&gt; 19335
[snippet] =&gt; POPOY
[threadId] =&gt; 155e27d1fe295bbd
[modelData:protected] =&gt; Array
    (
        [payload] =&gt; Google_Service_Gmail_MessagePart Object
            (
                [collection_key:protected] =&gt; parts
                [internal_gapi_mappings:protected] =&gt; Array
                    (
                    )

                [bodyType:protected] =&gt; Google_Service_Gmail_MessagePartBody
                [bodyDataType:protected] =&gt; 
                [filename] =&gt; 
                [headersType:protected] =&gt; Google_Service_Gmail_MessagePartHeader
                [headersDataType:protected] =&gt; array
                [mimeType] =&gt; multipart/mixed
                [partId] =&gt; 
                [partsType:protected] =&gt; Google_Service_Gmail_MessagePart
                [partsDataType:protected] =&gt; array
                [modelData:protected] =&gt; Array
                    (
                        [headers] =&gt; Array
                            (
                                [0] =&gt; Array
                                    (
                                        [name] =&gt; MIME-Version
                                        [value] =&gt; 1.0
                                    )

                                [1] =&gt; Array
                                    (
                                        [name] =&gt; Received
                                        [value] =&gt; by 10.140.92.230 with HTTP; Tue, 12 Jul 2016 21:21:38 -0700 (PDT)
                                    )

                                [2] =&gt; Array
                                    (
                                        [name] =&gt; Date
                                        [value] =&gt; Wed, 13 Jul 2016 12:21:38 +0800
                                    )

                                [3] =&gt; Array
                                    (
                                        [name] =&gt; Delivered-To
                                        [value] =&gt; maingabbytest@gmail.com
                                    )

                                [4] =&gt; Array
                                    (
                                        [name] =&gt; Message-ID
                                        [value] =&gt; 
                                    )

                                [5] =&gt; Array
                                    (
                                        [name] =&gt; Subject
                                        [value] =&gt; test w/ attachment
                                    )

                                [6] =&gt; Array
                                    (
                                        [name] =&gt; From
                                        [value] =&gt; Chris Jason Barredo 
                                    )

                                [7] =&gt; Array
                                    (
                                        [name] =&gt; To
                                        [value] =&gt; Chris Jason Barredo 
                                    )

                                [8] =&gt; Array
                                    (
                                        [name] =&gt; Content-Type
                                        [value] =&gt; multipart/mixed; boundary=001a11c13ea0c1971805377cb89b
                                    )

                            )

                        [body] =&gt; Array
                            (
                                [size] =&gt; 0
                            )

                        [parts] =&gt; Array
                            (
                                [0] =&gt; Google_Service_Gmail_MessagePart Object
                                    (
                                        [collection_key:protected] =&gt; parts
                                        [internal_gapi_mappings:protected] =&gt; Array
                                            (
                                            )

                                        [bodyType:protected] =&gt; Google_Service_Gmail_MessagePartBody
                                        [bodyDataType:protected] =&gt; 
                                        [filename] =&gt; 
                                        [headersType:protected] =&gt; Google_Service_Gmail_MessagePartHeader
                                        [headersDataType:protected] =&gt; array
                                        [mimeType] =&gt; multipart/alternative
                                        [partId] =&gt; 
                                        [partsType:protected] =&gt; Google_Service_Gmail_MessagePart
                                        [partsDataType:protected] =&gt; array
                                        [modelData:protected] =&gt; Array
                                            (
                                                [headers] =&gt; Array
                                                    (
                                                        [0] =&gt; Array
                                                            (
                                                                [name] =&gt; Content-Type
                                                                [value] =&gt; multipart/alternative; boundary=001a11c13ea0c1970b05377cb899
                                                            )

                                                    )

                                                [body] =&gt; Array
                                                    (
                                                        [size] =&gt; 0
                                                    )

                                                [parts] =&gt; Array
                                                    (
                                                        [0] =&gt; Array
                                                            (
                                                                [partId] =&gt; 0.0
                                                                [mimeType] =&gt; text/plain
                                                                [filename] =&gt; 
                                                                [headers] =&gt; Array
                                                                    (
                                                                        [0] =&gt; Array
                                                                            (
                                                                                [name] =&gt; Content-Type
                                                                                [value] =&gt; text/plain; charset=UTF-8
                                                                            )

                                                                    )

                                                                [body] =&gt; Array
                                                                    (
                                                                        [size] =&gt; 7
                                                                        [data] =&gt; UE9QT1kNCg==
                                                                    )

                                                            )

                                                        [1] =&gt; Array
                                                            (
                                                                [partId] =&gt; 0.1
                                                                [mimeType] =&gt; text/html
                                                                [filename] =&gt; 
                                                                [headers] =&gt; Array
                                                                    (
                                                                        [0] =&gt; Array
                                                                            (
                                                                                [name] =&gt; Content-Type
                                                                                [value] =&gt; text/html; charset=UTF-8
                                                                            )

                                                                    )

                                                                [body] =&gt; Array
                                                                    (
                                                                        [size] =&gt; 28
                                                                        [data] =&gt; PGRpdiBkaXI9Imx0ciI-UE9QT1k8L2Rpdj4NCg==
                                                                    )

                                                            )

                                                    )

                                            )

                                        [processed:protected] =&gt; Array
                                            (
                                            )

                                    )

                                [1] =&gt; Google_Service_Gmail_MessagePart Object
                                    (
                                        [collection_key:protected] =&gt; parts
                                        [internal_gapi_mappings:protected] =&gt; Array
                                            (
                                            )

                                        [bodyType:protected] =&gt; Google_Service_Gmail_MessagePartBody
                                        [bodyDataType:protected] =&gt; 
                                        [filename] =&gt; popoy.jpeg
                                        [headersType:protected] =&gt; Google_Service_Gmail_MessagePartHeader
                                        [headersDataType:protected] =&gt; array
                                        [mimeType] =&gt; image/jpeg
                                        [partId] =&gt; 1
                                        [partsType:protected] =&gt; Google_Service_Gmail_MessagePart
                                        [partsDataType:protected] =&gt; array
                                        [modelData:protected] =&gt; Array
                                            (
                                                [headers] =&gt; Array
                                                    (
                                                        [0] =&gt; Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [name] =&gt; Content-Type
                                                                [value] =&gt; image/jpeg; name="popoy.jpeg"
                                                                [modelData:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [processed:protected] =&gt; Array
                                                                    (
                                                                    )

                                                            )

                                                        [1] =&gt; Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [name] =&gt; Content-Disposition
                                                                [value] =&gt; attachment; filename="popoy.jpeg"
                                                                [modelData:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [processed:protected] =&gt; Array
                                                                    (
                                                                    )

                                                            )

                                                        [2] =&gt; Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [name] =&gt; Content-Transfer-Encoding
                                                                [value] =&gt; base64
                                                                [modelData:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [processed:protected] =&gt; Array
                                                                    (
                                                                    )

                                                            )

                                                        [3] =&gt; Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [name] =&gt; X-Attachment-Id
                                                                [value] =&gt; f_iqkdq8dk0
                                                                [modelData:protected] =&gt; Array
                                                                    (
                                                                    )

                                                                [processed:protected] =&gt; Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                                [body] =&gt; Google_Service_Gmail_MessagePartBody Object
                                                    (
                                                        [internal_gapi_mappings:protected] =&gt; Array
                                                            (
                                                            )

                                                        [attachmentId] =&gt; ANGjdJ-18nbgORhdtkl3nD4dezCwSPODA0qgR7UsIeoGWQp8xIC4VFCvxP0xRUkMGQYm1uAqbMTP8L_wMgZFA1IEy5FXwgXeJOW9UmJGB87SuBIjNezVlXOY-6OhbxMktWTfgkiGx4Eb6dhFkrY-iWvbOWEjmWajiT54oj0SoaHQvNXGI-HmZZ2L6m62DHSPQCWn0qSRZsMcxddXSlczatQtCcjvMR7Hy9rhoNxe2Rd2bf_jU2PyHbsQxtvaWvp-lx_fnBrcWX5pjWhW5x3XWzrxhgRIF4xJfGa_fqV7g-G750vqbj5vRRAWX_cPvuU
                                                        [data] =&gt; 
                                                        [size] =&gt; 18308
                                                        [modelData:protected] =&gt; Array
                                                            (
                                                            )

                                                        [processed:protected] =&gt; Array
                                                            (
                                                            )

                                                    )

                                            )

                                        [processed:protected] =&gt; Array
                                            (
                                                [body] =&gt; 1
                                                [headers] =&gt; 1
                                            )

                                    )

                            )

                    )

                [processed:protected] =&gt; Array
                    (
                        [parts] =&gt; 1
                    )

            )

    )

[processed:protected] =&gt; Array
    (
        [payload] =&gt; 1
    )

)
</code></pre>

<p>So far, when I am trying to download it, I am getting the file, but when I open it, I am not able to see the file. Looks like a corrupted file:</p>

<pre><code>public function download(){

    $file = 'popoy.jpg';

    header('Content-Type: application/download');
    header('Content-Disposition: attachment; filename="'.$file.'"');
    header("Content-Length: " . filesize($file));
}
</code></pre>