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
- What version of PHP is required for Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to install PHP Guzzle without Composer?
- How to use PHP Guzzle to make a batch request?
- How to stream with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to convert a Guzzle request to a cURL request in PHP?
- How to retry requests with PHP Guzzle?
See more codes...