title: Download file from google drive api to my server using php
body: <p>1 - I have configure google picker and it is working fine and I select the file from picker and get the file id.</p>
  
  <p>2 - After refresh token etc all process I get the file metadata and get the file export link</p>
  
  <pre><code>$downloadExpLink = $file-&gt;getExportLinks();
   $downloadUrl = $downloadExpLink['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
  </code></pre>
  
  <p>3 - After that I use this</p>
  
  <pre><code>if ($downloadUrl) {
       $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
  
          $httpRequest = Google_Client::$io-&gt;authenticatedRequest($request);
          if ($httpRequest-&gt;getResponseHttpCode() == 200) 
          {
              $content = $httpRequest-&gt;getResponseBody();
               print_r($content);
  
  
          } else {
          // An error occurred.
           return null;
       }
  </code></pre>
  
  <p>and get this response</p>
  
  <pre><code> [responseBody:protected] =&gt; PK��DdocProps/app.xml���
  �0D���k�I[ѫ��m
  ��!����A={���}�
  2G�Z�g�V��Bľ֧�n�Ҋ�ap!����fb�d����k}Ikc�_`t&lt;+�(�NJ̽�����#��EU-
  �0@���P����........
  </code></pre>
  
  <p>4 - I use some cURL functions to get file from google drive and save it to server. IN server directory a file created but cropped. I use this code</p>
  
  <pre><code>$downloadExpLink = $file-&gt;getExportLinks();
  $downloadUrl = $downloadExpLink['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
  
  //$downloadUrl value is
  /*https://docs.google.com/feeds/download/documents/export/Export?id=1CEt1ya5kKLtgK************IJjDEY5BdfaGI&amp;exportFormat=docx*/
  </code></pre>
  
  <p>When I put this url into browser it will download file successfully but when I use this url to fetch file with cURL or any php code and try to save it on server it saves corrupted file.</p>
  
  <pre><code>$ch = curl_init();
  $source = $downloadUrl;
  
  curl_setopt($ch, CURLOPT_URL, $source);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec ($ch);
  curl_close ($ch);
  
  $destination = "test/afile5.docx";
  $file = fopen($destination, "w+");
  fputs($file, $data);
  fclose($file);
  </code></pre>
  
  <p>It result a corrupted file stored on server but whe I use this code to get any file other then google drive I download it successfully on server.</p>
  
  <p>Can any one please help that how to download file from $downloadUrl to my server using php ?</p>