title: File download from Google API v3 in PHP
body: <p>Have been looking all over for downloading a file from google drive with the API as mentioned <a href="https://developers.google.com/drive/v3/web/manage-downloads" rel="nofollow">here</a>. In the flow, was able to pass the authentication of the user, and was able to list the files with the below code. </p>
  
  <pre><code> echo "&lt;br&gt;" . count($results-&gt;getFiles()). " File(s) found" ; 
          echo "&lt;hr&gt;";
          foreach ($results-&gt;getFiles() as $file) {
              echo "File Name is " . $file-&gt;getName() . " And file ID is ". $file-&gt;getId() . " Type is ".$file-&gt;getMimeType() ."&lt;br&gt;&lt;pre&gt;";
              print_r($file);
              echo "&lt;/pre&gt;&lt;hr&gt;";
          }
  </code></pre>
  
  <p>And the output is something like </p>
  
  <pre><code>100 File(s) found
  File Name is invoice receipt.pdf And file ID is 0B5CWXJTNwALMOEJkdzFvMVVsVFE Type is application/pdf
  Google_Service_Drive_DriveFile Object
  (
      [collection_key:protected] =&gt; spaces
      [appProperties] =&gt; 
      [capabilitiesType:protected] =&gt; Google_Service_Drive_DriveFileCapabilities
      [capabilitiesDataType:protected] =&gt; 
      [contentHintsType:protected] =&gt; Google_Service_Drive_DriveFileContentHints
      [contentHintsDataType:protected] =&gt; 
      [createdTime] =&gt; 
      [description] =&gt; 
      [explicitlyTrashed] =&gt; 
      [fileExtension] =&gt; 
      [folderColorRgb] =&gt; 
      [fullFileExtension] =&gt; 
      [headRevisionId] =&gt; 
      [iconLink] =&gt; 
      [id] =&gt; 0B5CWXJTNwALMOEJkdzFvMVVsVFE
      [imageMediaMetadataType:protected] =&gt; Google_Service_Drive_DriveFileImageMediaMetadata
      [imageMediaMetadataDataType:protected] =&gt; 
      [isAppAuthorized] =&gt; 
      [kind] =&gt; drive#file
      [lastModifyingUserType:protected] =&gt; Google_Service_Drive_User
      [lastModifyingUserDataType:protected] =&gt; 
      [md5Checksum] =&gt; 
      [mimeType] =&gt; application/pdf
      [modifiedByMeTime] =&gt; 
      [modifiedTime] =&gt; 
      [name] =&gt; invoice receipt.pdf
      [originalFilename] =&gt; 
      [ownedByMe] =&gt; 
      [ownersType:protected] =&gt; Google_Service_Drive_User
      [ownersDataType:protected] =&gt; array
      [parents] =&gt; 
      [permissionsType:protected] =&gt; Google_Service_Drive_Permission
      [permissionsDataType:protected] =&gt; array
      [properties] =&gt; 
      [quotaBytesUsed] =&gt; 
      [shared] =&gt; 
      [sharedWithMeTime] =&gt; 
      [sharingUserType:protected] =&gt; Google_Service_Drive_User
      [sharingUserDataType:protected] =&gt; 
      [size] =&gt; 
      [spaces] =&gt; 
      [starred] =&gt; 
      [thumbnailLink] =&gt; 
      [trashed] =&gt; 
      [version] =&gt; 
      [videoMediaMetadataType:protected] =&gt; Google_Service_Drive_DriveFileVideoMediaMetadata
      [videoMediaMetadataDataType:protected] =&gt; 
      [viewedByMe] =&gt; 
      [viewedByMeTime] =&gt; 
      [viewersCanCopyContent] =&gt; 
      [webContentLink] =&gt; 
      [webViewLink] =&gt; 
      [writersCanShare] =&gt; 
      [internal_gapi_mappings:protected] =&gt; Array
          (
          )
  
      [modelData:protected] =&gt; Array
          (
          )
  
      [processed:protected] =&gt; Array
          (
          )
  
  )
  </code></pre>
  
  <p>Now, trying to apply the download code as below</p>
  
  <pre><code>$fileId = '0B5CWXJTNwALMOEJkdzFvMVVsVFE';
          $content = $drive_service-&gt;files-&gt;get($fileId,  array(
              'alt' =&gt; 'media' ));
          echo "&lt;pre&gt;";
          print_r($content-&gt;getHeaders());
          foreach ($content-&gt;getHeaders() as $name =&gt; $values) {
  
          header($name . ': ' . implode(', ', $values[0]));
          }
  </code></pre>
  
  <p>The file is not downloaded but was able to see the output like below</p>
  
  <pre><code>Array
  (
      [X-GUploader-UploadID] =&gt; Array
          (
              [0] =&gt; AEnB2UrDOQ-mYxNd5tE5ctVr01jVTnh3ZykKviCBnEOjDRGtT_EIT2nbHLEM_f-2pZ22anDJpqMzbISzhcTTZY3PgsDj69PE-w
          )
  
      [Content-Type] =&gt; Array
          (
              [0] =&gt; application/pdf
          )
  
      [Content-Disposition] =&gt; Array
          (
              [0] =&gt; attachment
          )
  
      [Vary] =&gt; Array
          (
              [0] =&gt; Origin
              [1] =&gt; X-Origin
          )
  
      [Expires] =&gt; Array
          (
              [0] =&gt; Tue, 23 Aug 2016 13:20:18 GMT
          )
  
      [Date] =&gt; Array
          (
              [0] =&gt; Tue, 23 Aug 2016 13:20:18 GMT
          )
  
      [Cache-Control] =&gt; Array
          (
              [0] =&gt; private, max-age=0, must-revalidate
          )
  
      [Content-Length] =&gt; Array
          (
              [0] =&gt; 80277
          )
  
      [X-Goog-Hash] =&gt; Array
          (
              [0] =&gt; crc32c=5yKN2g==
          )
  
      [Server] =&gt; Array
          (
              [0] =&gt; UploadServer
          )
  
      [Alternate-Protocol] =&gt; Array
          (
              [0] =&gt; 443:quic
          )
  
      [Alt-Svc] =&gt; Array
          (
              [0] =&gt; quic=":443"; ma=2592000; v="35,34,33,32,31,30"
          )
  
  )
  </code></pre>
  
  <p><strong>Question :</strong> How do I download the file?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on paulo answer, have created two files as below.This is working for  image/png,  application/octet-stream,   application/pdf types, however have issues with zip/rar files (error : <em>This site can’t be reached</em>)</p>
  
  <p>test.php</p>
  
  <pre><code>&lt;?php
  require_once __DIR__ . '/vendor/autoload.php';
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setAuthConfigFile('client_secrets.json');
  $client-&gt;addScope(Google_Service_Drive::DRIVE);
  
  if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) 
  {
      $client-&gt;setAccessToken($_SESSION['access_token']);
      $drive_service = new Google_Service_Drive($client);
      $results = $drive_service-&gt;files-&gt;listFiles();
      if (count($results-&gt;getFiles()) == 0) 
      {
          echo "No files found";
      } 
      else 
      {
          echo "&lt;br&gt;" . count($results-&gt;getFiles()). " File(s) found" ; 
          echo "&lt;br&gt;&lt;table&gt;&lt;thead&gt;&lt;th&gt;File Name&lt;/th&gt;&lt;th&gt;File Type&lt;/th&gt;&lt;th&gt;Download Link&lt;/th&gt;&lt;/thead&gt;&lt;tbody&gt;";
          foreach ($results-&gt;getFiles() as $file) {
              echo "&lt;tr&gt;&lt;td&gt;" . $file-&gt;getName() . "&lt;/td&gt;&lt;td&gt;" . $file-&gt;getMimeType() ."&lt;/td&gt;&lt;td&gt;&lt;a href='gdrivedownlaod.php?fileid=". $file-&gt;getId() . "&amp;type=" . $file-&gt;getMimeType() . "&amp;name=" . $file-&gt;getName() . "' target='blank'&gt;Download&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";// ."&lt;br&gt;&lt;pre&gt;";
              }
              echo "&lt;/tbody&gt;&lt;/table&gt;";
      }
  
  } 
  else 
  {
      $redirect_uri = 'https://abcd.co/oauth2callback';
      echo "Hello, &lt;a href='" . $redirect_uri . "'&gt;Log in with Drive&lt;/a&gt;";
  }
  </code></pre>
  
  <p>and gdrivedownlaod.php as </p>
  
  <pre><code>&lt;?php
  require_once __DIR__ . '/vendor/autoload.php';
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setAuthConfigFile('client_secrets.json');
  $client-&gt;addScope(Google_Service_Drive::DRIVE);
  
  if (isset($_GET['name']) &amp;&amp; isset($_GET['type'])) {
  //echo "We will download " . $_GET['name'] . " type " . $_GET['type'] . " with file id " . $_GET['fileid'];
      $fileId=$_GET['fileid'];
      if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) 
      {
          $client-&gt;setAccessToken($_SESSION['access_token']);
          $drive_service = new Google_Service_Drive($client);
          $content = $drive_service-&gt;files-&gt;get($fileId,  array(
              'alt' =&gt; 'media' ));
                  $fileName = $_GET['name'];
          $headers = $content-&gt;getHeaders();
  
          foreach ($headers as $name =&gt; $values) {
              header($name . ': ' . implode(', ', $values));
          }
          header('Content-Disposition: attachment; filename="' . $fileName . '"');
          echo $content-&gt;getBody();
          //echo "&lt;script&gt;window.close();&lt;/script&gt;";
          exit;
      }
      else
      {
          session_destroy();
          session_unset();
          header('Location: test.php'); 
      }
  
  }
  else
  echo "incorrect files ";
  </code></pre>