1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Habrahabr\Api\HttpAdapter; |
4
|
|
|
|
5
|
|
|
use Habrahabr\Api\Exception\ExtensionNotLoadedException; |
6
|
|
|
use Habrahabr\Api\Exception\NetworkException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CurlAdapter |
10
|
|
|
* |
11
|
|
|
* Habrahabr Api HTTP адаптер использующий cURL как транспорт |
12
|
|
|
* |
13
|
|
|
* @package Habrahabr\Api\HttpAdapter |
14
|
|
|
* @version 0.0.8 |
15
|
|
|
* @author thematicmedia <[email protected]> |
16
|
|
|
* @link https://tmtm.ru/ |
17
|
|
|
* @link https://habrahabr.ru/ |
18
|
|
|
* @link https://github.com/thematicmedia/habrahabr_api |
19
|
|
|
* |
20
|
|
|
* For the full copyright and license information, please view the LICENSE |
21
|
|
|
* file that was distributed with this source code. |
22
|
|
|
*/ |
23
|
|
|
class CurlAdapter extends BaseAdapter implements HttpAdapterInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @const string Тип HTTP запроса GET |
27
|
|
|
*/ |
28
|
|
|
const METHOD_GET = 'GET'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @const string Тип HTTP запроса POST |
32
|
|
|
*/ |
33
|
|
|
const METHOD_POST = 'POST'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @const string Тип HTTP запроса PUT |
37
|
|
|
*/ |
38
|
|
|
const METHOD_PUT = 'PUT'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @const string Тип HTTP запроса DELETE |
42
|
|
|
*/ |
43
|
|
|
const METHOD_DELETE = 'DELETE'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var null|resource Экземпляр cURL |
47
|
|
|
*/ |
48
|
|
|
protected $curl = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool Строгая проверка SSL сертификата |
52
|
|
|
*/ |
53
|
|
|
protected $strictSSL = true; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* CurlAdapter constructor |
57
|
|
|
* |
58
|
|
|
* @throws ExtensionNotLoadedException |
59
|
|
|
*/ |
60
|
6 |
|
public function __construct() |
61
|
|
|
{ |
62
|
6 |
|
if (!function_exists('curl_init')) { |
63
|
|
|
throw new ExtensionNotLoadedException('The cURL PHP extension was not loaded'); |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
$this->curl = curl_init(); |
67
|
6 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* CurlAdapter destructor |
71
|
|
|
*/ |
72
|
1 |
|
public function __destruct() |
73
|
|
|
{ |
74
|
1 |
|
if ($this->curl) { |
75
|
1 |
|
curl_close($this->curl); |
76
|
1 |
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Выполнить HTTP GET запрос и вернуть тело ответа |
81
|
|
|
* |
82
|
|
|
* @param string $url URL суффикс запрашиваемого ресурса |
83
|
|
|
* @return array |
84
|
|
|
* @throws NetworkException |
85
|
|
|
*/ |
86
|
1 |
|
public function get($url) |
87
|
|
|
{ |
88
|
1 |
|
return $this->request($this->createUrl($url), self::METHOD_GET); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Выполнить HTTP POST запрос и вернуть тело ответа |
93
|
|
|
* |
94
|
|
|
* @param string $url URL суффикс запрашиваемого ресурса |
95
|
|
|
* @param array $params Параметры, передаваемые в теле запроса |
96
|
|
|
* @return array |
97
|
|
|
* @throws NetworkException |
98
|
|
|
*/ |
99
|
1 |
|
public function post($url, array $params = []) |
100
|
|
|
{ |
101
|
1 |
|
return $this->request($this->createUrl($url), self::METHOD_POST, $params); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Выполнить HTTP PUT запрос и вернуть тело ответа |
106
|
|
|
* |
107
|
|
|
* @param string $url URL суффикс запрашиваемого ресурса |
108
|
|
|
* @param array $params Параметры, передаваемые в теле запроса |
109
|
|
|
* @return array |
110
|
|
|
* @throws NetworkException |
111
|
|
|
*/ |
112
|
1 |
|
public function put($url, array $params = []) |
113
|
|
|
{ |
114
|
1 |
|
return $this->request($this->createUrl($url), self::METHOD_PUT, $params); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Выполнить HTTP DELETE запрос и вернуть тело ответа |
119
|
|
|
* |
120
|
|
|
* @param string $url URL суффикс запрашиваемого ресурса |
121
|
|
|
* @param array $params Параметры, передаваемые в теле запроса |
122
|
|
|
* @return array |
123
|
|
|
* @throws NetworkException |
124
|
|
|
*/ |
125
|
1 |
|
public function delete($url, array $params = []) |
126
|
|
|
{ |
127
|
1 |
|
return $this->request($this->createUrl($url), self::METHOD_DELETE, $params); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Устанавливает или убирает строгую проверку SSL сертификата |
132
|
|
|
* |
133
|
|
|
* @param bool $flag Флаг строгой проверки SSL сертификата |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
1 |
|
public function setStrictSSL($flag = true) |
137
|
|
|
{ |
138
|
1 |
|
$this->strictSSL = $flag; |
139
|
|
|
|
140
|
1 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Выполнить HTTP запрос и вернуть тело ответа |
145
|
|
|
* |
146
|
|
|
* @param string $url URL суффикс запрашиваемого ресурса |
147
|
|
|
* @param string $method метод HTTP запроса |
148
|
|
|
* @param array $params Параметры, передаваемые в теле запроса |
149
|
|
|
* @return array |
150
|
|
|
* @throws NetworkException |
151
|
|
|
*/ |
152
|
|
|
protected function request($url, $method, array $params = []) |
153
|
|
|
{ |
154
|
|
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
155
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
156
|
|
|
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
157
|
|
|
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout); |
158
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, [ |
159
|
|
|
'client: ' . $this->client, |
160
|
|
|
'token: ' . $this->token |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
if ($this->strictSSL === false) { |
164
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
165
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($method == self::METHOD_PUT || $method == self::METHOD_POST) { |
169
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!$result = curl_exec($this->curl)) { |
173
|
|
|
$error = curl_error($this->curl); |
174
|
|
|
$errno = curl_errno($this->curl); |
175
|
|
|
throw new NetworkException($error, $errno); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$result = json_decode($result, true); |
179
|
|
|
|
180
|
|
|
return $result ? $result : []; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|