php curl 错误 18 - 传输关闭,剩余读取数据未完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1759956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:44:05  来源:igfitidea点击:

curl error 18 - transfer closed with outstanding read data remaining

phpcurl

提问by David

when retrieving data from a URL using curl, I sometimes (in 80% of the cases) get

当使用 curl 从 URL 检索数据时,我有时(在 80% 的情况下)得到

error 18: transfer closed with outstanding read data remaining

错误 18:传输关闭,剩余未完成的读取数据

Part of the returned data is then missing. The weird thing is that this does never occur when the CURLOPT_RETURNTRANSFER is set to false, that is the curl_exec function doesn't return the data but displays the content directly.

然后丢失了部分返回的数据。奇怪的是,当 CURLOPT_RETURNTRANSFER 设置为 false 时,这永远不会发生,即 curl_exec 函数不返回数据而是直接显示内容。

What could be the problem? Can I set some of the options to avoid such behaviour?

可能是什么问题呢?我可以设置一些选项来避免这种行为吗?

采纳答案by Christophe Eblé

I bet this is related to a wrong Content-Lengthheader sent by the peer. My advice is to let curl set the length by itself.

我敢打赌这与Content-Length对等方发送的错误标头有关。我的建议是让 curl 自己设置长度。

回答by Daniel Stenberg

The error string is quite simply exactly what libcurl sees: since it is receiving a chunked encoding stream it knows when there is data left in a chunk to receive. When the connection is closed, libcurl knows that the last received chunk was incomplete. Then you get this error code.

错误字符串非常简单,正是 libcurl 所看到的:因为它正在接收一个分块的编码流,它知道何时在一个块中还有数据要接收。当连接关闭时,libcurl 知道最后接收到的块是不完整的。然后你会得到这个错误代码。

There's nothing you can do to avoid this error with the request unmodified, but you cantry to work around it by issuing a HTTP 1.0 request instead (since chunked encoding won't happen then) but the fact is that this is most likely a flaw in the server or in your network/setup somehow.

对于未修改的请求,您无法避免此错误,但是您可以尝试通过发出 HTTP 1.0 请求来解决此问题(因为那时不会发生分块编码),但事实是这很可能是一个缺陷以某种方式在服务器或您的网络/设置中。

回答by jcsahnwaldt says GoFundMonica

I had the same problem, but managed to fix it by suppressing the 'Expect: 100-continue' header that cURL usually sends (the following is PHP code, but should work similarly with other cURL APIs):

我遇到了同样的问题,但设法通过抑制 cURL 通常发送的 'Expect: 100-continue' 标头来修复它(以下是 PHP 代码,但应该与其他 cURL API 类似):

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));

By the way, I am sending calls to the HTTP server that is included in the JDK 6 REST stuff, which has all kinds of problems. In this case, it first sends a 100 response, and then with some requests doesn't send the subsequent 200 response correctly.

顺便说一下,我正在向包含在 JDK 6 REST 内容中的 HTTP 服务器发送调用,该服务器存在各种问题。在这种情况下,它首先发送 100 响应,然后某些请求没有正确发送后续的 200 响应。

回答by rambii

Seeing this error during the use of Guzzle as well. The following header fixed it for me:

在使用 Guzzle 的过程中也看到这个错误。以下标题为我修复了它:

'headers' => [
    'accept-encoding' => 'gzip, deflate',
],

I issued the request with Postman which gave me a complete response and no error. Then I started adding the headers that Postman sends to the Guzzle request and this was the one that fixed it.

我向邮递员发出了请求,邮递员给了我完整的回复,没有错误。然后我开始添加 Postman 发送到 Guzzle 请求的标头,这就是修复它的标头。

回答by koljaTM

I got this error when my server process got an exception midway during generating the response and simply closed the connection without saying goodbye. curl still expected data from the connection and complained (rightfully).

当我的服务器进程在生成响应的中途遇到异常并且简单地关闭连接而不说再见时,我收到了这个错误。curl 仍然期望来自连接的数据并抱怨(理所当然)。

回答by Jorge Blanco

I had this problem working with pycurl and I solved it using

我在使用 pycurl 时遇到了这个问题,我使用

c.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_0) 

like Eric Caronsays.

就像埃里克·卡伦说的那样。

回答by Darklighter

I got this error when i was accidentally downloading a file onto itself.
(I had created a symlink in an sshfs mount of the remote directory to make it available for download, forgot to switch the working directory, and used -OJ).

当我不小心将文件下载到自身时出现此错误。
(我在远程目录的 sshfs 挂载中创建了一个符号链接以使其可供下载,忘记切换工作目录,然后使用了-OJ)。

I guess it won't really ?help? you when you read this, since it means your file got trashed.

我想它不会真的?帮助?当你阅读这篇文章时,因为这意味着你的文件被破坏了。

回答by Parshin Dmitry

I've solved this error by this way.

我已经通过这种方式解决了这个错误。

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, 'http://www.someurl/' );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 30);
ob_start();
$response = curl_exec ( $ch );
$data = ob_get_clean();
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200 ) success;

Error still occurs, but I can handle response data in variable.

错误仍然发生,但我可以处理变量中的响应数据。

回答by FeiXia

Encountered similar issue, my server is behind nginx. There's no error in web server's (Python flask) log, but some error messsage in nginx log.

遇到类似问题,我的服务器在 nginx 后面。Web 服务器(Python Flask)日志中没有错误,但 nginx 日志中有一些错误消息。

[crit] 31054#31054: *269464 open() "/var/cache/nginx/proxy_temp/3/45/0000000453" failed (13: Permission denied) while reading upstream

[crit] 31054#31054: *269464 open() "/var/cache/nginx/proxy_temp/3/45/0000000453" 在读取上游时失败(13:权限被拒绝)

I fixed this issue by correcting the permission of directory:

我通过更正目录的权限解决了这个问题:

/var/cache/nginx