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 set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...