1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Transport; |
4
|
|
|
|
5
|
|
|
class CurlerResponse |
6
|
|
|
{ |
7
|
|
|
/** |
|
|
|
|
8
|
|
|
* @var mixed |
9
|
|
|
*/ |
10
|
|
|
public $_headers; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
|
|
|
|
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
public $_info; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
public $_error; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
public $_errorNo = 0; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* @var float |
29
|
|
|
*/ |
30
|
|
|
public $_useTime; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $_body; |
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Response constructor. |
|
|
|
|
40
|
|
|
*/ |
41
|
49 |
|
public function __construct() {} |
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
39 |
|
public function error_no() |
|
|
|
|
48
|
|
|
{ |
49
|
39 |
|
return $this->_errorNo; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
6 |
|
public function error() |
56
|
|
|
{ |
57
|
6 |
|
return $this->_error; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function url() |
64
|
|
|
{ |
65
|
|
|
return $this->_info['url']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
2 |
|
public function total_time() |
|
|
|
|
72
|
|
|
{ |
73
|
2 |
|
return round($this->_info['total_time'], 3); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public function starttransfer_time() |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
return round($this->_info['starttransfer_time'], 3); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function connect_time() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return round($this->_info['connect_time'], 3); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function pretransfer_time() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return round($this->_info['pretransfer_time'], 3); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function content_type() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
return $this->_info['content_type']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
49 |
|
public function http_code() |
|
|
|
|
112
|
|
|
{ |
113
|
49 |
|
return $this->_info['http_code']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $name |
|
|
|
|
118
|
|
|
* @return null|string |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
public function headers($name) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
if (isset($this->_headers[$name])) { |
123
|
|
|
return $this->_headers[$name]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return null|string |
|
|
|
|
131
|
|
|
*/ |
132
|
|
|
public function connection() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
return $this->headers('Connection'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
48 |
|
public function body() |
141
|
|
|
{ |
142
|
48 |
|
return $this->_body; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function as_string() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
return $this->body(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
|
|
|
|
154
|
|
|
* |
155
|
|
|
*/ |
156
|
|
|
public function dump_json() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
print_r($this->json()); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function getDetails(): array |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
return [ |
164
|
1 |
|
'body' => $this->_body, |
165
|
1 |
|
'headers' => $this->_headers, |
166
|
1 |
|
'error' => $this->error(), |
167
|
1 |
|
'info' => $this->_info, |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param bool $result |
|
|
|
|
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function dump($result = false) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$msg = "\n--------------------------- Response -------------------------------------\nBODY:\n"; |
|
|
|
|
178
|
|
|
$msg .= print_r($this->_body, true); |
|
|
|
|
179
|
|
|
$msg .= "\nHEAD:\n"; |
180
|
|
|
$msg .= print_r($this->_headers, true); |
|
|
|
|
181
|
|
|
$msg .= "\nERROR:\n" . $this->error(); |
182
|
|
|
$msg .= "\nINFO:\n"; |
183
|
|
|
$msg .= json_encode($this->_info); |
|
|
|
|
184
|
|
|
$msg .= "\n----------------------------------------------------------------------\n"; |
185
|
|
|
|
186
|
|
|
if ($result) { |
187
|
|
|
return $msg; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
echo $msg; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param int $size |
|
|
|
|
195
|
|
|
* @param string $unit |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
1 |
|
private function humanFileSize($size, $unit = '') |
|
|
|
|
199
|
|
|
{ |
200
|
1 |
|
if ((!$unit && $size >= 1 << 30) || $unit == 'GB') { |
|
|
|
|
201
|
|
|
return number_format($size / (1 << 30), 2) . ' GB'; |
|
|
|
|
202
|
|
|
} |
203
|
1 |
|
if ((!$unit && $size >= 1 << 20) || $unit == 'MB') { |
|
|
|
|
204
|
|
|
return number_format($size / (1 << 20), 2) . ' MB'; |
|
|
|
|
205
|
|
|
} |
206
|
1 |
|
if ((!$unit && $size >= 1 << 10) || $unit == 'KB') { |
|
|
|
|
207
|
|
|
return number_format($size / (1 << 10), 2) . ' KB'; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
return number_format($size) . ' bytes'; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
1 |
|
public function upload_content_length() |
|
|
|
|
217
|
|
|
{ |
218
|
1 |
|
return $this->humanFileSize($this->_info['upload_content_length']); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
1 |
|
public function speed_upload() |
|
|
|
|
225
|
|
|
{ |
226
|
1 |
|
$SPEED_UPLOAD = $this->_info['speed_upload']; |
|
|
|
|
227
|
1 |
|
return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps'; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
1 |
|
public function speed_download() |
|
|
|
|
234
|
|
|
{ |
235
|
1 |
|
$SPEED_UPLOAD = $this->_info['speed_download']; |
|
|
|
|
236
|
1 |
|
return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps'; |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
1 |
|
public function size_upload() |
|
|
|
|
243
|
|
|
{ |
244
|
1 |
|
return $this->humanFileSize($this->_info['size_upload']); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function request_size() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
return $this->humanFileSize($this->_info['request_size']); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
public function header_size() |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
return $this->humanFileSize($this->_info['header_size']); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
1 |
|
public function size_download() |
|
|
|
|
267
|
|
|
{ |
268
|
1 |
|
return $this->humanFileSize($this->_info['size_download']); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return mixed |
273
|
|
|
*/ |
274
|
1 |
|
public function info() |
275
|
|
|
{ |
276
|
1 |
|
return $this->_info; |
277
|
|
|
} |
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string|null $key |
|
|
|
|
280
|
|
|
* @return bool|mixed |
281
|
|
|
*/ |
282
|
30 |
|
public function json($key = null) |
|
|
|
|
283
|
|
|
{ |
284
|
30 |
|
$d = json_decode($this->body(), true); |
|
|
|
|
285
|
|
|
|
286
|
30 |
|
if (!$key) { |
|
|
|
|
287
|
30 |
|
return $d; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if (!isset($d[$key])) { |
|
|
|
|
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $d[$key]; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return mixed |
299
|
|
|
*/ |
300
|
31 |
|
public function rawDataOrJson($format) |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
// JSONCompact // JSONEachRow |
303
|
|
|
|
304
|
31 |
|
if (stripos($format, 'json') !== false) |
|
|
|
|
305
|
|
|
{ |
306
|
30 |
|
if (stripos($format,'JSONEachRow')===false) |
|
|
|
|
307
|
30 |
|
return $this->json(); |
|
|
|
|
308
|
|
|
} |
309
|
2 |
|
return $this->body(); |
|
|
|
|
310
|
|
|
|
311
|
|
|
} |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|