-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.php
310 lines (291 loc) · 7.94 KB
/
Curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* @author Semenov Alexander <[email protected]>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 24.06.2015
*
* @see https://github.com/linslin/Yii2-Curl
*/
namespace skeeks\yii2\curl;
use yii\base\Component;
use Yii;
use yii\base\Exception;
use yii\web\HttpException;
/**
* @property array $options
*
* Class Curl
* @package skeeks\cms\helpers
*/
class Curl extends Component
{
const METHOD_GET = "GET";
const METHOD_POST = "POST";
const METHOD_PUT = "PUT";
const METHOD_DELETE = "DELETE";
const METHOD_HEAD = "HEAD";
const METHOD_PATCH = "PATCH";
const METHOD_OPTIONS = "OPTIONS";
/**
* @var array Разрешенные методы
*/
static public $methods =
[
self::METHOD_GET, self::METHOD_POST, self::METHOD_HEAD, self::METHOD_PUT, self::METHOD_DELETE, self::METHOD_PATCH, self::METHOD_OPTIONS
];
public function init()
{
if (!function_exists('curl_init'))
{
throw new \InvalidArgumentException('Не установлена библиотека php — curl');
}
}
/**
* @var string
* Holds response data right after sending a request.
*/
public $response = null;
/**
* @var integer HTTP-Status Code
* This value will hold HTTP-Status Code. False if request was not successful.
*/
public $responseCode = null;
/**
* @var array
*/
private $_options = [];
/**
* @var array
*/
private $_defaultOptions = [
CURLOPT_USERAGENT => 'SkeekS-Cms-Curl-Agent',
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
];
// ############################################### class methods // ##############################################
/**
* Start performing GET-HTTP-Request
*
* @param string $url
*
* @return mixed response
*/
public function get($url)
{
return $this->httpRequest('GET', $url);
}
/**
* Start performing HEAD-HTTP-Request
*
* @param string $url
* @return mixed response
*/
public function head($url)
{
return $this->httpRequest('HEAD', $url);
}
/**
* Start performing POST-HTTP-Request
*
* @param string $url
* @return mixed response
*/
public function post($url)
{
return $this->httpRequest('POST', $url);
}
/**
* Start performing PUT-HTTP-Request
*
* @param $url
* @return mixed
* @throws Exception
*/
public function put($url)
{
return $this->httpRequest('PUT', $url);
}
/**
* Start performing DELETE-HTTP-Request
*
* @param $url
* @return bool|mixed|string
* @throws Exception
*/
public function delete($url)
{
return $this->httpRequest(self::METHOD_DELETE, $url);
}
/**
* Start performing PATCH-HTTP-Request
*
* @param $url
* @return bool|mixed|string
* @throws Exception
*/
public function patch($url)
{
return $this->httpRequest(self::METHOD_PATCH, $url);
}
/**
* Start performing OPTIONS-HTTP-Request
*
* @param $url
* @return bool|mixed|string
* @throws Exception
*/
public function options($url)
{
return $this->httpRequest(self::METHOD_OPTIONS, $url);
}
/**
* Set curl option
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function setOption($key, $value)
{
//set value
$this->_options[$key] = $value;
//return self
return $this;
}
/**
* Unset a single curl option
*
* @param string $key
*
* @return $this
*/
public function unsetOption($key)
{
//reset a single option if its set already
if (isset($this->_options[$key])) {
unset($this->_options[$key]);
}
return $this;
}
/**
* Unset all curl option, excluding default options.
*
* @return $this
*/
public function unsetOptions()
{
//reset all options
if (isset($this->_options)) {
$this->_options = array();
}
return $this;
}
/**
* Total reset of options, responses, etc.
*
* @return $this
*/
public function reset()
{
//reset all options
if (isset($this->_options)) {
$this->_options = array();
}
//reset response & status code
$this->response = null;
$this->responseCode = null;
return $this;
}
/**
* Return a single option
*
* @return mixed // false if option is not set.
*/
public function getOption($key)
{
//get merged options depends on default and user options
$mergesOptions = $this->getOptions();
//return value or false if key is not set.
return isset($mergesOptions[$key]) ? $mergesOptions[$key] : false;
}
/**
* Return merged curl options and keep keys!
*
* @return array
*/
public function getOptions()
{
return $this->_options + $this->_defaultOptions;
}
/**
* Performs HTTP request
*
* @param $method
* @param $url
* @return bool|mixed|string
* @throws Exception
*/
public function httpRequest($method, $url)
{
$method = strtoupper($method);
if (!in_array($method, self::$methods))
{
throw new \InvalidArgumentException("Method '{$method}' not allow execute");
}
//Init
$body = '';
//set request type and writer function
$this->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($method));
//check if method is head and set no body
if ($method === 'HEAD') {
$this->setOption(CURLOPT_NOBODY, true);
$this->setOption(CURLOPT_HEADER, true);
$this->unsetOption(CURLOPT_WRITEFUNCTION);
} if ($method === 'OPTIONS') {
$this->setOption(CURLOPT_NOBODY, true);
$this->setOption(CURLOPT_HEADER, true);
$this->unsetOption(CURLOPT_WRITEFUNCTION);
} else {
$this->setOption(CURLOPT_WRITEFUNCTION, function ($curl, $data) use (&$body) {
$body .= $data;
return mb_strlen($data, '8bit');
});
}
//setup error reporting and profiling
Yii::trace('Start sending cURL-Request: '.$url.'\n', __METHOD__);
Yii::beginProfile($method.' '.$url.'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
/**
* proceed curl
*/
$curl = curl_init($url);
curl_setopt_array($curl, $this->getOptions());
$body = curl_exec($curl);
//check if curl was successful
if ($body === false) {
throw new Exception('curl request failed: ' . curl_error($curl) , curl_errno($curl));
}
//retrieve response code
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->response = $body;
//stop curl
curl_close($curl);
//end yii debug profile
Yii::endProfile($method.' '.$url .'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
//check responseCode and return data/status
if ($this->responseCode >= 200 && $this->responseCode < 300) { // all between 200 && 300 is successful
if ($this->getOption(CURLOPT_CUSTOMREQUEST) === 'HEAD') {
return true;
} else {
return $this->response;
}
} elseif ($this->responseCode >= 400 && $this->responseCode <= 510) { // client and server errors return false.
return false;
} else { //any other status code or custom codes
return true;
}
}
}