title: Google Drive PHP API - Simple File Upload
body: <p>I am trying to write a small script to upload a local file to Google Drive, using Google Drive PHP API. The documentation is very poor maintained, but so far I am pretty sure the code should be looking like that:</p>
  
  <pre><code>&lt;?php
  
  include_once 'Google/Client.php';
  include_once 'Google/Service/Drive.php';
  include_once 'Google/Auth/OAuth2.php';
  
  $client = new Google_Client();
  
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/drive.file'));
  $client-&gt;setClientId('dfgdfgdg');
  $client-&gt;setClientSecret('dfgdfgdf');
  $client-&gt;setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
  
  
  $service = new Google_Service_Drive($client);
  
  $data = file_get_contents("a.jpg");
  
  // create and upload a new Google Drive file, including the data
  try
  {
  //Insert a file
  $file = new Google_Service_Drive_DriveFile($client);
  
  $file-&gt;setTitle(uniqid().'.jpg');
  $file-&gt;setMimeType('image/jpeg');
  
  $createdFile = $service-&gt;files-&gt;insert($file, array(
      'data' =&gt; $data,
      'mimeType' =&gt; 'image/jpeg',
      'uploadType' =&gt; 'media',
  ));
  }
  catch (Exception $e)
  {
      print $e-&gt;getMessage();
  }
  
  print_r($createdFile);
  
  ?&gt;
  </code></pre>
  
  <p>The issue is I am not able to do the authentication right (or I am doing something else wrong?). The error I received is:</p>
  
  <pre><code>HTTP Error: Unable to connect: 'fopen(compress.zlib://https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart) [function.fopen]: failed to open stream: operation failed'
  </code></pre>
  
  <p>Followed by this error:</p>
  
  <pre><code>Notice: Undefined variable: createdFile in C:\wamp\www\GoogleAPI\index.php on line 39
  </code></pre>
  
  <p>What am I doing wrong? Can you provide a simple working script of uploading a file to Google Drive using Google Drive PHP API? Thank you in advance!</p>