Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

cURL error 18 in PHP: transfer closed before the whole response arrived

ErrorcURL error 18: transfer closed with outstanding read data remaining

Short 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.

  • Applies to PHP 8.3, PHP 8.4, curl 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/export
  2. 2Match what you see, then jump to its fix

    1. 1Error text ends with 'outstanding read data remaining' and the headers show Transfer-Encoding: chunked.A chunked response was cut before its final chunkFix
    2. 2Error text says 'transfer closed with N bytes remaining to read'.The Content-Length header is larger than the body that was sentFix

What does the error look like?

  • cURL error 18: transfer closed with outstanding read data remaining
  • cURL error 18: transfer closed with 1024 bytes remaining to read
  • curl: (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/export

3Verify

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/export

Confirmed by rfc-editor.org

Did this fix it?

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 -c

2Fix

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 -c

Confirmed by rfc-editor.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.