add logic to handle all 2xx response code, bug fix for string response,

bug fix for showing error message using serialize
This commit is contained in:
William Cheng 2015-03-11 01:37:39 +08:00
parent 911be4360a
commit 7555dda2bf
2 changed files with 13 additions and 4 deletions

View File

@ -129,11 +129,14 @@ class APIClient {
if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url .
" took more than 5s to return" );
} else if ($response_info['http_code'] == 200) {
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response);
if (json_last_error() > 0) { // if response is a string
$data = $response;
}
} else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url .
": ".json_decode($response)->message );
": ".serialize($response));
} else if ($response_info['http_code'] == 404) {
$data = null;
} else {

View File

@ -107,6 +107,9 @@ class APIClient {
if ($method == self::$POST) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$PATCH) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$PUT) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
@ -126,11 +129,14 @@ class APIClient {
if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url .
" took more than 5s to return" );
} else if ($response_info['http_code'] == 200) {
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response);
if (json_last_error() > 0) { // if response is a string
$data = $response;
}
} else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url .
": ".json_decode($response)->message );
": ".serialize($response));
} else if ($response_info['http_code'] == 404) {
$data = null;
} else {