guzzlehttp \客户端卷曲错误18:转移关闭

发布于 2025-01-18 20:49:45 字数 883 浏览 4 评论 0原文

当我尝试从API中获取数据时,我会遇到

我正在使用guzzlehttp \ client laravel 6 ,

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

I am using GuzzleHttp\Client Laravel 6 and I am getting this error when I am trying to get Data from API, it's working fine on postman

Here is My Code

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()
        ];
    }

getting this error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2025-01-25 20:49:46

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

 $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();

I have resolved this issue , Maybe it will help someone

 $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();
泪是无色的血 2025-01-25 20:49:46

错误代码18

卷曲错误18:转移与剩余的未出色读取数据关闭

卷曲错误18:转移封闭,按照卷曲错误指定的

curle_partial_file(18)
文件传输短或大于预期。当这种情况发生时
服务器首先报告预期的传输大小,然后提供数据
与先前给定的大小不匹配。

由于它正在收到一个块的编码流,因此知道何时在块中剩下数据。当连接关闭时,Curl告诉最后一个收到的块是不完整的。因此,您获得此错误代码。

为什么要添加使用GZIP压缩格式编码的Accept编码?

为了解决上述问题,我们需要一个编码数据来保留数据包以接收所有问题,HTTP标头提供了编码,并且客户端IE IE告诉您支持哪些编码的服务器,然后服务器会相应地响应并告知服务器的客户端并告知服务器的客户端并告知服务器它可以选择内容编码的响应标头。

'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();

The error code 18

cURL error 18: transfer closed with outstanding read data remaining

as specified by curl errors,

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.

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,curl tells that the last received chunk was incomplete. Thus you get this error code.

Why adding a Accept Encoding with gzip compression format works?

To solve the above problem we need a encode the data to preserve the packets to receive all of it, HTTP headers provides encoding and for the client i.e you to tell the server which encoding is supported, then the server responds accordingly and informs the client of its choice with the Content-Encoding response 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文