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