cURL error 18 in PHP: transfer closed before the whole response arrived
cURL error 18: transfer closed with outstanding read data remainingShort answer
Error 18 means the server ended the response before the body was complete. For a chunked response that is a missing final chunk; for a fixed length it is fewer bytes than Content-Length promised. The fault is on the server or proxy side. Run curl -v against the origin to see which one cuts the connection.
Fix it fast
1Find out which cause you have
Repeat the request with the command line curl in verbose mode and look at the response headers. Transfer-Encoding: chunked points to the first cause, a Content-Length header to the second.
curl -v -o /dev/null https://api.example.com/export2Match what you see, then jump to its fix
What does the error look like?
cURL error 18: transfer closed with outstanding read data remainingcURL error 18: transfer closed with 1024 bytes remaining to readcurl: (18) transfer closed with outstanding read data remaining
Cause 1 of 2
A chunked response was cut before its final chunk
1Check
A chunked body is only complete when the zero-sized terminating chunk arrives. If the connection closes before it, the client must treat the message as incomplete, and curl reports error 18. Something between you and the app closed the connection: the app crashed, hit a time limit, or a proxy dropped it.
curl -sv -o /dev/null https://api.example.com/export 2>&1 | grep -i "transfer-encoding\|closed"
curl -sv -o /dev/null http://127.0.0.1:8080/export 2>&1 | grep -i "closed"2Fix
Compare the request through the public address with the request straight to the app. If only the public one fails, fix the proxy or load balancer in front. If both fail, read the application's error log for a crash or a timeout in the middle of streaming.
# run the same request straight at the app and watch its own error log
curl -sv -o /dev/null http://127.0.0.1:8080/export3Verify
The full download finishes and the byte count matches.
curl -s -o /tmp/out.bin -w "%{size_download} %{http_code}\n" https://api.example.com/exportConfirmed by rfc-editor.org
Cause 2 of 2
The Content-Length header is larger than the body that was sent
1Check
When a response declares a length and the connection closes earlier, the recipient must consider the message incomplete. In curl the check compares the bytes received with the declared size at the end of the transfer. Typical origin: code that sets a Content-Length by hand and then outputs something shorter, or a layer that rewrites the body after the length was computed.
curl -sI https://api.example.com/export | grep -i content-length
curl -s https://api.example.com/export | wc -c2Fix
Stop setting Content-Length yourself unless you are sure of the exact byte count, and make sure nothing changes the body after the header is produced.
// remove manual length headers in your PHP endpoint
// header('Content-Length: ' . $size);3Verify
Declared length and received bytes are equal.
curl -sI https://api.example.com/export | grep -i content-length
curl -s https://api.example.com/export | wc -cConfirmed by rfc-editor.org
How do I stop it coming back?
- When a download fails, repeat it once with curl -v directly against the origin to separate app faults from proxy faults.
- Do not set Content-Length by hand in PHP endpoints that stream or transform output.
- Log script crashes and timeouts on the server; a response cut halfway is usually a symptom of one.
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
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 fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix