GuzzleHttp\Client cURL 错误 18:传输已关闭

GuzzleHttp\Client cURL error 18: transfer closed

我正在使用 GuzzleHttp\Client Laravel 6,当我尝试从 API 获取数据时出现此错误,它在邮递员

上运行良好

这是我的代码

try {
        $client = new Client();
        $result = $client->request("POST", $this->url,
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->ApiToken
                ],
            ]);
        $content = $result->getBody()->getContents();
        return [
            'bool' => true,
            'message' => 'Success',
            'result' => $content,
        ];

    }  catch (\Exception  $exception) {
        return [
            'bool' => false,
            'message' => $exception->getMessage()
        ];
    }

收到此错误

cURL error 18: transfer closed with outstanding read data remaining (see https://curl.haxx.se/libcurl/c/libcurl-errors.html

我已经解决了这个问题,也许会对某人有所帮助

 $client = new Client();
    $result = $client->request("POST", $this->url,
        [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->ApiToken,
                'Accept-Encoding' => 'gzip, deflate', //new line added
            ],
        ]);
    $content = $result->getBody()->getContents();

错误代码 18

cURL error 18: transfer closed with outstanding read data remaining

由卷曲错误指定,

CURLE_PARTIAL_FILE (18) A file transfer was shorter or larger than expected. This happens when the server first reports an expected transfer size, and then delivers data that does not match the previously given size.

因为它正在接收一个分块编码流,所以它知道什么时候块中还有数据要接收。当连接关闭时,curl 告知最后接收到的数据块不完整。因此你得到这个错误代码。

为什么添加 gzip 压缩格式的接受编码有效?

为了解决上述问题,我们需要对数据进行编码以保留数据包以接收所有数据,HTTP headers 提供了编码并为客户端提供编码,即您告诉服务器支持哪种编码,然后服务器相应地响应并通过 Content-Encoding 响应 header.

通知客户端它的选择
'Accept-Encoding' => 'gzip,                      deflate                   br'
                     encoding technique       compression format(zlib)  providing one more compression format as alternate to the server
$result = $client->request("POST", $this->url,
        [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->ApiToken,
                'Accept-Encoding' => 'gzip, deflate, br', //add encoding technique
            ],
        ]);
    $content = $result->getBody()->getContents();