title: Google Drive File Downloading using PHP and api save on the server
body: <p>the file from drive open in my browser but how can i download it on my server. 
  I have tried but it's not working: </p>
  
  <pre><code>&lt;?php
  require_once "google/google-api-php-client/src/Google_Client.php";
  
  require_once "google/google-api-php-client/src/contrib/Google_DriveService.php";
  
  require_once "google/google-api-php-client/src/contrib/Google_Oauth2Service.php";
  
  require_once "google/vendor/autoload.php";
  
  $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
  $SERVICE_ACCOUNT_EMAIL = 'mobileformsafica@appspot.gserviceaccount.com';
  $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'mobileformsafica-0497d13f3902.p12';
  
  function buildService() {//function for first build up service
  global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
  
    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_AssertionCredentials(
        $SERVICE_ACCOUNT_EMAIL,
        array($DRIVE_SCOPE),
        $key);
    $client = new Google_Client();
    $client-&gt;setUseObjects(true);
    $client-&gt;setAssertionCredentials($auth);
    return new Google_DriveService($client);
  }
  /**
   * Print a file's metadata.
   *
   * @param Google_Service_Drive $service Drive API service instance.
   * @param string $fileId ID of the file to print metadata for.
   */
  function printFile($service, $fileId) {
    try {
      $file = $service-&gt;files-&gt;get($fileId);
  
      print "Title: " . $file-&gt;getTitle();
      print "Description: " . $file-&gt;getDescription();
      print "MIME type: " . $file-&gt;getMimeType();
    } catch (Exception $e) {
      print "An error occurred: " . $e-&gt;getMessage();
    }
  }
  
  /**
   * Download a file's content.
   *
   * @param Google_Service_Drive $service Drive API service instance.
   * @param File $file Drive File instance.
   * @return String The file's content if successful, null otherwise.
   */
  
  function downloadFile($service, $file) {
  $downloadUrl = $file-&gt;getDownloadUrl();
  if ($downloadUrl) {
  $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
  $httpRequest = Google_Client::$io-&gt;authenticatedRequest($request);
  if ($httpRequest-&gt;getResponseHttpCode() == 200) {
  
  return $httpRequest-&gt;getResponseBody();
  } else {
  // An error occurred.
  return null;
  }
  } else {
  // The file doesn't have any content stored on Drive.
  return null;
  }
  }
  try {
  
  //https://drive.google.com/open?id=0B9ez4Vc-n0DbWkV6VmtRZFJIbnhqU3d2QmNHTTZfWWJYZGM0
  $file_id='0B9ez4Vc-n0DbWkV6VmtRZFJIbnhqU3d2QmNHTTZfWWJYZGM0';
  
  $service=buildService();
  //printFile($service,$file_id);
  $file = $service-&gt;files-&gt;get($file_id);
  $content = downloadFile($service,$file);
  $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/filename","wb");
  fwrite($fp,$content);
  fclose($fp);
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header("Content-Disposition: attachment; filename=" . urlencode($file-&gt;getTitle()));
      header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: '.strlen($file));
  
  
  print(downloadFile($service,$file));
  
  
    } catch (Exception $e) {
    print "An error occurred1: " . $e-&gt;getMessage();
    }
  ?&gt;
  </code></pre>
  
  <p>the script also eats a lot of incoming bandwidth. how to throttle or limit speed per task?</p>
  
  <p>So i want something like a cache where i download file x once from google drive and after that the file should be served from my own server</p>
  
  <p>full script <a href="http://www.7logic.info/2017/05/google-drive-file-downloading-using-php.html" rel="nofollow noreferrer">here</a></p>