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 keep alive with Guzzle in PHP?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- How to set a user agent in PHP Guzzle?
- How to stream with PHP Guzzle?
- How to send multipart requests with Guzzle in PHP?
- How to use multipart/form-data with 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 multiple requests with Guzzle in PHP?
- How to use PHP Guzzle to make a batch request?
See more codes...