php-guzzleHow to set the content-length for a Guzzle request in PHP?
The content-length of a Guzzle request in PHP can be set by using the setBody method.
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'http://httpbin.org/post', [
'body' => 'Hello World',
'headers' => [
'Content-Length' => 11
]
]);
The output of the above code will be a response object.
Code explanation
$client = new GuzzleHttp\Client();- This creates a new Guzzle client object.$response = $client->request('POST', 'http://httpbin.org/post', [- This sends a POST request to the specified URL.'body' => 'Hello World',- This sets the body of the request to the stringHello World.'headers' => [- This sets the headers of the request.'Content-Length' => 11- This sets the content-length of the request to 11.
Helpful links
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to keep alive with Guzzle in PHP?
- What version of PHP is required for Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to add a bearer token in PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- How to stream with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
See more codes...