PHP cURL request fails with 411 Length Required
411 Length RequiredShort answer
A 411 means the server refuses a request that does not state its body length. In PHP cURL it happens when you POST with no body at all, or when the request goes out with chunked transfer encoding. Send a Content-Length header (0 for an empty body), or remove the Transfer-Encoding header and give cURL a complete string body.
Fix it fast
1Find out which cause you have
Print the request headers cURL actually sent. Look for a missing Content-Length line or a Transfer-Encoding: chunked line.
curl_setopt($ch, CURLINFO_HEADER_OUT, true); $response = curl_exec($ch); echo curl_getinfo($ch, CURLINFO_HEADER_OUT);2Match what you see, then jump to its fix
- 1
The sent headers show POST but no Content-Length line, for an endpoint that takes no payload, such as an action URLA POST (or PUT) is sent with no body and no Content-LengthFix - 2
The sent headers contain Transfer-Encoding: chunked, and the server answers 411The request is sent with chunked transfer encodingFix
- 1
What does the error look like?
411 Length RequiredHTTP/1.1 411 Length Required
Cause 1 of 2
A POST (or PUT) is sent with no body and no Content-Length
1Check
A request with no content has nothing to measure, so some servers and proxies insist on an explicit zero. RFC 9110 says a POST with empty content still sends the header, as 0.
grep -n 'CURLOPT_POST\|CURLOPT_POSTFIELDS' src/CalendarClient.php2Fix
Add the header yourself with CURLOPT_HTTPHEADER and give an empty string body.
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Length: 0',
]);3Verify
Run the call again; the status should no longer be 411.
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);Confirmed by rfc-editor.org
Cause 2 of 2
The request is sent with chunked transfer encoding
1Check
cURL sends the body in chunks when the request carries a Transfer-Encoding: chunked header. A server that wants a defined length answers 411, and RFC 9110 says the client may retry with a valid Content-Length.
grep -rn 'Transfer-Encoding' src/2Fix
Remove that header (a header with an empty value after the colon removes it in cURL) and pass the whole body as one string, so the length is known.
$body = json_encode($event);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Transfer-Encoding:',
'Content-Length: ' . strlen($body),
]);3Verify
The sent headers show Content-Length and no Transfer-Encoding.
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);Confirmed by curl.se
How do I stop it coming back?
- Print CURLINFO_HEADER_OUT in your debug mode so the real request is visible.
- Build JSON bodies as a string first, then compute the length from that same string.
- Replace a hand-written header list in one place; calling CURLOPT_HTTPHEADER twice replaces the first list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
cURL error 18 in PHP: transfer closed before the whole response arrived
cURL error 18: transfer closed with 1024 bytes remaining to read
Error 18 means the server ended the response before the body was complete.
2 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix