kraken-php
kraken-php copied to clipboard
Returning HTTP status
I'm wondering if the HTTP status codes could be returned from the Kraken->request()
method to improve error handling?
https://kraken.io/docs/http-status-codes
Possibly something like this:
private function request($data, $url, $type) {
$curl = curl_init();
if ($type === 'url') {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$response = json_decode(substr($response, $header_size), true);
if (!isset($response["http_status"])) {
$response["http_status"] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
curl_close($curl);
return $response;
}
having access to the status code would indeed be good to allow handling errors
this is really needed to proper handle (and log!) the behavior. the API is not stable enough to "trust" the code in this lib:
if ($response === null) {
$response = array (
"success" => false,
"error" => 'cURL Error: ' . curl_error($curl)
);
}
this code curl_error($curl)
does not work with HTTP status codes for curl_setopt($curl, CURLOPT_FAILONERROR, 0);
When https://api.kraken.io is DOWN !! No "success"
and the response from $kraken->upload($params)
is
{
"error": {
"status_code": 502,
"status": "Bad Gateway"
}
}