kraken-php icon indicating copy to clipboard operation
kraken-php copied to clipboard

Returning HTTP status

Open PeterDekkers opened this issue 9 years ago • 3 comments

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;
}

PeterDekkers avatar May 13 '15 21:05 PeterDekkers

having access to the status code would indeed be good to allow handling errors

stof avatar Oct 24 '18 12:10 stof

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

mposchl avatar Aug 17 '20 12:08 mposchl

When https://api.kraken.io is DOWN !! No "success" and the response from $kraken->upload($params) is

{
  "error": {
    "status_code": 502,
    "status": "Bad Gateway"
  }
}

ricvale avatar Mar 04 '21 17:03 ricvale