Skip to content

Instantly share code, notes, and snippets.

@romainneutron
Created April 8, 2013 21:59
Show Gist options
  • Save romainneutron/5340930 to your computer and use it in GitHub Desktop.
Save romainneutron/5340930 to your computer and use it in GitHub Desktop.
Download large files using Guzzle
<?php
use Guzzle\Http\Client;
require __DIR__ . '/vendor/autoload.php';
$tmpFile = tempnam(sys_get_temp_dir(), 'guzzle-download');
$handle = fopen($tmpFile, 'w');
$client = new Client('', array(
Client::CURL_OPTIONS => array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FILE' => $handle
)
));
$client->get('http://domain.tld/large-file.mp4')->send();
fclose($handle);
@jmauerhan
Copy link

👍 thanks for this! I had to figure out how to test downloading a zip file and then I needed to find it and unzip it. Your code helped a ton.

@yarikdot
Copy link

Doesn't work anymore with the latest version (6.1.11):

PHP Fatal error:  Undefined class constant 'CURL_OPTIONS' in ....

@timgws
Copy link

timgws commented Mar 31, 2016

Check out the sink documentation here:

http://docs.guzzlephp.org/en/latest/request-options.html#sink

@gnat42
Copy link

gnat42 commented Feb 5, 2018

How should this work if we use the HttpClient interface type hint? I should be more specific, if we're using php-http/httplug for a generic interface, how do you use the sink option?

@devNoiseConsulting
Copy link

I forked this gist to use Guzzle 6.3. https://gist.github.com/devNoiseConsulting/fb6195fbd09bfb2c1f81367dd9e727ed
I still need to test with large files, but the code will write the file to disk.

@muhammadsikandarkhatri
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment