cURL错误18:尝试了一切,但仍在连接到Xero API时收到此错误。

huangapple go评论64阅读模式
英文:

cURL error 18: Tried everything yet still getting this error when connecting to Xero API

问题

我正在开发一个连接到Xero的API以获取联系人和发票的Web应用程序,使用Laravel Xero。起初,它可以正确获取数据。然后,不定期出现cURL 18错误。现在这个错误已经变成了永久性的。

在查看Xero的开发者仪表板时,我发现我发出的调用似乎得到了状态码200,这使我相信错误确实是出自我的端口。

以下是发出调用时的代码:

protected function guzzle ($type, $request, $data = [], $raw = false)

   {
        try {
            
            $client = new Client;

            $headers = [
                'Accept'         => 'application/json',
                'Authorization'  => 'Bearer '.$this->getAccessToken(),
                'Xero-tenant-id' => $this->getTenantId(),
                'Accept-Encoding' => 'gzip, deflate',
            ];

            $response = $client->$type(self::$baseUrl.$request, [
                'headers' => $headers,
                'body'    => $raw ? $data : json_encode($data),
            ]);
            

            return [
                'body'    => json_decode($response->getBody()->getContents(), true),
                'headers' => $response->getHeaders()
            ];
            
            
        } catch (ClientException $e) {

            throw new Exception($e->getResponse()->getBody()->getContents());

        } catch (Exception $e) {
                  
            throw an Exception($e->getMessage());
        }
    }

cURL 18错误开始在$response处出现,异常在catch (Exception $e)函数中捕获。

我已经尝试了网络上找到的几乎所有建议,但都没有成功。

提前感谢您的任何帮助。

供参考:cURL错误18:
传输关闭,尚有未完成的读取数据

英文:

I am developing a web app that connects to Xero's API to fetch Contacts and Invoices using Laravel Xero. At first it was fetching the data properly. Then the cURL 18 error started to appear erratically. And now the error has become permanent.

Checking Xero's Developer dashboard, the calls I make apparently get a status 200 which makes me believe that the error is truly from my end.

Here's the code when making the call:

protected function guzzle ($type, $request, $data = [], $raw = false)

   {
        try {
            
            $client = new Client;

            $headers = [
                'Accept'         => 'application/json',
                'Authorization'  => 'Bearer '.$this->getAccessToken(),
                'Xero-tenant-id' => $this->getTenantId(),
                'Accept-Encoding' => 'gzip, deflate',
            ];

            $response = $client->$type(self::$baseUrl.$request, [
                'headers' => $headers,
                'body'    => $raw ? $data : json_encode($data),
            ]);
            

            return [
                'body'    => json_decode($response->getBody()->getContents(), true),
                'headers' => $response->getHeaders()
            ];
            
            
        } catch (ClientException $e) {

            throw new Exception($e->getResponse()->getBody()->getContents());

        } catch (Exception $e) {
                  
            throw new Exception($e->getMessage());
        }
    }




The cURL 18 error starts to appear upon $response, with the exception being caught at the catch (Exception $e) function.

I have tried virtually every suggestion found on the web and haven't had any success.

Thank you in advance for any help.

For Reference: cURL error 18:
Transfer closed with outstanding read data remaining

答案1

得分: 0

最近我一直遇到相同的问题,即尽管Xero报告调用在他们的端口成功执行,但仍然不时出现cURL错误18。一直让我抓狂,但终于找到了解决方法。

尝试在受影响的GET请求的标头中设置以下内容:

'Content-Length:'

因此,不指定Content-Length的任何值,但仍在标头中包含它。以这种方式告诉Xero我的GET调用没有任何主体已经解决了我的问题。

英文:

I've recently been running into the same issue of getting intermittent cURL error 18 errors despite Xero reporting that the call went through successfully on their end. Been driving me nuts, but finally found a fix.

Try setting the following in the header of your affected GET requests:

'Content-Length:'

So not specifying any value for the Content-Length at all, but still including it in the header. Telling Xero that my GET calls do not have any body in this manner has resolved the issue for me.

huangapple
  • 本文由 发表于 2023年7月20日 11:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定