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