From 9a04be2d9a77abe6a6bf419dc75b5381af670027 Mon Sep 17 00:00:00 2001 From: kruglov Date: Wed, 3 Jul 2013 11:34:39 +0400 Subject: [PATCH] Error handling added --- IntaroCrmRestApi.php | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/IntaroCrmRestApi.php b/IntaroCrmRestApi.php index ac30c13..5120f61 100644 --- a/IntaroCrmRestApi.php +++ b/IntaroCrmRestApi.php @@ -30,6 +30,8 @@ class IntaroCrmRestApi protected $apiUrl; protected $apiKey; protected $apiVersion = '1'; + protected $lastError; + protected $statusCode; /** * @param string $crmUrl - адрес CRM @@ -41,6 +43,22 @@ class IntaroCrmRestApi $this->apiKey = $apiKey; } + public function getStatusCode() + { + return $this->statusCode; + } + + public function getLastError() + { + return $this->statusCode . ' ' . $this->lastError; + } + + public function getLastErrorMessage() + { + return $this->lastError; + } + + /* Методы для работы с заказами */ /** * Получение заказа по id @@ -357,13 +375,27 @@ class IntaroCrmRestApi } $response = curl_exec($ch); + $this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_errno($ch)) + { + $this->lastError = 'Curl error: ' . curl_error($ch); + return null; + } curl_close($ch); - if (empty($response)) - return false; - $result = (array)json_decode($response, true); - return $result; + if ($result['success'] == false) + { + $this->lastError = $result['errorMsg']; + return null; + } + + $this->lastError = null; + unset($result['success']); + if (count($result) == 0) + return true; + return reset($result); } }