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 use PHP Guzzle to make a batch request?
- How to update PHP Guzzle?
- How to set a user agent in PHP Guzzle?
- How to stream with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to download a file with Guzzle in PHP?
- How to handle a RequestException with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- How to get the response body in PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
See more codes...