title: Copy remote file using Guzzle
body: <p>I'm trying to copy a remote file (image PNG, GIF, JPG ...) to my server. I use <a href="http://guzzlephp.org/" rel="nofollow noreferrer">Guzzle</a> since I sometimes get 404 with <a href="http://php.net/manual/fr/function.copy.php" rel="nofollow noreferrer">copy()</a> even if the file exists and I also need to do a basic auth. This script is within a long script launched in command triggered by a cron job.
  I'm pretty new to Guzzle and I successfully copy the image but my files have wrong mime type. I must be doing something wrong here. Please suggest me a good way to do this (including checking success/failure of copy and mime type check). If file has no mime type I would pop an error with details informations.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>$remoteFilePath = 'http://example.com/path/to/file.jpg';
  $localFilePath = '/home/www/path/to/file.jpg';
  try {
      $client = new Guzzle\Http\Client();
      $response = $client-&gt;send($client-&gt;get($remoteFilePath)-&gt;setAuth('login', 'password'));
      if ($response-&gt;getBody()-&gt;isReadable()) {
          if ($response-&gt;getStatusCode()==200) {
              // is this the proper way to retrieve mime type?
              //$mime = array_shift(array_values($response-&gt;getHeaders()-&gt;get('Content-Type')));
              file_put_contents ($localFilePath , $response-&gt;getBody()-&gt;getStream());
              return true;
          }
      }
  } catch (Exception $e) {
      return $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>When I do this my mime type is set to <em>application/x-empty</em> </p>
  
  <p>Also it looks like when status is different from 200 Guzzle will automatically throw an exception. How can I stop this behaviour and check status myself so I can custom error message?</p>
  
  <p><strong>EDIT:</strong> This was for Guzzle 3.X
  Now this is how you can do it using Guzzle v 4.X (works as well with Guzzle 6)</p>
  
  <pre><code>$client = new \GuzzleHttp\Client();
  $client-&gt;get(
      'http://path.to/remote.file',
      [
          'headers' =&gt; ['key'=&gt;'value'],
          'query'   =&gt; ['param'=&gt;'value'],
          'auth'    =&gt; ['username', 'password'],
          'save_to' =&gt; '/path/to/local.file',
      ]);
  </code></pre>
  
  <p>Or using Guzzle stream:</p>
  
  <pre><code>use GuzzleHttp\Stream;
  
  $original = Stream\create(fopen('https://path.to/remote.file', 'r')); 
  $local = Stream\create(fopen('/path/to/local.file', 'w')); 
  $local-&gt;write($original-&gt;getContents());
  </code></pre>
  
  <p>This looks great. Is there better/proper solution when using Guzzle 4?</p>