1
0
mirror of synced 2024-11-25 06:46:07 +03:00

Methods updated

This commit is contained in:
Kruglov Kirill 2013-07-09 16:24:28 +04:00
parent 253b818cd1
commit 31106561b8

View File

@ -33,6 +33,7 @@ class RestApi
protected $apiVersion = '1'; protected $apiVersion = '1';
protected $lastError; protected $lastError;
protected $statusCode; protected $statusCode;
protected $parameters;
/** /**
* @param string $crmUrl - адрес CRM * @param string $crmUrl - адрес CRM
@ -42,6 +43,7 @@ class RestApi
{ {
$this->apiUrl = $crmUrl.'/api/v'.$this->apiVersion.'/'; $this->apiUrl = $crmUrl.'/api/v'.$this->apiVersion.'/';
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->parameters = array('apiKey' => $this->apiKey);
} }
@ -71,11 +73,15 @@ class RestApi
* Получение заказа по id * Получение заказа по id
* *
* @param string $id - идентификатор заказа * @param string $id - идентификатор заказа
* @param string $by - поиск заказа по id или externalId
* @return array - информация о заказе * @return array - информация о заказе
*/ */
public function orderGet($id) public function orderGet($id, $by = 'externalId')
{ {
$url = $this->apiUrl.'orders/'.$id; $url = $this->apiUrl.'orders/'.$id;
if ($by != 'externalId')
$this->parameters['by'] = $by;
$result = $this->curlRequest($url); $result = $this->curlRequest($url);
return $result; return $result;
} }
@ -91,11 +97,10 @@ class RestApi
$dataJson = json_encode($order); $dataJson = json_encode($order);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['order'] = $dataJson;
$parameters['order'] = $dataJson;
$url = $this->apiUrl.'orders/create'; $url = $this->apiUrl.'orders/create';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -110,11 +115,10 @@ class RestApi
$dataJson = json_encode($order); $dataJson = json_encode($order);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['order'] = $dataJson;
$parameters['order'] = $dataJson;
$url = $this->apiUrl.'orders/'.$order['id'].'/edit'; $url = $this->apiUrl.'orders/'.$order['externalId'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -129,11 +133,10 @@ class RestApi
$dataJson = json_encode($orders); $dataJson = json_encode($orders);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['orders'] = $dataJson;
$parameters['orders'] = $dataJson;
$url = $this->apiUrl.'orders/upload'; $url = $this->apiUrl.'orders/upload';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -141,12 +144,15 @@ class RestApi
* Удаление заказа * Удаление заказа
* *
* @param string $id - идентификатор заказа * @param string $id - идентификатор заказа
* @param string $by - поиск заказа по id или externalId
* @return array * @return array
*/ */
public function orderDelete($id) public function orderDelete($id, $by = 'externalId')
{ {
$url = $this->apiUrl.'orders/'.$id.'/delete'; $url = $this->apiUrl.'orders/'.$id.'/delete';
$result = $this->curlRequest($url, array(), 'POST'); if ($by != 'externalId')
$this->parameters['by'] = $by;
$result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -162,13 +168,12 @@ class RestApi
public function orderHistory($startDate = null, $endDate = null, $limit = 100, $offset = 0) public function orderHistory($startDate = null, $endDate = null, $limit = 100, $offset = 0)
{ {
$url = $this->apiUrl.'orders/history'; $url = $this->apiUrl.'orders/history';
$parameters = array(); $this->parameters['startDate'] = $startDate;
$parameters['startDate'] = $startDate; $this->parameters['endDate'] = $endDate;
$parameters['endDate'] = $endDate; $this->parameters['limit'] = $limit;
$parameters['limit'] = $limit; $this->parameters['offset'] = $offset;
$parameters['offset'] = $offset;
$result = $this->curlRequest($url, $parameters); $result = $this->curlRequest($url);
return $result; return $result;
} }
@ -178,11 +183,14 @@ class RestApi
* Получение клиента по id * Получение клиента по id
* *
* @param string $id - идентификатор * @param string $id - идентификатор
* @param string $by - поиск заказа по id или externalId
* @return array - информация о клиенте * @return array - информация о клиенте
*/ */
public function customerGet($id) public function customerGet($id, $by = 'externalId')
{ {
$url = $this->apiUrl.'customers/'.$id; $url = $this->apiUrl.'customers/'.$id;
if ($by != 'externalId')
$this->parameters['by'] = $by;
$result = $this->curlRequest($url); $result = $this->curlRequest($url);
return $result; return $result;
} }
@ -198,11 +206,10 @@ class RestApi
$dataJson = json_encode($customer); $dataJson = json_encode($customer);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['customer'] = $dataJson;
$parameters['customer'] = $dataJson;
$url = $this->apiUrl.'customers/create'; $url = $this->apiUrl.'customers/create';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -217,11 +224,10 @@ class RestApi
$dataJson = json_encode($customer); $dataJson = json_encode($customer);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['customer'] = $dataJson;
$parameters['customer'] = $dataJson;
$url = $this->apiUrl.'customers/'.$customer['id'].'/edit'; $url = $this->apiUrl.'customers/'.$customer['externalId'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -229,12 +235,15 @@ class RestApi
* Удаление клиента * Удаление клиента
* *
* @param string $id - идентификатор * @param string $id - идентификатор
* @param string $by - поиск заказа по id или externalId
* @return array * @return array
*/ */
public function customerDelete($id) public function customerDelete($id, $by = 'externalId')
{ {
$url = $this->apiUrl.'customers/'.$id.'/delete'; $url = $this->apiUrl.'customers/'.$id.'/delete';
$result = $this->curlRequest($url, array(), 'POST'); if ($by != 'externalId')
$this->parameters['by'] = $by;
$result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -242,6 +251,7 @@ class RestApi
* Получение списка заказов клиента * Получение списка заказов клиента
* *
* @param string $id - идентификатор клиента * @param string $id - идентификатор клиента
* @param string $by - поиск заказа по id или externalId
* @param DateTime $startDate - начальная дата выборки * @param DateTime $startDate - начальная дата выборки
* @param DateTime $endDate - конечная дата * @param DateTime $endDate - конечная дата
* @param int $limit - ограничение на размер выборки * @param int $limit - ограничение на размер выборки
@ -249,16 +259,17 @@ class RestApi
* @return array - массив заказов * @return array - массив заказов
*/ */
public function customerOrdersList($id, $startDate = null, $endDate = null, public function customerOrdersList($id, $startDate = null, $endDate = null,
$limit = 100, $offset = 0) $limit = 100, $offset = 0, $by = 'externalId')
{ {
$url = $this->apiUrl.'customers/'.$id.'/orders'; $url = $this->apiUrl.'customers/'.$id.'/orders';
$parameters = array(); if ($by != 'externalId')
$parameters['startDate'] = $startDate; $this->parameters['by'] = $by;
$parameters['endDate'] = $endDate; $this->parameters['startDate'] = $startDate;
$parameters['limit'] = $limit; $this->parameters['endDate'] = $endDate;
$parameters['offset'] = $offset; $this->parameters['limit'] = $limit;
$this->parameters['offset'] = $offset;
$result = $this->curlRequest($url, $parameters); $result = $this->curlRequest($url);
return $result; return $result;
} }
@ -286,11 +297,10 @@ class RestApi
$dataJson = json_encode($deliveryType); $dataJson = json_encode($deliveryType);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['deliveryType'] = $dataJson;
$parameters['deliveryType'] = $dataJson;
$url = $this->apiUrl.'delivery-types/'.$deliveryType['code'].'/edit'; $url = $this->apiUrl.'delivery-types/'.$deliveryType['code'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -318,11 +328,10 @@ class RestApi
$dataJson = json_encode($paymentType); $dataJson = json_encode($paymentType);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['paymentType'] = $dataJson;
$parameters['paymentType'] = $dataJson;
$url = $this->apiUrl.'payment-types/'.$paymentType['code'].'/edit'; $url = $this->apiUrl.'payment-types/'.$paymentType['code'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -350,11 +359,10 @@ class RestApi
$dataJson = json_encode($paymentStatus); $dataJson = json_encode($paymentStatus);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['paymentStatus'] = $dataJson;
$parameters['paymentStatus'] = $dataJson;
$url = $this->apiUrl.'payment-statuses/'.$paymentStatus['code'].'/edit'; $url = $this->apiUrl.'payment-statuses/'.$paymentStatus['code'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -382,11 +390,10 @@ class RestApi
$dataJson = json_encode($orderType); $dataJson = json_encode($orderType);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['orderType'] = $dataJson;
$parameters['orderType'] = $dataJson;
$url = $this->apiUrl.'order-types/'.$orderType['code'].'/edit'; $url = $this->apiUrl.'order-types/'.$orderType['code'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -413,11 +420,10 @@ class RestApi
$dataJson = json_encode($status); $dataJson = json_encode($status);
$dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget,
$dataJson); $dataJson);
$parameters = array(); $this->parameters['status'] = $dataJson;
$parameters['status'] = $dataJson;
$url = $this->apiUrl.'statuses/'.$status['code'].'/edit'; $url = $this->apiUrl.'statuses/'.$status['code'].'/edit';
$result = $this->curlRequest($url, $parameters, 'POST'); $result = $this->curlRequest($url, 'POST');
return $result; return $result;
} }
@ -437,12 +443,10 @@ class RestApi
protected function curlRequest($url, $parameters = null, $method = 'GET', $format = 'json') protected function curlRequest($url, $method = 'GET', $format = 'json')
{ {
$parameters['apiKey'] = $this->apiKey; if ($method == 'GET' && !is_null($this->parameters))
$url .= '?'.http_build_query($this->parameters);
if ($method == 'GET' && !is_null($parameters))
$url .= '?'.http_build_query($parameters);
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
@ -454,11 +458,14 @@ class RestApi
if ($method == 'POST') if ($method == 'POST')
{ {
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameters);
} }
$response = curl_exec($ch); $response = curl_exec($ch);
$this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
unset($this->parameters);
/* Сброс массива с параметрами */
$this->parameters = array('apiKey' => $this->apiKey);
if (curl_errno($ch)) if (curl_errno($ch))
{ {