我正在使用GuzzleHttp\ClientLaravel 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
uj5u.com熱心網友回復:
錯誤代碼 18
cURL 錯誤 18:傳輸已關閉,剩余未完成的讀取資料
由 curl 錯誤指定,
CURLE_PARTIAL_FILE (18) 檔案傳輸比預期的短或大。當服務器首先報告預期的傳輸大小,然后交付與先前給定大小不匹配的資料時,就會發生這種情況。
因為它正在接收一個分塊的編碼流,它知道何時有資料留在一個塊中要接收。當連接關閉時,curl 告訴你最后收到的塊是不完整的。因此,您會收到此錯誤代碼。
為什么使用 gzip 壓縮格式添加接受編碼有效?
為了解決上述問題,我們需要對資料進行編碼以保存資料包以接收所有資料,HTTP 標頭為客戶端提供編碼,即您告訴服務器支持哪種編碼,然后服務器相應地回應并通知客戶端它與 Content-Encoding 回應標頭的選擇。
'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();
uj5u.com熱心網友回復:
我已經解決了這個問題,也許它會幫助別人
$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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459360.html
