php-guzzleHow to upload a file with PHP Guzzle?
Using PHP Guzzle, you can upload a file with a POST request. The following example code will upload a file to a server:
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'http://example.com/upload', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen('/path/to/file', 'r')
]
]
]);
The output of the above code will be a response object containing the response from the server.
The code consists of the following parts:
- Creating a new Guzzle client:
$client = new GuzzleHttp\Client();
- Sending a POST request to the server:
$response = $client->request('POST', 'http://example.com/upload', [
- Specifying the file to upload:
'multipart' => [ [ 'name' => 'file', 'contents' => fopen('/path/to/file', 'r') ] ]
- Storing the response from the server:
$response = $client->request('POST', 'http://example.com/upload', [
Helpful links
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...