1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Transport; |
4
|
|
|
|
5
|
|
|
use const CURLOPT_CONNECTTIMEOUT_MS; |
6
|
|
|
use const CURLOPT_HTTPGET; |
7
|
|
|
use const CURLOPT_POST; |
8
|
|
|
use const CURLOPT_TIMEOUT_MS; |
9
|
|
|
|
10
|
|
|
class CurlerRequest |
11
|
|
|
{ |
12
|
|
|
/** |
|
|
|
|
13
|
|
|
* @var array |
|
|
|
|
14
|
|
|
*/ |
15
|
|
|
public $extendinfo = array(); |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @var string|array |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
private $parameters = ''; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @var array |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
private $options; |
26
|
|
|
|
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* @var array |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
private $headers; // Parsed reponse header object. |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $url; |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $method; |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $id; |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var resource|null |
49
|
|
|
*/ |
50
|
|
|
private $handle; |
51
|
|
|
|
52
|
|
|
/** @var CurlerResponse */ |
53
|
|
|
private $response; |
54
|
|
|
|
55
|
|
|
/** @var bool */ |
56
|
|
|
private $_persistent = false; |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
private $_attachFiles = false; |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $callback_class = ''; |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $callback_functionName = ''; |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
private $_httpCompression = false; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @var callable |
80
|
|
|
*/ |
81
|
|
|
private $callback_function = null; |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* @var bool|resource |
85
|
|
|
*/ |
86
|
|
|
private $infile_handle = false; |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* @var int |
90
|
|
|
*/ |
91
|
|
|
private $_dns_cache = 120; |
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* @var resource |
95
|
|
|
*/ |
96
|
|
|
private $resultFileHandle = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param bool $id |
100
|
|
|
*/ |
101
|
63 |
|
public function __construct($id = false) |
|
|
|
|
102
|
|
|
{ |
103
|
63 |
|
$this->id = $id; |
104
|
|
|
|
105
|
63 |
|
$this->header('Cache-Control', 'no-cache, no-store, must-revalidate'); |
106
|
63 |
|
$this->header('Expires', '0'); |
107
|
63 |
|
$this->header('Pragma', 'no-cache'); |
108
|
|
|
|
109
|
63 |
|
$this->options = array( |
|
|
|
|
110
|
63 |
|
CURLOPT_SSL_VERIFYHOST => 0, |
|
|
|
|
111
|
63 |
|
CURLOPT_SSL_VERIFYPEER => false, |
|
|
|
|
112
|
63 |
|
CURLOPT_TIMEOUT => 10, |
|
|
|
|
113
|
63 |
|
CURLOPT_CONNECTTIMEOUT => 5, // Количество секунд ожидания при попытке соединения |
|
|
|
|
114
|
63 |
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
|
|
|
115
|
63 |
|
CURLOPT_MAXREDIRS => 10, |
|
|
|
|
116
|
63 |
|
CURLOPT_HEADER => TRUE, |
|
|
|
|
117
|
63 |
|
CURLOPT_FOLLOWLOCATION => TRUE, |
|
|
|
|
118
|
63 |
|
CURLOPT_AUTOREFERER => 1, // при редиректе подставлять в «Referer:» значение из «Location:» |
|
|
|
|
119
|
63 |
|
CURLOPT_BINARYTRANSFER => 1, // передавать в binary-safe |
|
|
|
|
120
|
63 |
|
CURLOPT_RETURNTRANSFER => TRUE, |
|
|
|
|
121
|
63 |
|
CURLOPT_USERAGENT => 'smi2/PHPClickHouse/client', |
|
|
|
|
122
|
|
|
); |
123
|
63 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
|
|
|
|
126
|
|
|
* |
127
|
|
|
*/ |
128
|
63 |
|
public function __destruct() |
|
|
|
|
129
|
|
|
{ |
130
|
63 |
|
$this->close(); |
131
|
63 |
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
63 |
|
public function close() |
|
|
|
|
135
|
|
|
{ |
136
|
63 |
|
if ($this->handle) |
137
|
|
|
{ |
138
|
63 |
|
curl_close($this->handle); |
|
|
|
|
139
|
|
|
} |
140
|
63 |
|
$this->handle = null; |
141
|
63 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $attachFiles |
|
|
|
|
145
|
|
|
*/ |
146
|
1 |
|
public function attachFiles($attachFiles) |
|
|
|
|
147
|
|
|
{ |
148
|
1 |
|
$this->header("Content-Type", "multipart/form-data"); |
|
|
|
|
149
|
|
|
|
150
|
1 |
|
$out = []; |
151
|
1 |
|
foreach ($attachFiles as $post_name => $file_path) { |
152
|
1 |
|
$out[$post_name] = new \CURLFile($file_path); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
$this->_attachFiles = true; |
156
|
1 |
|
$this->parameters($out); |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param bool $set |
|
|
|
|
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function id($set = false) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
if ($set) { |
167
|
|
|
$this->id = $set; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $params |
|
|
|
|
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
63 |
|
public function setRequestExtendedInfo($params) |
|
|
|
|
178
|
|
|
{ |
179
|
63 |
|
$this->extendinfo = $params; |
180
|
63 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string|integer|null $key |
|
|
|
|
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
63 |
|
public function getRequestExtendedInfo($key = null) |
188
|
|
|
{ |
189
|
63 |
|
if ($key) { |
190
|
63 |
|
return isset($this->extendinfo[$key]) ? $this->extendinfo[$key] : false; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this->extendinfo; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return bool|resource |
198
|
|
|
*/ |
199
|
8 |
|
public function getInfileHandle() |
200
|
|
|
{ |
201
|
8 |
|
return $this->infile_handle; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $file_name |
|
|
|
|
206
|
|
|
* @return bool|resource |
207
|
|
|
*/ |
208
|
8 |
|
public function setInfile($file_name) |
|
|
|
|
209
|
|
|
{ |
210
|
8 |
|
$this->header('Expect', ''); |
211
|
8 |
|
$this->infile_handle = fopen($file_name, 'r'); |
|
|
|
|
212
|
8 |
|
if (is_resource($this->infile_handle)) |
|
|
|
|
213
|
|
|
{ |
|
|
|
|
214
|
|
|
|
215
|
8 |
|
if ($this->_httpCompression) { |
216
|
8 |
|
$this->header('Content-Encoding', 'gzip'); |
217
|
8 |
|
$this->header('Content-Type', 'application/x-www-form-urlencoded'); |
218
|
|
|
|
219
|
8 |
|
stream_filter_append($this->infile_handle, 'zlib.deflate', STREAM_FILTER_READ, ["window" => 30]); |
|
|
|
|
220
|
|
|
|
221
|
8 |
|
$this->options[CURLOPT_SAFE_UPLOAD] = 1; |
|
|
|
|
222
|
|
|
} else { |
223
|
|
|
$this->options[CURLOPT_INFILESIZE] = filesize($file_name); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
8 |
|
$this->options[CURLOPT_INFILE] = $this->infile_handle; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
8 |
|
return $this->infile_handle; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param callable $callback |
234
|
|
|
*/ |
235
|
8 |
|
public function setCallbackFunction($callback) |
|
|
|
|
236
|
|
|
{ |
237
|
8 |
|
$this->callback_function = $callback; |
238
|
8 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param callable $callback |
242
|
|
|
*/ |
243
|
1 |
|
public function setWriteFunction($callback) |
|
|
|
|
244
|
|
|
{ |
245
|
1 |
|
$this->options[CURLOPT_WRITEFUNCTION]=$callback; |
|
|
|
|
246
|
1 |
|
} |
247
|
|
|
/** |
248
|
|
|
* @param callable $callback |
249
|
|
|
*/ |
250
|
3 |
|
public function setReadFunction($callback) |
|
|
|
|
251
|
|
|
{ |
252
|
3 |
|
$this->options[CURLOPT_READFUNCTION] = $callback; |
|
|
|
|
253
|
3 |
|
} |
254
|
|
|
|
255
|
|
|
public function setHeaderFunction($callback) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$this->options[CURLOPT_HEADERFUNCTION] = $callback; |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $classCallBack |
262
|
|
|
* @param string $functionName |
263
|
|
|
*/ |
264
|
|
|
public function setCallback($classCallBack, $functionName) |
|
|
|
|
265
|
|
|
{ |
266
|
|
|
$this->callback_class = $classCallBack; |
|
|
|
|
267
|
|
|
$this->callback_functionName = $functionName; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
|
|
|
|
271
|
|
|
* |
272
|
|
|
*/ |
273
|
10 |
|
public function onCallback() |
|
|
|
|
274
|
|
|
{ |
275
|
10 |
|
if ($this->callback_function) { |
276
|
8 |
|
$x = $this->callback_function; |
277
|
8 |
|
$x($this); |
278
|
|
|
} |
279
|
|
|
|
280
|
10 |
|
if ($this->callback_class && $this->callback_functionName) { |
|
|
|
|
281
|
|
|
$c = $this->callback_functionName; |
282
|
|
|
$this->callback_class->$c($this); |
283
|
|
|
} |
284
|
10 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param bool $result |
|
|
|
|
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function dump($result = false) |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$message = "\n------------ Request ------------\n"; |
|
|
|
|
293
|
|
|
$message .= 'URL:' . $this->url . "\n\n"; |
294
|
|
|
$message .= 'METHOD:' . $this->method . "\n\n"; |
295
|
|
|
$message .= 'PARAMS:' . print_r($this->parameters, true) . "\n"; |
|
|
|
|
296
|
|
|
$message .= 'PARAMS:' . print_r($this->headers, true) . "\n"; |
|
|
|
|
297
|
|
|
$message .= "-----------------------------------\n"; |
298
|
|
|
|
299
|
|
|
if ($result) { |
300
|
|
|
return $message; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
echo $message; |
304
|
|
|
return ''; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return bool |
309
|
|
|
*/ |
310
|
13 |
|
public function getId() |
|
|
|
|
311
|
|
|
{ |
312
|
13 |
|
return $this->id; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param integer $key |
|
|
|
|
317
|
|
|
* @param mixed $value |
|
|
|
|
318
|
|
|
* @return $this |
319
|
|
|
*/ |
320
|
1 |
|
private function option($key, $value) |
|
|
|
|
321
|
|
|
{ |
322
|
1 |
|
$this->options[$key] = $value; |
323
|
1 |
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return $this |
328
|
|
|
*/ |
329
|
1 |
|
public function persistent() |
330
|
|
|
{ |
331
|
1 |
|
$this->_persistent = true; |
332
|
1 |
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @return bool |
337
|
|
|
*/ |
338
|
10 |
|
public function isPersistent() |
|
|
|
|
339
|
|
|
{ |
340
|
10 |
|
return $this->_persistent; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param int $sec |
|
|
|
|
345
|
|
|
* @return $this |
346
|
|
|
*/ |
347
|
63 |
|
public function keepAlive($sec = 60) |
|
|
|
|
348
|
|
|
{ |
349
|
63 |
|
$this->options[CURLOPT_FORBID_REUSE] = TRUE; |
|
|
|
|
350
|
63 |
|
$this->headers['Connection'] = 'Keep-Alive'; |
|
|
|
|
351
|
63 |
|
$this->headers['Keep-Alive'] = $sec; |
|
|
|
|
352
|
|
|
|
353
|
63 |
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param bool $flag |
|
|
|
|
358
|
|
|
* @return $this |
359
|
|
|
*/ |
360
|
63 |
|
public function verbose($flag = true) |
|
|
|
|
361
|
|
|
{ |
362
|
63 |
|
$this->options[CURLOPT_VERBOSE] = $flag; |
|
|
|
|
363
|
63 |
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string $key |
|
|
|
|
368
|
|
|
* @param string $value |
369
|
|
|
* @return $this |
370
|
|
|
*/ |
371
|
63 |
|
public function header($key, $value) |
|
|
|
|
372
|
|
|
{ |
373
|
63 |
|
$this->headers[$key] = $value; |
374
|
63 |
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @return array |
|
|
|
|
379
|
|
|
*/ |
380
|
|
|
public function getHeaders() |
|
|
|
|
381
|
|
|
{ |
382
|
|
|
$head = []; |
383
|
|
|
foreach ($this->headers as $key=>$value) { |
|
|
|
|
384
|
|
|
$head[] = sprintf("%s: %s", $key, $value); |
|
|
|
|
385
|
|
|
} |
386
|
|
|
return $head; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param string $url |
|
|
|
|
391
|
|
|
* @return $this |
392
|
|
|
*/ |
393
|
63 |
|
public function url($url) |
|
|
|
|
394
|
|
|
{ |
395
|
63 |
|
$this->url = $url; |
396
|
63 |
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return mixed |
401
|
|
|
*/ |
402
|
|
|
public function getUrl() |
403
|
|
|
{ |
404
|
|
|
return $this->url; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @param string $id |
|
|
|
|
410
|
|
|
* @return string |
411
|
|
|
*/ |
412
|
13 |
|
public function getUniqHash($id) |
|
|
|
|
413
|
|
|
{ |
414
|
13 |
|
return $id . '.' . microtime() . mt_rand(0, 1000000); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
63 |
|
public function httpCompression(bool $flag) : void |
418
|
|
|
{ |
419
|
63 |
|
if ($flag) { |
420
|
63 |
|
$this->_httpCompression = $flag; |
|
|
|
|
421
|
63 |
|
$this->options[CURLOPT_ENCODING] = 'gzip'; |
|
|
|
|
422
|
|
|
} else { |
423
|
|
|
$this->_httpCompression = false; |
424
|
|
|
unset($this->options[CURLOPT_ENCODING]); |
|
|
|
|
425
|
|
|
} |
426
|
63 |
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @param string $username |
|
|
|
|
430
|
|
|
* @param string $password |
431
|
|
|
* @return $this |
432
|
|
|
*/ |
433
|
63 |
|
public function auth($username, $password) |
|
|
|
|
434
|
|
|
{ |
435
|
63 |
|
$this->options[CURLOPT_USERPWD] = sprintf("%s:%s", $username, $password); |
|
|
|
|
436
|
63 |
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @param array|string $data |
|
|
|
|
441
|
|
|
* @return $this |
442
|
|
|
*/ |
443
|
1 |
|
public function parameters($data) |
444
|
|
|
{ |
445
|
1 |
|
$this->parameters = $data; |
446
|
1 |
|
return $this; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* The maximum number of seconds (float) allowed to execute cURL functions. |
451
|
|
|
* |
452
|
|
|
* @param float $seconds |
|
|
|
|
453
|
|
|
* @return $this |
454
|
|
|
*/ |
455
|
63 |
|
public function setTimeout(float $seconds = 10) : self |
456
|
|
|
{ |
457
|
63 |
|
$this->options[CURLOPT_TIMEOUT_MS] = (int) ($seconds * 1000); |
458
|
|
|
|
459
|
63 |
|
return $this; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* The number of seconds to wait when trying to connect. Use 0 for infinite waiting. |
464
|
|
|
*/ |
465
|
63 |
|
public function setConnectTimeout(float $seconds = 1) : self |
466
|
|
|
{ |
467
|
63 |
|
$this->options[CURLOPT_CONNECTTIMEOUT_MS] = (int) ($seconds * 1000); |
468
|
|
|
|
469
|
63 |
|
return $this; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @param array|mixed $data |
|
|
|
|
474
|
|
|
* @return $this |
475
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
476
|
|
|
*/ |
477
|
63 |
|
public function parameters_json($data) |
|
|
|
|
478
|
|
|
{ |
|
|
|
|
479
|
|
|
|
480
|
63 |
|
$this->header("Content-Type", "application/json, text/javascript; charset=utf-8"); |
|
|
|
|
481
|
63 |
|
$this->header("Accept", "application/json, text/javascript, */*; q=0.01"); |
|
|
|
|
482
|
|
|
|
483
|
63 |
|
if ($data === null) { |
484
|
|
|
$this->parameters = '{}'; |
485
|
|
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
63 |
|
if (is_string($data)) { |
|
|
|
|
489
|
63 |
|
$this->parameters = $data; |
490
|
63 |
|
return $this; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
$this->parameters = json_encode($data); |
|
|
|
|
494
|
|
|
|
495
|
|
|
if (!$this->parameters && $data) { |
|
|
|
|
496
|
|
|
throw new \ClickHouseDB\Exception\TransportException('Cant json_encode: ' . strval($data)); |
|
|
|
|
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
return $this; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @return resource |
504
|
|
|
*/ |
505
|
|
|
public function getResultFileHandle() |
506
|
|
|
{ |
507
|
|
|
return $this->resultFileHandle; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @return bool |
512
|
|
|
*/ |
513
|
|
|
public function isResultFile() |
|
|
|
|
514
|
|
|
{ |
515
|
|
|
return ($this->resultFileHandle ? true : false); |
|
|
|
|
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @param resource $h resource |
|
|
|
|
520
|
|
|
* @param bool $zlib |
|
|
|
|
521
|
|
|
* @return $this |
522
|
|
|
*/ |
523
|
1 |
|
public function setResultFileHandle($h, $zlib = false) |
524
|
|
|
{ |
525
|
1 |
|
$this->resultFileHandle = $h; |
526
|
1 |
|
if ($zlib) { |
527
|
|
|
$params = array('level' => 6, 'window' => 15, 'memory' => 9); |
|
|
|
|
528
|
|
|
stream_filter_append($this->resultFileHandle, 'zlib.deflate', STREAM_FILTER_WRITE, $params); |
|
|
|
|
529
|
|
|
} |
530
|
1 |
|
return $this; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @return CurlerRequest |
535
|
|
|
*/ |
536
|
|
|
public function PUT() |
|
|
|
|
537
|
|
|
{ |
538
|
|
|
return $this->execute('PUT'); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* @return CurlerRequest |
543
|
|
|
*/ |
544
|
63 |
|
public function POST() |
|
|
|
|
545
|
|
|
{ |
546
|
63 |
|
return $this->execute('POST'); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* @return CurlerRequest |
551
|
|
|
*/ |
552
|
|
|
public function OPTIONS() |
|
|
|
|
553
|
|
|
{ |
554
|
|
|
return $this->execute('OPTIONS'); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* @return CurlerRequest |
559
|
|
|
*/ |
560
|
39 |
|
public function GET() |
|
|
|
|
561
|
|
|
{ |
562
|
39 |
|
return $this->execute('GET'); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* The number of seconds that DNS records are stored in memory. By default this parameter is 120 (2 minutes). |
567
|
|
|
* |
568
|
|
|
* @param integer $set |
|
|
|
|
569
|
|
|
* @return $this |
570
|
|
|
*/ |
571
|
|
|
public function setDnsCache($set) |
|
|
|
|
572
|
|
|
{ |
573
|
|
|
$this->_dns_cache = $set; |
574
|
|
|
return $this; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* The number of seconds that DNS records are stored in memory. By default this parameter is 120 (2 minutes). |
579
|
|
|
* |
580
|
|
|
* @return int |
581
|
|
|
*/ |
582
|
63 |
|
public function getDnsCache() |
|
|
|
|
583
|
|
|
{ |
584
|
63 |
|
return $this->_dns_cache; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @param string $method |
|
|
|
|
589
|
|
|
* @return $this |
590
|
|
|
*/ |
591
|
63 |
|
private function execute($method) |
|
|
|
|
592
|
|
|
{ |
593
|
63 |
|
$this->method = $method; |
594
|
63 |
|
return $this; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* @return CurlerResponse |
|
|
|
|
599
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
600
|
|
|
*/ |
601
|
63 |
|
public function response() |
|
|
|
|
602
|
|
|
{ |
603
|
63 |
|
if (! $this->response) { |
604
|
|
|
throw new \ClickHouseDB\Exception\TransportException('Can`t fetch response - is empty'); |
|
|
|
|
605
|
|
|
} |
606
|
|
|
|
607
|
63 |
|
return $this->response; |
608
|
|
|
} |
609
|
|
|
|
610
|
29 |
|
public function isResponseExists() : bool |
611
|
|
|
{ |
612
|
29 |
|
return $this->response !== null; |
613
|
|
|
} |
614
|
|
|
|
615
|
63 |
|
public function setResponse(CurlerResponse $response) : void |
616
|
|
|
{ |
617
|
63 |
|
$this->response = $response; |
618
|
63 |
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* @return mixed |
622
|
|
|
*/ |
623
|
63 |
|
public function handle() |
624
|
|
|
{ |
625
|
63 |
|
$this->prepareRequest(); |
626
|
63 |
|
return $this->handle; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* @param callable $callback |
|
|
|
|
631
|
|
|
* @throws \Exception |
|
|
|
|
632
|
|
|
*/ |
633
|
1 |
|
public function setFunctionProgress(callable $callback) |
|
|
|
|
634
|
|
|
{ |
635
|
1 |
|
if (!is_callable($callback)) { |
|
|
|
|
636
|
|
|
throw new \Exception('setFunctionProgress not is_callable'); |
|
|
|
|
637
|
|
|
} |
638
|
|
|
|
639
|
1 |
|
$this->option(CURLOPT_NOPROGRESS, false); |
|
|
|
|
640
|
1 |
|
$this->option(CURLOPT_PROGRESSFUNCTION, $callback); // version 5.5.0 |
|
|
|
|
641
|
1 |
|
} |
642
|
|
|
|
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* @return bool |
646
|
|
|
*/ |
647
|
63 |
|
private function prepareRequest() |
|
|
|
|
648
|
|
|
{ |
649
|
63 |
|
if (!$this->handle) { |
|
|
|
|
650
|
63 |
|
$this->handle = curl_init(); |
|
|
|
|
651
|
|
|
} |
652
|
|
|
|
653
|
63 |
|
$curl_opt = $this->options; |
654
|
63 |
|
$method = $this->method; |
|
|
|
|
655
|
|
|
|
656
|
63 |
|
if ($this->_attachFiles) { |
657
|
1 |
|
$curl_opt[CURLOPT_SAFE_UPLOAD] = true; |
|
|
|
|
658
|
|
|
} |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
661
|
63 |
|
if (strtoupper($method) == 'GET') { |
|
|
|
|
662
|
39 |
|
$curl_opt[CURLOPT_HTTPGET] = true; |
663
|
39 |
|
$curl_opt[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
|
|
|
|
664
|
39 |
|
$curl_opt[CURLOPT_POSTFIELDS] = false; |
|
|
|
|
665
|
|
|
} else { |
666
|
63 |
|
if (strtoupper($method) === 'POST') { |
|
|
|
|
667
|
63 |
|
$curl_opt[CURLOPT_POST] = true; |
668
|
|
|
} |
669
|
|
|
|
670
|
63 |
|
$curl_opt[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
|
|
|
|
671
|
|
|
|
672
|
63 |
|
if ($this->parameters) { |
673
|
63 |
|
$curl_opt[CURLOPT_POSTFIELDS] = $this->parameters; |
|
|
|
|
674
|
|
|
|
675
|
63 |
|
if (!is_array($this->parameters)) { |
|
|
|
|
676
|
63 |
|
$this->header('Content-Length', strlen($this->parameters)); |
|
|
|
|
677
|
|
|
} |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
// CURLOPT_DNS_CACHE_TIMEOUT - Количество секунд, в течение которых в памяти хранятся DNS-записи. |
681
|
63 |
|
$curl_opt[CURLOPT_DNS_CACHE_TIMEOUT] = $this->getDnsCache(); |
|
|
|
|
682
|
63 |
|
$curl_opt[CURLOPT_URL] = $this->url; |
|
|
|
|
683
|
|
|
|
684
|
63 |
|
if (!empty($this->headers) && sizeof($this->headers)) { |
|
|
|
|
685
|
63 |
|
$curl_opt[CURLOPT_HTTPHEADER] = array(); |
|
|
|
|
686
|
|
|
|
687
|
63 |
|
foreach ($this->headers as $key => $value) { |
688
|
63 |
|
$curl_opt[CURLOPT_HTTPHEADER][] = sprintf("%s: %s", $key, $value); |
|
|
|
|
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
|
692
|
63 |
|
if (!empty($curl_opt[CURLOPT_INFILE])) { |
|
|
|
|
693
|
|
|
|
694
|
8 |
|
$curl_opt[CURLOPT_PUT] = true; |
|
|
|
|
695
|
|
|
} |
696
|
|
|
|
697
|
63 |
|
if (!empty($curl_opt[CURLOPT_WRITEFUNCTION])) |
|
|
|
|
698
|
|
|
{ |
699
|
1 |
|
$curl_opt[CURLOPT_HEADER]=false; |
|
|
|
|
700
|
|
|
} |
701
|
|
|
|
702
|
63 |
|
if ($this->resultFileHandle) { |
703
|
1 |
|
$curl_opt[CURLOPT_FILE] = $this->resultFileHandle; |
|
|
|
|
704
|
1 |
|
$curl_opt[CURLOPT_HEADER] = false; |
|
|
|
|
705
|
|
|
} |
706
|
|
|
|
707
|
63 |
|
if ($this->options[CURLOPT_VERBOSE]) { |
|
|
|
|
708
|
|
|
echo "\n-----------BODY REQUEST----------\n" . $curl_opt[CURLOPT_POSTFIELDS] . "\n------END--------\n"; |
|
|
|
|
709
|
|
|
} |
710
|
63 |
|
curl_setopt_array($this->handle, $curl_opt); |
|
|
|
|
711
|
63 |
|
return true; |
712
|
|
|
} |
713
|
|
|
} |
714
|
|
|
|