-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcURL.class.php
More file actions
415 lines (373 loc) · 10.6 KB
/
cURL.class.php
File metadata and controls
415 lines (373 loc) · 10.6 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php //CODE BY HW
/**
* cURL封装
* @author hewei
* @datetime 2021-3-12 11:41:36
* @version 2.0
*/
class cURL {
/**
* cURL选项
* @var array
*/
protected $options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
);
/**
* 上传文件
* @var array
*/
protected $files = array();
/**
* 请求数据
* @var null
*/
protected $data;
/**
* 请求方法
* @var string
*/
protected $method;
/**
* 是否是POST请求
* @var bool
*/
protected $post;
/**
* 请求地址
* @var null
*/
protected $url;
/**
* 响应信息
* @var
*/
protected $info;
/**
* 响应主体
* @var
*/
protected $response;
/**
* 是否将响应json转为数组
* @var
*/
protected $jsonToArray;
/**
* cURL constructor.
* @param null $url 请求地址
* @param string $method 请求方法
* @param null $data 请求数据
* @param array $options cURL选项
*/
public function __construct($url = null, $method = 'GET', $data = null, $options = array()) {
$this->setUrl($url);
$this->setMethod($method);
$this->setData($data);
$this->setOptions($options);
}
/**
* 设置请求地址
* @param string $arg 请求地址
* @return $this
*/
public function setUrl($arg) {
$this->url = $arg;
return $this;
}
/**
* 获取请求地址
* @return null|string
*/
public function getUrl() {
return $this->url;
}
/**
* 设置cURL选项
* @param int $option 选项
* @param mixed $value 选项值
* @return $this
*/
public function setOption($option, $value = true) {
$this->options[$option] = $value;
return $this;
}
/**
* 批量设置cURL选项
* @param array $options 选项数组
* @return $this
*/
public function setOptions($options) {
foreach($options as $option => $value) {
$this->options[$option] = $value;
}
return $this;
}
/**
* 获取cURL选项值
* @param mixed $option 选项
* @return mixed|null
*/
public function getOption($option) {
return isset($this->options[$option]) ? $this->options[$option] : null;
}
/**
* 获取全部cURL选项
* @return array
*/
public function getOptions() {
return $this->options;
}
/**
* 添加HTTP请求头
* @param array|string $arg
* @return $this
*/
public function addHeader($arg) {
$this->setOption(CURLOPT_HTTPHEADER, array_merge($this->getHeaders(), is_array($arg) ? $arg : array($arg)));
return $this;
}
/**
* 获取全部HTTP请求头
* @return array
*/
public function getHeaders() {
return isset($this->options[CURLOPT_HTTPHEADER]) ? $this->options[CURLOPT_HTTPHEADER] : array();
}
/**
* 设置Cookie文件
* @param bool|string $arg 文件地址或true
* @return $this
*/
public function setCookieFile($arg = true) {
$cookie = is_bool($arg) ? dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cookie.tmp' : $arg;
$this->setOption(CURLOPT_COOKIEJAR, $cookie);
$this->setOption(CURLOPT_COOKIEFILE, $cookie);
return $this;
}
/**
* 获取Cookie文件地址
* @return mixed|null
*/
public function getCookieFile() {
return isset($this->options[CURLOPT_COOKIEFILE]) ? $this->options[CURLOPT_COOKIEFILE] : null;
}
/**
* 设置浏览器User-Agent
* @param bool|string $arg 自定义UA或true
* @return $this
*/
public function setUa($arg = true) {
$customUa = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0';
$this->addHeader(is_bool($arg) ? $customUa : (string)$arg);
return $this;
}
/**
* 模拟XMLHttpRequest请求
* @return $this
*/
public function setXhr() {
$this->addHeader('X-Requested-With: XMLHttpRequest');
return $this;
}
/**
* 设置是否关闭证书验证
* @param bool $arg
* @return $this
*/
public function setNoSSL($arg = true) {
$this->setOption(CURLOPT_SSL_VERIFYPEER, $arg ? false : true);
$this->setOption(CURLOPT_SSL_VERIFYHOST, $arg ? false : true);
return $this;
}
/**
* 设置是否是POST请求
* @param bool $arg
* @return $this
*/
public function setPost($arg = true) {
$this->post = $arg;
return $this;
}
/**
* 设置请求数据
* @param mixed $arg
* @return $this
*/
public function setData($arg) {
$this->data = $arg;
return $this;
}
/**
* 设置发送文件
* @param string $file 文件路径
* @param string $name 文件名称
* @return $this
* @throws cURLException
*/
public function setFile($file, $name = 'file') {
if(!class_exists('CURLFile')) {
throw new \cURLException('CURLFile class does not exist');
}
if(!file_exists($file)) {
throw new \cURLException('file does not exists. ' . $file);
}
$this->files[$name] = $file;
return $this;
}
/**
* 批量设置发送文件
* @param array $files 文件列表
* @return $this
* @throws cURLException
*/
public function setFiles($files) {
foreach($files as $key => $value) {
$this->setFile($value, $key);
}
return $this;
}
/**
* 获取上传文件
* @param mixed $arg
* @return mixed|null
*/
public function getFile($arg = 'file') {
return isset($this->files[$arg]) ? $this->files[$arg] : null;
}
/**
* 获取全部上传文件
* @return array
*/
public function getFiles() {
return $this->files;
}
/**
* 设置HTTP请求的方法,如GET|POST|HEAD|PUT|OPTION|DELETE|CONNECT等
* @param string $arg 请求方法
* @return $this
*/
public function setMethod($arg) {
if($arg === 'GET' || $arg === 'POST') {
$this->setPost($arg === 'POST');
} else {
$this->setOption(CURLOPT_CUSTOMREQUEST, $arg);
}
return $this;
}
/**
* 获取HTTP请求的方法
* @return string
*/
public function getMethod() {
return isset($this->options[CURLOPT_CUSTOMREQUEST]) ? $this->options[CURLOPT_CUSTOMREQUEST] : ($this->post ? 'POST' : 'GET');
}
/**
* 设置是否将返回JSON数据转为数组
* @param bool $arg
* @return $this
*/
public function setJsonToArray($arg = true) {
$this->jsonToArray = $arg;
return $this;
}
/**
* 发送cURL请求
* @return mixed
* @throws cURLException
*/
public function exec() {
if(is_array($this->files) && $this->files) {
$this->post = true;
if(!is_array($this->data)) {
throw new \cURLException('The data property can only be an array');
}
foreach($this->files as $name => $file) {
$mime = null;
if(class_exists('finfo')) {
$fi = finfo_open(FILEINFO_MIME);
$mime = finfo_file($fi, $file);
finfo_close($fi);
}
$this->data[$name] = new \CURLFile($file, $mime);
}
}
$url = $this->url;
if($this->post) {
$this->setOption(CURLOPT_POST, true);
$this->setOption(CURLOPT_POSTFIELDS, $this->data);
} else {
if(is_array($this->data)) {
$url = $url . (strpos($url, '?') ? '&' : '?') . http_build_query($this->data);
} else {
$url = $url . (strpos($url, '?') ? '&' : '?') . trim((string)$this->data, '?');
}
}
if($this->method) {
$this->setOption(CURLOPT_CUSTOMREQUEST, $this->method);
}
$this->setOption(CURLOPT_URL, $url);
$curl = curl_init();
if(false == curl_setopt_array($curl, $this->options)) {
throw new \cURLException('Options contain wrong options');
}
$this->response = curl_exec($curl);
if(curl_errno($curl) !== CURLE_OK) {
throw new \cURLException(curl_error($curl), curl_errno($curl), 500);
}
$this->info = curl_getinfo($curl);
if($this->jsonToArray) {
$result = json_decode($this->response, true);
if(json_last_error() !== JSON_ERROR_NONE) {
throw new \cURLException('json_decode error' . (function_exists('json_last_error_msg') ? ': ' . json_last_error_msg() : ''), 0, 501);
}
$this->response = $result;
}
curl_close($curl);
return $this->response;
}
/**
* 获取响应信息
* @param null|string $arg 响应信息的键名或null
* @return mixed
*/
public function getInfo($arg = null) {
return is_null($arg) ? $this->info : (isset($this->info[$arg]) ? $this->info[$arg] : null);
}
/**
* 获取响应的数据
* @return mixed
*/
public function getResponse() {
return $this->response;
}
}
/**
* cURL异常处理类
*/
class cURLException extends \Exception {
/**
* cURL错误编号
* @var int
*/
public $curl_errorno;
/**
* cURLException constructor.
* @param string $message 异常信息
* @param int $curl_errorno cURL错误编号
* @param int $code 异常错误编号
* @param Exception|null $previous
*/
public function __construct($message = '', $curl_errorno = 0, $code = 0, \Exception $previous = null) {
$this->curl_errorno = $curl_errorno;
parent::__construct($message, $code, $previous);
}
/**
* 获取cURL错误信息
* @return NULL|string
*/
public function curl_strerror() {
return function_exists('curl_strerror') ? curl_strerror($this->curl_errorno) : '';
}
}