Issue
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
Solution
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.
Answered By - DashRantic Answer Checked By - David Goodson (WPSolving Volunteer)