1
0
mirror of synced 2024-11-22 21:36:06 +03:00
api-client-php/lib/RetailCrm/Http/Client.php

160 lines
4.3 KiB
PHP
Raw Normal View History

<?php
namespace RetailCrm\Http;
use RetailCrm\Exception\CurlException;
use RetailCrm\Response\ApiResponse;
/**
2016-03-09 02:31:29 +03:00
* PHP version 5.3
*
* 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
*/
class Client
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
protected $url;
protected $defaultParameters;
2015-12-23 10:23:10 +03:00
protected $retry;
2016-03-09 02:31:29 +03:00
/**
* Client constructor.
*
* @param string $url api url
* @param array $defaultParameters array of parameters
*/
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'
);
}
$this->url = $url;
$this->defaultParameters = $defaultParameters;
$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
);
}
/**
* 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())
* @param int $timeout (default: 30)
* @param bool $verify (default: false)
* @param bool $debug (default: false)
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @return ApiResponse
*/
2015-05-08 18:03:05 +03:00
public function makeRequest(
$path,
$method,
array $parameters = array(),
$timeout = 30,
$verify = false,
$debug = false
) {
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
if (!in_array($method, $allowedMethods)) {
2016-03-09 02:31:29 +03:00
throw new \InvalidArgumentException(
sprintf(
'Method "%s" is not valid. Allowed methods are %s',
$method,
implode(', ', $allowedMethods)
)
);
}
$parameters = array_merge($this->defaultParameters, $parameters);
$url = $this->url . $path;
2015-05-08 18:03:05 +03:00
if (self::METHOD_GET === $method && sizeof($parameters)) {
2016-01-09 15:23:50 +03:00
$url .= '?' . http_build_query($parameters, '', '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify);
if (!$debug) {
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
} else {
2016-03-09 02:31:29 +03:00
curl_setopt(
$ch,
CURLOPT_TIMEOUT_MS,
(int) $timeout + ($this->retry * 2000)
);
}
if (self::METHOD_POST === $method) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
}
2015-05-08 18:03:05 +03:00
$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
2015-05-08 18:03:05 +03:00
curl_close($ch);
2016-03-09 02:31:29 +03:00
if ($errno && in_array($errno, $this->curlErrors) && $this->retry < 3) {
$errno = null;
$error = null;
$this->retry += 1;
$this->makeRequest(
$path,
$method,
$parameters,
$timeout,
$verify,
$debug
);
}
2015-05-08 18:03:05 +03:00
if ($errno) {
throw new CurlException($error, $errno);
}
2015-05-08 18:03:05 +03:00
return new ApiResponse($statusCode, $responseBody);
}
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;
}
}