|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RetailCrm\Http; |
|
4
|
|
|
|
|
5
|
|
|
use RetailCrm\Exception\CurlException; |
|
6
|
|
|
use RetailCrm\Response\ApiResponse; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* HTTP client |
|
10
|
|
|
*/ |
|
11
|
|
|
class Client |
|
12
|
|
|
{ |
|
13
|
|
|
const METHOD_GET = 'GET'; |
|
14
|
|
|
const METHOD_POST = 'POST'; |
|
15
|
|
|
|
|
16
|
|
|
protected $url; |
|
17
|
|
|
protected $defaultParameters; |
|
18
|
|
|
protected $retry; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($url, array $defaultParameters = array()) |
|
21
|
|
|
{ |
|
22
|
|
|
if (false === stripos($url, 'https://')) { |
|
23
|
|
|
throw new \InvalidArgumentException('API schema requires HTTPS protocol'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->url = $url; |
|
27
|
|
|
$this->defaultParameters = $defaultParameters; |
|
28
|
|
|
$this->retry = 0; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Make HTTP request |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $path |
|
35
|
|
|
* @param string $method (default: 'GET') |
|
36
|
|
|
* @param array $parameters (default: array()) |
|
37
|
|
|
* @param int $timeout |
|
38
|
|
|
* @param bool $verify |
|
39
|
|
|
* @param bool $debug |
|
40
|
|
|
* @return ApiResponse |
|
41
|
|
|
*/ |
|
42
|
|
|
public function makeRequest( |
|
43
|
|
|
$path, |
|
44
|
|
|
$method, |
|
45
|
|
|
array $parameters = array(), |
|
46
|
|
|
$timeout = 30, |
|
47
|
|
|
$verify = false, |
|
48
|
|
|
$debug = false |
|
|
|
|
|
|
49
|
|
|
) { |
|
50
|
|
|
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST); |
|
51
|
|
|
if (!in_array($method, $allowedMethods)) { |
|
52
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
53
|
|
|
'Method "%s" is not valid. Allowed methods are %s', |
|
54
|
|
|
$method, |
|
55
|
|
|
implode(', ', $allowedMethods) |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$parameters = array_merge($this->defaultParameters, $parameters); |
|
60
|
|
|
|
|
61
|
|
|
$path = $this->url . $path; |
|
62
|
|
|
|
|
63
|
|
|
if (self::METHOD_GET === $method && sizeof($parameters)) { |
|
64
|
|
|
$path .= '?' . http_build_query($parameters, '', '&'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$ch = curl_init(); |
|
68
|
|
|
curl_setopt($ch, CURLOPT_URL, $path); |
|
69
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); |
|
70
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); |
|
71
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
72
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
|
73
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
74
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify); |
|
75
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify); |
|
76
|
|
|
|
|
77
|
|
|
if (self::METHOD_POST === $method) { |
|
78
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
79
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$responseBody = curl_exec($ch); |
|
83
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
84
|
|
|
$errno = curl_errno($ch); |
|
85
|
|
|
$error = curl_error($ch); |
|
86
|
|
|
|
|
87
|
|
|
curl_close($ch); |
|
88
|
|
|
|
|
89
|
|
|
if ($errno) { |
|
90
|
|
|
throw new CurlException($error, $errno); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new ApiResponse($statusCode, $responseBody); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.