php-guzzleHow to download a file with Guzzle in PHP?
Using Guzzle in PHP to download a file is easy. Here is an example code block to demonstrate how to do it:
<?php
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com/file.zip', [
'sink' => '/path/to/file.zip'
]);
This code will download the file from http://example.com/file.zip
and save it to /path/to/file.zip
.
The code consists of the following parts:
$client = new GuzzleHttp\Client();
- creates a new Guzzle client.$response = $client->request('GET', 'http://example.com/file.zip', [
- sends a GET request to the specified URL.'sink' => '/path/to/file.zip'
- specifies the path to save the file to.]);
- closes the array of options.
For more information, please refer to the Guzzle documentation.
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to send multipart requests with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- How to use cookies with Guzzle in PHP?
- How to add an authorization header bearer in PHP Guzzle?
- How to stream with PHP Guzzle?
- What version of PHP is required for Guzzle?
See more codes...