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