1
0
mirror of synced 2024-12-03 18:26:01 +03:00
magento-module/Model/ApiClient/Http/Client.php

129 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
2017-02-03 11:05:00 +03:00
* PHP version 5.3
*
* HTTP client
2017-02-03 11:05:00 +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/ApiVersion4
*/
2017-05-31 15:24:46 +03:00
namespace Retailcrm\Retailcrm\Model\ApiClient\Http;
use Retailcrm\Retailcrm\Model\ApiClient\Exception\CurlException;
use Retailcrm\Retailcrm\Model\ApiClient\Exception\InvalidJsonException;
use Retailcrm\Retailcrm\Model\ApiClient\Response\ApiResponse;
/**
* PHP version 5.3
*
* HTTP client
*
* @category RetailCrm
* @package RetailCrm
* @author RetailCrm <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion4
*/
class Client
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
protected $url;
protected $defaultParameters;
2017-02-03 11:05:00 +03:00
/**
* Client constructor.
*
* @param string $url api url
* @param array $defaultParameters array of parameters
*
* @throws \InvalidArgumentException
*/
public function __construct($url, array $defaultParameters = array())
{
if (false === stripos($url, 'https://')) {
2017-05-31 15:24:46 +03:00
throw new \InvalidArgumentException(
2017-02-03 11:05:00 +03:00
'API schema requires HTTPS protocol'
);
}
$this->url = $url;
$this->defaultParameters = $defaultParameters;
}
/**
* Make HTTP request
*
2017-02-03 11:05:00 +03:00
* @param string $path request url
* @param string $method (default: 'GET')
* @param array $parameters (default: array())
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @throws \InvalidArgumentException
* @throws CurlException
* @throws InvalidJsonException
*
* @return ApiResponse
*/
2017-05-31 15:24:46 +03:00
public function makeRequest(
$path,
$method,
array $parameters = array()
) {
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
2017-02-03 11:05:00 +03:00
if (!in_array($method, $allowedMethods, false)) {
2017-05-31 15:24:46 +03:00
throw new \InvalidArgumentException(
2017-02-03 11:05:00 +03:00
sprintf(
'Method "%s" is not valid. Allowed methods are %s',
$method,
implode(', ', $allowedMethods)
)
);
}
$parameters = array_merge($this->defaultParameters, $parameters);
2015-06-25 18:01:54 +03:00
$url = $this->url . $path;
2017-02-03 11:05:00 +03:00
if (self::METHOD_GET === $method && count($parameters)) {
$url .= '?' . http_build_query($parameters, '', '&');
}
2017-02-03 11:05:00 +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);
if (self::METHOD_POST === $method) {
2017-02-03 11:05:00 +03:00
curl_setopt($curlHandler, CURLOPT_POST, true);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
}
2017-02-03 11:05:00 +03:00
$responseBody = curl_exec($curlHandler);
$statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
$errno = curl_errno($curlHandler);
$error = curl_error($curlHandler);
2015-06-25 18:01:54 +03:00
2017-02-03 11:05:00 +03:00
curl_close($curlHandler);
2015-06-25 18:01:54 +03:00
if ($errno) {
2017-05-31 15:24:46 +03:00
throw new CurlException($error, $errno);
}
2017-05-31 15:24:46 +03:00
return new ApiResponse($statusCode, $responseBody);
}
}