2014-11-06 02:44:52 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\Http;
|
|
|
|
|
|
|
|
use RetailCrm\Exception\CurlException;
|
2016-03-12 01:54:33 +03:00
|
|
|
use RetailCrm\Exception\InvalidJsonException;
|
2014-11-06 02:44:52 +03:00
|
|
|
use RetailCrm\Response\ApiResponse;
|
|
|
|
|
|
|
|
/**
|
2016-03-09 02:31:29 +03:00
|
|
|
* PHP version 5.3
|
|
|
|
*
|
2014-11-06 02:44:52 +03:00
|
|
|
* HTTP client
|
2016-03-09 02:31:29 +03:00
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion3
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
const METHOD_GET = 'GET';
|
|
|
|
const METHOD_POST = 'POST';
|
|
|
|
|
|
|
|
protected $url;
|
|
|
|
protected $defaultParameters;
|
2015-12-23 10:23:10 +03:00
|
|
|
protected $retry;
|
2014-11-06 02:44:52 +03:00
|
|
|
|
2016-03-09 02:31:29 +03:00
|
|
|
/**
|
|
|
|
* Client constructor.
|
|
|
|
*
|
|
|
|
* @param string $url api url
|
|
|
|
* @param array $defaultParameters array of parameters
|
2016-03-12 01:54:33 +03:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
2016-03-09 02:31:29 +03:00
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
public function __construct($url, array $defaultParameters = array())
|
|
|
|
{
|
|
|
|
if (false === stripos($url, 'https://')) {
|
2016-03-09 02:31:29 +03:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'API schema requires HTTPS protocol'
|
|
|
|
);
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->url = $url;
|
|
|
|
$this->defaultParameters = $defaultParameters;
|
2015-04-30 17:35:57 +03:00
|
|
|
$this->retry = 0;
|
2016-03-09 02:31:29 +03:00
|
|
|
$this->curlErrors = array(
|
|
|
|
CURLE_COULDNT_RESOLVE_PROXY,
|
|
|
|
CURLE_COULDNT_RESOLVE_HOST,
|
|
|
|
CURLE_COULDNT_CONNECT,
|
|
|
|
CURLE_OPERATION_TIMEOUTED,
|
|
|
|
CURLE_HTTP_POST_ERROR,
|
|
|
|
CURLE_SSL_CONNECT_ERROR,
|
|
|
|
CURLE_SEND_ERROR,
|
|
|
|
CURLE_RECV_ERROR
|
|
|
|
);
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make HTTP request
|
|
|
|
*
|
2016-03-09 02:31:29 +03:00
|
|
|
* @param string $path request url
|
|
|
|
* @param string $method (default: 'GET')
|
|
|
|
* @param array $parameters (default: array())
|
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
|
|
|
*
|
2016-03-12 01:54:33 +03:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @throws CurlException
|
|
|
|
* @throws InvalidJsonException
|
|
|
|
*
|
2014-11-06 02:44:52 +03:00
|
|
|
* @return ApiResponse
|
|
|
|
*/
|
2015-05-08 18:03:05 +03:00
|
|
|
public function makeRequest(
|
|
|
|
$path,
|
|
|
|
$method,
|
2016-03-12 01:54:33 +03:00
|
|
|
array $parameters = array()
|
2015-05-08 18:03:05 +03:00
|
|
|
) {
|
2014-11-06 02:44:52 +03:00
|
|
|
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
|
2016-03-12 01:54:33 +03:00
|
|
|
|
|
|
|
if (!in_array($method, $allowedMethods, false)) {
|
2016-03-09 02:31:29 +03:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf(
|
|
|
|
'Method "%s" is not valid. Allowed methods are %s',
|
|
|
|
$method,
|
|
|
|
implode(', ', $allowedMethods)
|
|
|
|
)
|
|
|
|
);
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = array_merge($this->defaultParameters, $parameters);
|
|
|
|
|
2015-06-15 17:50:31 +03:00
|
|
|
$url = $this->url . $path;
|
2015-05-08 18:03:05 +03:00
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
if (self::METHOD_GET === $method && count($parameters)) {
|
2016-01-09 15:23:50 +03:00
|
|
|
$url .= '?' . http_build_query($parameters, '', '&');
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
$curlHandler = curl_init();
|
|
|
|
curl_setopt($curlHandler, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_FAILONERROR, false);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 30);
|
2015-06-15 17:50:31 +03:00
|
|
|
|
2014-11-06 02:44:52 +03:00
|
|
|
if (self::METHOD_POST === $method) {
|
2016-03-12 01:54:33 +03:00
|
|
|
curl_setopt($curlHandler, CURLOPT_POST, true);
|
|
|
|
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
$responseBody = curl_exec($curlHandler);
|
|
|
|
$statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
|
|
|
|
$errno = curl_errno($curlHandler);
|
|
|
|
$error = curl_error($curlHandler);
|
2015-04-30 17:35:57 +03:00
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
curl_close($curlHandler);
|
2015-05-08 18:03:05 +03:00
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
if ($errno && in_array($errno, $this->curlErrors, false) && $this->retry < 3) {
|
2015-06-15 17:50:31 +03:00
|
|
|
$errno = null;
|
|
|
|
$error = null;
|
2016-03-12 01:54:33 +03:00
|
|
|
++$this->retry;
|
|
|
|
$this->makeRequest($path, $method, $parameters);
|
2015-06-15 17:50:31 +03:00
|
|
|
}
|
|
|
|
|
2015-05-08 18:03:05 +03:00
|
|
|
if ($errno) {
|
|
|
|
throw new CurlException($error, $errno);
|
2015-04-30 17:35:57 +03:00
|
|
|
}
|
|
|
|
|
2015-05-08 18:03:05 +03:00
|
|
|
return new ApiResponse($statusCode, $responseBody);
|
2015-04-30 17:35:57 +03:00
|
|
|
}
|
2015-12-23 10:23:10 +03:00
|
|
|
|
2016-03-09 02:31:29 +03:00
|
|
|
/**
|
|
|
|
* Retry connect
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2015-12-23 10:23:10 +03:00
|
|
|
public function getRetry()
|
|
|
|
{
|
|
|
|
return $this->retry;
|
|
|
|
}
|
2014-11-10 03:12:50 +03:00
|
|
|
}
|