@@ -20,614 +20,614 @@ |
||
20 | 20 | */ |
21 | 21 | class Request extends Http |
22 | 22 | { |
23 | - /** |
|
24 | - * you can implement more traits |
|
25 | - */ |
|
26 | - use JsonTrait; |
|
27 | - |
|
28 | - protected static $curlAlias = array( |
|
29 | - 'url' => 'CURLOPT_URL', |
|
30 | - 'uri' => 'CURLOPT_URL', |
|
31 | - 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
32 | - 'method' => 'CURLOPT_CUSTOMREQUEST', |
|
33 | - 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
|
34 | - 'ua' => 'CURLOPT_USERAGENT', |
|
35 | - 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
|
36 | - 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
|
37 | - 'referer' => 'CURLOPT_REFERER', |
|
38 | - 'binary' => 'CURLOPT_BINARYTRANSFER', |
|
39 | - 'port' => 'CURLOPT_PORT', |
|
40 | - 'header' => 'CURLOPT_HEADER', // TRUE:include header |
|
41 | - 'headers' => 'CURLOPT_HTTPHEADER', // array |
|
42 | - 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
|
43 | - 'upload' => 'CURLOPT_INFILE', // reading file stream |
|
44 | - 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
|
45 | - 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
|
46 | - 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
|
47 | - 'expects_mime' => null, //expected mime |
|
48 | - 'send_mime' => null, //send mime |
|
49 | - 'ip' => null,//specify ip to send request |
|
50 | - 'callback' => null,//callback on end |
|
51 | - |
|
52 | - ); |
|
53 | - protected static $loggerHandler; |
|
54 | - public |
|
55 | - $curlHandle, |
|
56 | - $uri, |
|
57 | - $sendMime, |
|
58 | - $expectedMime, |
|
59 | - $timeout, |
|
60 | - $maxRedirects, |
|
61 | - $encoding, |
|
62 | - $payload, |
|
63 | - $retryTimes, |
|
64 | - /** |
|
65 | - * @var int seconds |
|
66 | - */ |
|
67 | - $retryDuration, |
|
68 | - $followRedirects; |
|
69 | - |
|
70 | - protected |
|
71 | - $body, |
|
72 | - $endCallback, |
|
73 | - $withURIQuery, |
|
74 | - $hasInitialized = false, |
|
75 | - /** |
|
76 | - * @var array |
|
77 | - */ |
|
78 | - $options = array( |
|
79 | - 'CURLOPT_MAXREDIRS' => 10, |
|
80 | - 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
|
81 | - 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
|
82 | - 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
|
23 | + /** |
|
24 | + * you can implement more traits |
|
25 | + */ |
|
26 | + use JsonTrait; |
|
27 | + |
|
28 | + protected static $curlAlias = array( |
|
29 | + 'url' => 'CURLOPT_URL', |
|
30 | + 'uri' => 'CURLOPT_URL', |
|
31 | + 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
32 | + 'method' => 'CURLOPT_CUSTOMREQUEST', |
|
33 | + 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
|
34 | + 'ua' => 'CURLOPT_USERAGENT', |
|
35 | + 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
|
36 | + 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
|
37 | + 'referer' => 'CURLOPT_REFERER', |
|
38 | + 'binary' => 'CURLOPT_BINARYTRANSFER', |
|
39 | + 'port' => 'CURLOPT_PORT', |
|
40 | + 'header' => 'CURLOPT_HEADER', // TRUE:include header |
|
41 | + 'headers' => 'CURLOPT_HTTPHEADER', // array |
|
42 | + 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
|
43 | + 'upload' => 'CURLOPT_INFILE', // reading file stream |
|
44 | + 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
|
45 | + 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
|
46 | + 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
|
47 | + 'expects_mime' => null, //expected mime |
|
48 | + 'send_mime' => null, //send mime |
|
49 | + 'ip' => null,//specify ip to send request |
|
50 | + 'callback' => null,//callback on end |
|
51 | + |
|
52 | + ); |
|
53 | + protected static $loggerHandler; |
|
54 | + public |
|
55 | + $curlHandle, |
|
56 | + $uri, |
|
57 | + $sendMime, |
|
58 | + $expectedMime, |
|
59 | + $timeout, |
|
60 | + $maxRedirects, |
|
61 | + $encoding, |
|
62 | + $payload, |
|
63 | + $retryTimes, |
|
64 | + /** |
|
65 | + * @var int seconds |
|
66 | + */ |
|
67 | + $retryDuration, |
|
68 | + $followRedirects; |
|
69 | + |
|
70 | + protected |
|
71 | + $body, |
|
72 | + $endCallback, |
|
73 | + $withURIQuery, |
|
74 | + $hasInitialized = false, |
|
75 | + /** |
|
76 | + * @var array |
|
77 | + */ |
|
78 | + $options = array( |
|
79 | + 'CURLOPT_MAXREDIRS' => 10, |
|
80 | + 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
|
81 | + 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
|
82 | + 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
|
83 | 83 | // 'CURLOPT_SAFE_UPLOAD' => false,// compatible with PHP 5.6.0 |
84 | - 'header' => true, |
|
85 | - 'method' => self::GET, |
|
86 | - 'transfer' => true, |
|
87 | - 'headers' => array(), |
|
88 | - 'follow_location' => true, |
|
89 | - 'timeout' => 0, |
|
90 | - // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
|
91 | - 'retry_times' => 1,//redo task when failed |
|
92 | - 'retry_duration' => 0,//in seconds |
|
93 | - ); |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Request constructor. |
|
98 | - */ |
|
99 | - protected function __construct() |
|
100 | - { |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @return Request |
|
105 | - */ |
|
106 | - public static function create() |
|
107 | - { |
|
108 | - return new self; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param callable $handler |
|
113 | - */ |
|
114 | - public static function setLogHandler(callable $handler) |
|
115 | - { |
|
116 | - self::$loggerHandler = $handler; |
|
117 | - } |
|
118 | - /** |
|
119 | - * Specify timeout |
|
120 | - * @param float|int $timeout seconds to timeout the HTTP call |
|
121 | - * @return Request |
|
122 | - */ |
|
123 | - public function timeout($timeout) |
|
124 | - { |
|
125 | - $this->timeout = $timeout; |
|
126 | - return $this; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return Request |
|
131 | - */ |
|
132 | - public function noFollow() |
|
133 | - { |
|
134 | - return $this->follow(0); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * If the response is a 301 or 302 redirect, automatically |
|
139 | - * send off another request to that location |
|
140 | - * @param int $follow follow or not to follow or maximal number of redirects |
|
141 | - * @return Request |
|
142 | - */ |
|
143 | - public function follow($follow) |
|
144 | - { |
|
145 | - $this->maxRedirects = abs($follow); |
|
146 | - $this->followRedirects = $follow > 0; |
|
147 | - return $this; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param $parsedComponents |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - private static function combineUrl($parsedComponents) |
|
155 | - { |
|
156 | - $scheme = isset($parsedComponents['scheme']) ? $parsedComponents['scheme'] . '://' : ''; |
|
157 | - $host = isset($parsedComponents['host']) ? $parsedComponents['host'] : ''; |
|
158 | - $port = isset($parsedComponents['port']) ? ':' . $parsedComponents['port'] : ''; |
|
159 | - $user = isset($parsedComponents['user']) ? $parsedComponents['user'] : ''; |
|
160 | - $pass = isset($parsedComponents['pass']) ? ':' . $parsedComponents['pass'] : ''; |
|
161 | - $pass = ($user || $pass) ? "$pass@" : ''; |
|
162 | - $path = isset($parsedComponents['path']) ? $parsedComponents['path'] : ''; |
|
163 | - $query = isset($parsedComponents['query']) ? '?' . $parsedComponents['query'] : ''; |
|
164 | - $fragment = isset($parsedComponents['fragment']) ? '#' . $parsedComponents['fragment'] : ''; |
|
165 | - return "$scheme$user$pass$host$port$path$query$fragment"; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param string $mime |
|
170 | - * @return $this |
|
171 | - */ |
|
172 | - public function expectsMime($mime = 'json') |
|
173 | - { |
|
174 | - $this->expectedMime = $mime; |
|
175 | - return $this; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param string $mime |
|
180 | - * @return Request |
|
181 | - */ |
|
182 | - public function sendMime($mime = 'json') |
|
183 | - { |
|
184 | - $this->sendMime = $mime; |
|
84 | + 'header' => true, |
|
85 | + 'method' => self::GET, |
|
86 | + 'transfer' => true, |
|
87 | + 'headers' => array(), |
|
88 | + 'follow_location' => true, |
|
89 | + 'timeout' => 0, |
|
90 | + // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
|
91 | + 'retry_times' => 1,//redo task when failed |
|
92 | + 'retry_duration' => 0,//in seconds |
|
93 | + ); |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Request constructor. |
|
98 | + */ |
|
99 | + protected function __construct() |
|
100 | + { |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @return Request |
|
105 | + */ |
|
106 | + public static function create() |
|
107 | + { |
|
108 | + return new self; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param callable $handler |
|
113 | + */ |
|
114 | + public static function setLogHandler(callable $handler) |
|
115 | + { |
|
116 | + self::$loggerHandler = $handler; |
|
117 | + } |
|
118 | + /** |
|
119 | + * Specify timeout |
|
120 | + * @param float|int $timeout seconds to timeout the HTTP call |
|
121 | + * @return Request |
|
122 | + */ |
|
123 | + public function timeout($timeout) |
|
124 | + { |
|
125 | + $this->timeout = $timeout; |
|
126 | + return $this; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return Request |
|
131 | + */ |
|
132 | + public function noFollow() |
|
133 | + { |
|
134 | + return $this->follow(0); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * If the response is a 301 or 302 redirect, automatically |
|
139 | + * send off another request to that location |
|
140 | + * @param int $follow follow or not to follow or maximal number of redirects |
|
141 | + * @return Request |
|
142 | + */ |
|
143 | + public function follow($follow) |
|
144 | + { |
|
145 | + $this->maxRedirects = abs($follow); |
|
146 | + $this->followRedirects = $follow > 0; |
|
147 | + return $this; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param $parsedComponents |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + private static function combineUrl($parsedComponents) |
|
155 | + { |
|
156 | + $scheme = isset($parsedComponents['scheme']) ? $parsedComponents['scheme'] . '://' : ''; |
|
157 | + $host = isset($parsedComponents['host']) ? $parsedComponents['host'] : ''; |
|
158 | + $port = isset($parsedComponents['port']) ? ':' . $parsedComponents['port'] : ''; |
|
159 | + $user = isset($parsedComponents['user']) ? $parsedComponents['user'] : ''; |
|
160 | + $pass = isset($parsedComponents['pass']) ? ':' . $parsedComponents['pass'] : ''; |
|
161 | + $pass = ($user || $pass) ? "$pass@" : ''; |
|
162 | + $path = isset($parsedComponents['path']) ? $parsedComponents['path'] : ''; |
|
163 | + $query = isset($parsedComponents['query']) ? '?' . $parsedComponents['query'] : ''; |
|
164 | + $fragment = isset($parsedComponents['fragment']) ? '#' . $parsedComponents['fragment'] : ''; |
|
165 | + return "$scheme$user$pass$host$port$path$query$fragment"; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param string $mime |
|
170 | + * @return $this |
|
171 | + */ |
|
172 | + public function expectsMime($mime = 'json') |
|
173 | + { |
|
174 | + $this->expectedMime = $mime; |
|
175 | + return $this; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param string $mime |
|
180 | + * @return Request |
|
181 | + */ |
|
182 | + public function sendMime($mime = 'json') |
|
183 | + { |
|
184 | + $this->sendMime = $mime; |
|
185 | 185 | // $this->addHeader('Content-type', Mime::getFullMime($mime)); |
186 | - return $this; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @param $headerName |
|
191 | - * @param $value , can be rawurlencode |
|
192 | - * @return $this |
|
193 | - */ |
|
194 | - public function addHeader($headerName, $value) |
|
195 | - { |
|
196 | - $this->options['headers'][] = $headerName . ': ' . $value; |
|
197 | - return $this; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @param $uri |
|
202 | - * @return $this |
|
203 | - */ |
|
204 | - public function uri($uri) |
|
205 | - { |
|
206 | - $this->uri = $uri; |
|
207 | - return $this; |
|
208 | - } |
|
209 | - |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * @param array $headers |
|
214 | - * @return $this |
|
215 | - */ |
|
216 | - public function addHeaders(array $headers) |
|
217 | - { |
|
218 | - foreach ($headers as $header => $value) { |
|
219 | - $this->addHeader($header, $value); |
|
220 | - } |
|
221 | - return $this; |
|
222 | - } |
|
223 | - public function expectsType($mime) |
|
224 | - { |
|
225 | - return $this->expects($mime); |
|
226 | - } |
|
227 | - public function sendType($mime) |
|
228 | - { |
|
229 | - return $this->contentType = $mime; |
|
230 | - } |
|
231 | - public function expects($mime) |
|
232 | - { |
|
233 | - if (empty($mime)) return $this; |
|
234 | - $this->expected_type = Mime::getFullMime($mime); |
|
235 | - return $this; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @return mixed |
|
240 | - */ |
|
241 | - public function endCallback() |
|
242 | - { |
|
243 | - return $this->endCallback; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * @return bool |
|
248 | - */ |
|
249 | - public function hasEndCallback() |
|
250 | - { |
|
251 | - return isset($this->endCallback); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * @param $field alias or field name |
|
256 | - * @return bool|mixed |
|
257 | - */ |
|
258 | - public function getIni($field = null) |
|
259 | - { |
|
260 | - if(!$field) return $this->options; |
|
261 | - $full = self::fullOption($field); |
|
262 | - return isset($this->options[$full]) ? $this->options[$full] : false; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @param $key |
|
267 | - * @return mixed |
|
268 | - */ |
|
269 | - protected static function fullOption($key) |
|
270 | - { |
|
271 | - $full = false; |
|
272 | - if (isset(self::$curlAlias[$key])) { |
|
273 | - $full = self::$curlAlias[$key]; |
|
274 | - } elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) { |
|
275 | - $full = $key; |
|
276 | - } |
|
277 | - return $full; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @param $queryData |
|
282 | - * @return $this |
|
283 | - */ |
|
284 | - public function addQuery($queryData) |
|
285 | - { |
|
286 | - if (!empty($queryData)) { |
|
287 | - if (is_array($queryData)) { |
|
288 | - $this->withURIQuery = http_build_query($queryData); |
|
289 | - } else if (is_string($queryData)) { |
|
290 | - $this->withURIQuery = $queryData; |
|
291 | - } else { |
|
292 | - throw new InvalidArgumentException('data must be array or string'); |
|
293 | - } |
|
294 | - } |
|
295 | - return $this; |
|
296 | - } |
|
297 | - /** |
|
298 | - * @param $uri |
|
299 | - * @param null $payload |
|
300 | - * @param array $options |
|
301 | - * @return Request |
|
302 | - */ |
|
303 | - public function post($uri, $payload = null, array $options = array()) |
|
304 | - { |
|
305 | - return $this->ini(Http::POST, $uri, $payload, $options); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * @param $uri |
|
310 | - * @param null $payload |
|
311 | - * @param array $options |
|
312 | - * @param null $response |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
|
316 | - { |
|
317 | - $response = $this->post($uri, $payload, $options)->send(); |
|
318 | - return $response->body; |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * @param $method |
|
324 | - * @param $url |
|
325 | - * @param $data |
|
326 | - * @param array $options |
|
327 | - * @return $this |
|
328 | - */ |
|
329 | - protected function ini($method, $url, $data, array $options = array()) |
|
330 | - { |
|
331 | - $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
332 | - $this->addOptions($options); |
|
333 | - |
|
334 | - return $this; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param array $options |
|
339 | - * @return $this |
|
340 | - */ |
|
341 | - public function addOptions(array $options = array()) |
|
342 | - { |
|
343 | - $this->options = $options + $this->options; |
|
344 | - $this->uri = $this->options['url']; |
|
345 | - return $this; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @param $uri |
|
350 | - * @param null $payload |
|
351 | - * @param array $options |
|
352 | - * @return Request |
|
353 | - */ |
|
354 | - function put($uri, $payload = null, array $options = array()) |
|
355 | - { |
|
356 | - return $this->ini(Http::PUT, $uri, $payload, $options); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * @param $uri |
|
361 | - * @param null $payload |
|
362 | - * @param array $options |
|
363 | - * @return Request |
|
364 | - */ |
|
365 | - function patch($uri, $payload = null, array $options = array()) |
|
366 | - { |
|
367 | - return $this->ini(Http::PATCH, $uri, $payload, $options); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * @param $uri |
|
372 | - * @param array $options |
|
373 | - * @return Request |
|
374 | - */ |
|
375 | - public function get($uri, array $options = array()) |
|
376 | - { |
|
377 | - return $this->ini(Http::GET, $uri, array(), $options); |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * @param $uri |
|
383 | - * @param array $options |
|
384 | - * @param null $response |
|
385 | - * @return string |
|
386 | - */ |
|
387 | - public function quickGet($uri, array $options = array(), &$response = null) |
|
388 | - { |
|
389 | - $response = $this->get($uri, $options)->send(); |
|
390 | - return $response->body; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * @param $uri |
|
395 | - * @param array $options |
|
396 | - * @return Request |
|
397 | - */ |
|
398 | - function options($uri, array $options = array()) |
|
399 | - { |
|
400 | - return $this->ini(Http::OPTIONS, $uri, array(), $options); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * @param $uri |
|
405 | - * @param array $options |
|
406 | - * @return Request |
|
407 | - */ |
|
408 | - function head($uri, array $options = array()) |
|
409 | - { |
|
410 | - return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * @param $uri |
|
415 | - * @param array $options |
|
416 | - * @return Request |
|
417 | - */ |
|
418 | - function delete($uri, array $options = array()) |
|
419 | - { |
|
420 | - return $this->ini(Http::DELETE, $uri, array(), $options); |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * @param $uri |
|
425 | - * @param array $options |
|
426 | - * @return Request |
|
427 | - */ |
|
428 | - function trace($uri, array $options = array()) |
|
429 | - { |
|
430 | - return $this->ini(Http::TRACE, $uri, array(), $options); |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * @param bool $isMultiCurl |
|
435 | - * @return Response |
|
436 | - */ |
|
437 | - public function send($isMultiCurl = false) |
|
438 | - { |
|
439 | - try { |
|
440 | - if (!$this->hasInitialized) |
|
441 | - $this->applyOptions(); |
|
442 | - $response = $this->makeResponse($isMultiCurl); |
|
443 | - $response->parse(); |
|
444 | - } catch (\Exception $e) { |
|
445 | - if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
446 | - $response->error = $e->getMessage(); |
|
447 | - $response->errorCode = 999; |
|
448 | - } |
|
449 | - |
|
450 | - if (self::$loggerHandler) { |
|
451 | - call_user_func(self::$loggerHandler, $response); |
|
452 | - } |
|
453 | - if ($this->endCallback) { |
|
454 | - call_user_func($this->endCallback, $response); |
|
455 | - } |
|
456 | - |
|
457 | - return $response; |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * @return $this |
|
462 | - */ |
|
463 | - public function applyOptions() |
|
464 | - { |
|
465 | - $curl = curl_init(); |
|
466 | - $this->curlHandle = $curl; |
|
467 | - $this->prepare(); |
|
468 | - $this->hasInitialized = true; |
|
469 | - return $this; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * @return $this |
|
474 | - */ |
|
475 | - protected function prepare() |
|
476 | - { |
|
477 | - $this->options['url'] = trim($this->options['url']); |
|
478 | - if (empty($this->options['url'])) { |
|
479 | - throw new InvalidArgumentException('url can not empty'); |
|
480 | - } |
|
481 | - |
|
482 | - if (isset($this->options['retry_times'])) { |
|
483 | - $this->retryTimes = abs($this->options['retry_times']); |
|
484 | - } |
|
485 | - |
|
486 | - if (isset($this->options['retry_duration'])) { |
|
487 | - $this->retryDuration = abs($this->options['retry_duration']); |
|
488 | - } |
|
489 | - |
|
490 | - if(isset($this->options['expects_mime'])){ |
|
491 | - $this->expectsMime($this->options['expects_mime']); |
|
492 | - } |
|
493 | - |
|
494 | - if(isset($this->options['send_mime'])){ |
|
495 | - $this->sendMime($this->options['send_mime']); |
|
496 | - } |
|
186 | + return $this; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @param $headerName |
|
191 | + * @param $value , can be rawurlencode |
|
192 | + * @return $this |
|
193 | + */ |
|
194 | + public function addHeader($headerName, $value) |
|
195 | + { |
|
196 | + $this->options['headers'][] = $headerName . ': ' . $value; |
|
197 | + return $this; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @param $uri |
|
202 | + * @return $this |
|
203 | + */ |
|
204 | + public function uri($uri) |
|
205 | + { |
|
206 | + $this->uri = $uri; |
|
207 | + return $this; |
|
208 | + } |
|
209 | + |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * @param array $headers |
|
214 | + * @return $this |
|
215 | + */ |
|
216 | + public function addHeaders(array $headers) |
|
217 | + { |
|
218 | + foreach ($headers as $header => $value) { |
|
219 | + $this->addHeader($header, $value); |
|
220 | + } |
|
221 | + return $this; |
|
222 | + } |
|
223 | + public function expectsType($mime) |
|
224 | + { |
|
225 | + return $this->expects($mime); |
|
226 | + } |
|
227 | + public function sendType($mime) |
|
228 | + { |
|
229 | + return $this->contentType = $mime; |
|
230 | + } |
|
231 | + public function expects($mime) |
|
232 | + { |
|
233 | + if (empty($mime)) return $this; |
|
234 | + $this->expected_type = Mime::getFullMime($mime); |
|
235 | + return $this; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @return mixed |
|
240 | + */ |
|
241 | + public function endCallback() |
|
242 | + { |
|
243 | + return $this->endCallback; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * @return bool |
|
248 | + */ |
|
249 | + public function hasEndCallback() |
|
250 | + { |
|
251 | + return isset($this->endCallback); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * @param $field alias or field name |
|
256 | + * @return bool|mixed |
|
257 | + */ |
|
258 | + public function getIni($field = null) |
|
259 | + { |
|
260 | + if(!$field) return $this->options; |
|
261 | + $full = self::fullOption($field); |
|
262 | + return isset($this->options[$full]) ? $this->options[$full] : false; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @param $key |
|
267 | + * @return mixed |
|
268 | + */ |
|
269 | + protected static function fullOption($key) |
|
270 | + { |
|
271 | + $full = false; |
|
272 | + if (isset(self::$curlAlias[$key])) { |
|
273 | + $full = self::$curlAlias[$key]; |
|
274 | + } elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) { |
|
275 | + $full = $key; |
|
276 | + } |
|
277 | + return $full; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @param $queryData |
|
282 | + * @return $this |
|
283 | + */ |
|
284 | + public function addQuery($queryData) |
|
285 | + { |
|
286 | + if (!empty($queryData)) { |
|
287 | + if (is_array($queryData)) { |
|
288 | + $this->withURIQuery = http_build_query($queryData); |
|
289 | + } else if (is_string($queryData)) { |
|
290 | + $this->withURIQuery = $queryData; |
|
291 | + } else { |
|
292 | + throw new InvalidArgumentException('data must be array or string'); |
|
293 | + } |
|
294 | + } |
|
295 | + return $this; |
|
296 | + } |
|
297 | + /** |
|
298 | + * @param $uri |
|
299 | + * @param null $payload |
|
300 | + * @param array $options |
|
301 | + * @return Request |
|
302 | + */ |
|
303 | + public function post($uri, $payload = null, array $options = array()) |
|
304 | + { |
|
305 | + return $this->ini(Http::POST, $uri, $payload, $options); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * @param $uri |
|
310 | + * @param null $payload |
|
311 | + * @param array $options |
|
312 | + * @param null $response |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
|
316 | + { |
|
317 | + $response = $this->post($uri, $payload, $options)->send(); |
|
318 | + return $response->body; |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * @param $method |
|
324 | + * @param $url |
|
325 | + * @param $data |
|
326 | + * @param array $options |
|
327 | + * @return $this |
|
328 | + */ |
|
329 | + protected function ini($method, $url, $data, array $options = array()) |
|
330 | + { |
|
331 | + $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
332 | + $this->addOptions($options); |
|
333 | + |
|
334 | + return $this; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param array $options |
|
339 | + * @return $this |
|
340 | + */ |
|
341 | + public function addOptions(array $options = array()) |
|
342 | + { |
|
343 | + $this->options = $options + $this->options; |
|
344 | + $this->uri = $this->options['url']; |
|
345 | + return $this; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @param $uri |
|
350 | + * @param null $payload |
|
351 | + * @param array $options |
|
352 | + * @return Request |
|
353 | + */ |
|
354 | + function put($uri, $payload = null, array $options = array()) |
|
355 | + { |
|
356 | + return $this->ini(Http::PUT, $uri, $payload, $options); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * @param $uri |
|
361 | + * @param null $payload |
|
362 | + * @param array $options |
|
363 | + * @return Request |
|
364 | + */ |
|
365 | + function patch($uri, $payload = null, array $options = array()) |
|
366 | + { |
|
367 | + return $this->ini(Http::PATCH, $uri, $payload, $options); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * @param $uri |
|
372 | + * @param array $options |
|
373 | + * @return Request |
|
374 | + */ |
|
375 | + public function get($uri, array $options = array()) |
|
376 | + { |
|
377 | + return $this->ini(Http::GET, $uri, array(), $options); |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * @param $uri |
|
383 | + * @param array $options |
|
384 | + * @param null $response |
|
385 | + * @return string |
|
386 | + */ |
|
387 | + public function quickGet($uri, array $options = array(), &$response = null) |
|
388 | + { |
|
389 | + $response = $this->get($uri, $options)->send(); |
|
390 | + return $response->body; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * @param $uri |
|
395 | + * @param array $options |
|
396 | + * @return Request |
|
397 | + */ |
|
398 | + function options($uri, array $options = array()) |
|
399 | + { |
|
400 | + return $this->ini(Http::OPTIONS, $uri, array(), $options); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * @param $uri |
|
405 | + * @param array $options |
|
406 | + * @return Request |
|
407 | + */ |
|
408 | + function head($uri, array $options = array()) |
|
409 | + { |
|
410 | + return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * @param $uri |
|
415 | + * @param array $options |
|
416 | + * @return Request |
|
417 | + */ |
|
418 | + function delete($uri, array $options = array()) |
|
419 | + { |
|
420 | + return $this->ini(Http::DELETE, $uri, array(), $options); |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * @param $uri |
|
425 | + * @param array $options |
|
426 | + * @return Request |
|
427 | + */ |
|
428 | + function trace($uri, array $options = array()) |
|
429 | + { |
|
430 | + return $this->ini(Http::TRACE, $uri, array(), $options); |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * @param bool $isMultiCurl |
|
435 | + * @return Response |
|
436 | + */ |
|
437 | + public function send($isMultiCurl = false) |
|
438 | + { |
|
439 | + try { |
|
440 | + if (!$this->hasInitialized) |
|
441 | + $this->applyOptions(); |
|
442 | + $response = $this->makeResponse($isMultiCurl); |
|
443 | + $response->parse(); |
|
444 | + } catch (\Exception $e) { |
|
445 | + if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
446 | + $response->error = $e->getMessage(); |
|
447 | + $response->errorCode = 999; |
|
448 | + } |
|
449 | + |
|
450 | + if (self::$loggerHandler) { |
|
451 | + call_user_func(self::$loggerHandler, $response); |
|
452 | + } |
|
453 | + if ($this->endCallback) { |
|
454 | + call_user_func($this->endCallback, $response); |
|
455 | + } |
|
456 | + |
|
457 | + return $response; |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * @return $this |
|
462 | + */ |
|
463 | + public function applyOptions() |
|
464 | + { |
|
465 | + $curl = curl_init(); |
|
466 | + $this->curlHandle = $curl; |
|
467 | + $this->prepare(); |
|
468 | + $this->hasInitialized = true; |
|
469 | + return $this; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * @return $this |
|
474 | + */ |
|
475 | + protected function prepare() |
|
476 | + { |
|
477 | + $this->options['url'] = trim($this->options['url']); |
|
478 | + if (empty($this->options['url'])) { |
|
479 | + throw new InvalidArgumentException('url can not empty'); |
|
480 | + } |
|
481 | + |
|
482 | + if (isset($this->options['retry_times'])) { |
|
483 | + $this->retryTimes = abs($this->options['retry_times']); |
|
484 | + } |
|
485 | + |
|
486 | + if (isset($this->options['retry_duration'])) { |
|
487 | + $this->retryDuration = abs($this->options['retry_duration']); |
|
488 | + } |
|
489 | + |
|
490 | + if(isset($this->options['expects_mime'])){ |
|
491 | + $this->expectsMime($this->options['expects_mime']); |
|
492 | + } |
|
493 | + |
|
494 | + if(isset($this->options['send_mime'])){ |
|
495 | + $this->sendMime($this->options['send_mime']); |
|
496 | + } |
|
497 | 497 | |
498 | 498 | // if(!empty($this->options['data']) && !Http::hasBody($this->options['method'])){ |
499 | 499 | // $this->withURIQuery = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |
500 | 500 | // } |
501 | - if (isset($this->withURIQuery)) { |
|
502 | - $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; |
|
503 | - $this->options['url'] .= $this->withURIQuery; |
|
504 | - } |
|
505 | - |
|
506 | - $this->serializeBody(); |
|
507 | - |
|
508 | - //try fix url |
|
509 | - if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
510 | - $components = parse_url($this->options['url']); |
|
511 | - if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
512 | - if($this->withURIQuery){ |
|
513 | - if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
514 | - else $components['query'] = trim($this->withURIQuery); |
|
515 | - } |
|
516 | - $this->options['url'] = self::combineUrl($components); |
|
517 | - |
|
518 | - if (isset($this->options['callback'])) { |
|
519 | - $this->onEnd($this->options['callback']); |
|
520 | - } |
|
521 | - //swap ip and host |
|
522 | - if (!empty($this->options['ip'])) { |
|
523 | - $matches = array(); |
|
524 | - preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
|
525 | - $host = $matches[1]; |
|
526 | - if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
|
527 | - $this->options['headers'] = array('Host: ' . $host); |
|
528 | - } else { |
|
529 | - $this->options['headers'][] = 'Host: ' . $host; |
|
530 | - } |
|
531 | - $this->options['url'] = str_replace("//{$host}", '//' . $this->options['ip'], $this->options['url']); |
|
532 | - unset($host); |
|
533 | - } |
|
534 | - //process version |
|
535 | - if (!empty($this->options['http_version'])) { |
|
536 | - $version = $this->options['http_version']; |
|
537 | - if ($version == '1.0') { |
|
538 | - $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_0; |
|
539 | - } elseif ($version == '1.1') { |
|
540 | - $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1; |
|
541 | - } |
|
542 | - |
|
543 | - unset($version); |
|
544 | - } |
|
545 | - |
|
546 | - //convert secs to milliseconds |
|
547 | - if (defined('CURLOPT_TIMEOUT_MS')) { |
|
548 | - if (!isset($this->options['timeout_ms'])) { |
|
549 | - $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
550 | - } else { |
|
551 | - $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
|
552 | - } |
|
553 | - } |
|
554 | - |
|
555 | - $cURLOptions = self::filterAndRaw($this->options); |
|
556 | - if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
557 | - curl_setopt_array($this->curlHandle, $cURLOptions); |
|
558 | - |
|
559 | - return $this; |
|
560 | - } |
|
561 | - |
|
562 | - public function serializeBody() |
|
563 | - { |
|
564 | - if (isset($this->options['data'])) { |
|
565 | - if (isset($this->sendMime)) { |
|
566 | - $method = $this->sendMime; |
|
567 | - if (!method_exists($this, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
568 | - $this->body = $this->$method($this->options['data']); |
|
569 | - } else { |
|
570 | - $this->body = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |
|
571 | - } |
|
572 | - |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - /** |
|
577 | - * @param callable $callback |
|
578 | - * @return $this |
|
579 | - */ |
|
580 | - public function onEnd(callable $callback) |
|
581 | - { |
|
582 | - if (!is_callable($callback)) { |
|
583 | - throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
584 | - } |
|
585 | - |
|
586 | - $this->endCallback = $callback; |
|
587 | - return $this; |
|
588 | - } |
|
589 | - |
|
590 | - /** |
|
591 | - * @param array $options |
|
592 | - * @return array |
|
593 | - */ |
|
594 | - protected static function filterAndRaw(array &$options) |
|
595 | - { |
|
596 | - $opts = $fullsOpts = array(); |
|
597 | - foreach ($options as $key => $val) { |
|
598 | - $fullOption = self::fullOption($key); |
|
599 | - |
|
600 | - if ($fullOption) { |
|
601 | - $fullsOpts[$fullOption] = $val; |
|
602 | - $opts[constant($fullOption)] = $val; |
|
603 | - } |
|
604 | - unset($options[$key]); |
|
605 | - } |
|
606 | - $options = $fullsOpts; |
|
607 | - return $opts; |
|
608 | - } |
|
609 | - |
|
610 | - /** |
|
611 | - * @param bool $isMultiCurl |
|
612 | - * @return Response |
|
613 | - * @throws \Exception |
|
614 | - */ |
|
615 | - public function makeResponse($isMultiCurl = false) |
|
616 | - { |
|
617 | - $handle = $this->curlHandle; |
|
618 | - $body = $errno = null; |
|
619 | - Helper::retry($this->retryTimes, function()use(&$body, &$errno, $isMultiCurl, $handle){ |
|
620 | - $body = $isMultiCurl ? curl_multi_getcontent($handle) : curl_exec($handle); |
|
621 | - $errno = curl_errno($handle); |
|
622 | - return 0 == $errno; |
|
623 | - }, $this->retryDuration); |
|
624 | - |
|
625 | - $info = curl_getinfo($this->curlHandle); |
|
626 | - $errorCode = curl_errno($this->curlHandle); |
|
627 | - $error = curl_error($this->curlHandle); |
|
628 | - $response = Response::create($this, $body, $info, $errorCode, $error); |
|
629 | - return $response; |
|
630 | - } |
|
501 | + if (isset($this->withURIQuery)) { |
|
502 | + $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; |
|
503 | + $this->options['url'] .= $this->withURIQuery; |
|
504 | + } |
|
505 | + |
|
506 | + $this->serializeBody(); |
|
507 | + |
|
508 | + //try fix url |
|
509 | + if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
510 | + $components = parse_url($this->options['url']); |
|
511 | + if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
512 | + if($this->withURIQuery){ |
|
513 | + if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
514 | + else $components['query'] = trim($this->withURIQuery); |
|
515 | + } |
|
516 | + $this->options['url'] = self::combineUrl($components); |
|
517 | + |
|
518 | + if (isset($this->options['callback'])) { |
|
519 | + $this->onEnd($this->options['callback']); |
|
520 | + } |
|
521 | + //swap ip and host |
|
522 | + if (!empty($this->options['ip'])) { |
|
523 | + $matches = array(); |
|
524 | + preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
|
525 | + $host = $matches[1]; |
|
526 | + if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
|
527 | + $this->options['headers'] = array('Host: ' . $host); |
|
528 | + } else { |
|
529 | + $this->options['headers'][] = 'Host: ' . $host; |
|
530 | + } |
|
531 | + $this->options['url'] = str_replace("//{$host}", '//' . $this->options['ip'], $this->options['url']); |
|
532 | + unset($host); |
|
533 | + } |
|
534 | + //process version |
|
535 | + if (!empty($this->options['http_version'])) { |
|
536 | + $version = $this->options['http_version']; |
|
537 | + if ($version == '1.0') { |
|
538 | + $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_0; |
|
539 | + } elseif ($version == '1.1') { |
|
540 | + $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1; |
|
541 | + } |
|
542 | + |
|
543 | + unset($version); |
|
544 | + } |
|
545 | + |
|
546 | + //convert secs to milliseconds |
|
547 | + if (defined('CURLOPT_TIMEOUT_MS')) { |
|
548 | + if (!isset($this->options['timeout_ms'])) { |
|
549 | + $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
550 | + } else { |
|
551 | + $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
|
552 | + } |
|
553 | + } |
|
554 | + |
|
555 | + $cURLOptions = self::filterAndRaw($this->options); |
|
556 | + if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
557 | + curl_setopt_array($this->curlHandle, $cURLOptions); |
|
558 | + |
|
559 | + return $this; |
|
560 | + } |
|
561 | + |
|
562 | + public function serializeBody() |
|
563 | + { |
|
564 | + if (isset($this->options['data'])) { |
|
565 | + if (isset($this->sendMime)) { |
|
566 | + $method = $this->sendMime; |
|
567 | + if (!method_exists($this, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
568 | + $this->body = $this->$method($this->options['data']); |
|
569 | + } else { |
|
570 | + $this->body = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |
|
571 | + } |
|
572 | + |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + /** |
|
577 | + * @param callable $callback |
|
578 | + * @return $this |
|
579 | + */ |
|
580 | + public function onEnd(callable $callback) |
|
581 | + { |
|
582 | + if (!is_callable($callback)) { |
|
583 | + throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
584 | + } |
|
585 | + |
|
586 | + $this->endCallback = $callback; |
|
587 | + return $this; |
|
588 | + } |
|
589 | + |
|
590 | + /** |
|
591 | + * @param array $options |
|
592 | + * @return array |
|
593 | + */ |
|
594 | + protected static function filterAndRaw(array &$options) |
|
595 | + { |
|
596 | + $opts = $fullsOpts = array(); |
|
597 | + foreach ($options as $key => $val) { |
|
598 | + $fullOption = self::fullOption($key); |
|
599 | + |
|
600 | + if ($fullOption) { |
|
601 | + $fullsOpts[$fullOption] = $val; |
|
602 | + $opts[constant($fullOption)] = $val; |
|
603 | + } |
|
604 | + unset($options[$key]); |
|
605 | + } |
|
606 | + $options = $fullsOpts; |
|
607 | + return $opts; |
|
608 | + } |
|
609 | + |
|
610 | + /** |
|
611 | + * @param bool $isMultiCurl |
|
612 | + * @return Response |
|
613 | + * @throws \Exception |
|
614 | + */ |
|
615 | + public function makeResponse($isMultiCurl = false) |
|
616 | + { |
|
617 | + $handle = $this->curlHandle; |
|
618 | + $body = $errno = null; |
|
619 | + Helper::retry($this->retryTimes, function()use(&$body, &$errno, $isMultiCurl, $handle){ |
|
620 | + $body = $isMultiCurl ? curl_multi_getcontent($handle) : curl_exec($handle); |
|
621 | + $errno = curl_errno($handle); |
|
622 | + return 0 == $errno; |
|
623 | + }, $this->retryDuration); |
|
624 | + |
|
625 | + $info = curl_getinfo($this->curlHandle); |
|
626 | + $errorCode = curl_errno($this->curlHandle); |
|
627 | + $error = curl_error($this->curlHandle); |
|
628 | + $response = Response::create($this, $body, $info, $errorCode, $error); |
|
629 | + return $response; |
|
630 | + } |
|
631 | 631 | |
632 | 632 | |
633 | 633 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | protected static $curlAlias = array( |
29 | 29 | 'url' => 'CURLOPT_URL', |
30 | 30 | 'uri' => 'CURLOPT_URL', |
31 | - 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
31 | + 'debug' => 'CURLOPT_VERBOSE', //for debug verbose |
|
32 | 32 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
33 | 33 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
34 | 34 | 'ua' => 'CURLOPT_USERAGENT', |
@@ -46,8 +46,8 @@ discard block |
||
46 | 46 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
47 | 47 | 'expects_mime' => null, //expected mime |
48 | 48 | 'send_mime' => null, //send mime |
49 | - 'ip' => null,//specify ip to send request |
|
50 | - 'callback' => null,//callback on end |
|
49 | + 'ip' => null, //specify ip to send request |
|
50 | + 'callback' => null, //callback on end |
|
51 | 51 | |
52 | 52 | ); |
53 | 53 | protected static $loggerHandler; |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | */ |
78 | 78 | $options = array( |
79 | 79 | 'CURLOPT_MAXREDIRS' => 10, |
80 | - 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
|
81 | - 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
|
82 | - 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
|
80 | + 'CURLOPT_SSL_VERIFYPEER' => false, //for https |
|
81 | + 'CURLOPT_SSL_VERIFYHOST' => 0, //for https |
|
82 | + 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4, //ipv4 first |
|
83 | 83 | // 'CURLOPT_SAFE_UPLOAD' => false,// compatible with PHP 5.6.0 |
84 | 84 | 'header' => true, |
85 | 85 | 'method' => self::GET, |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | 'follow_location' => true, |
89 | 89 | 'timeout' => 0, |
90 | 90 | // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
91 | - 'retry_times' => 1,//redo task when failed |
|
92 | - 'retry_duration' => 0,//in seconds |
|
91 | + 'retry_times' => 1, //redo task when failed |
|
92 | + 'retry_duration' => 0, //in seconds |
|
93 | 93 | ); |
94 | 94 | |
95 | 95 | |
@@ -153,15 +153,15 @@ discard block |
||
153 | 153 | */ |
154 | 154 | private static function combineUrl($parsedComponents) |
155 | 155 | { |
156 | - $scheme = isset($parsedComponents['scheme']) ? $parsedComponents['scheme'] . '://' : ''; |
|
156 | + $scheme = isset($parsedComponents['scheme']) ? $parsedComponents['scheme'].'://' : ''; |
|
157 | 157 | $host = isset($parsedComponents['host']) ? $parsedComponents['host'] : ''; |
158 | - $port = isset($parsedComponents['port']) ? ':' . $parsedComponents['port'] : ''; |
|
158 | + $port = isset($parsedComponents['port']) ? ':'.$parsedComponents['port'] : ''; |
|
159 | 159 | $user = isset($parsedComponents['user']) ? $parsedComponents['user'] : ''; |
160 | - $pass = isset($parsedComponents['pass']) ? ':' . $parsedComponents['pass'] : ''; |
|
160 | + $pass = isset($parsedComponents['pass']) ? ':'.$parsedComponents['pass'] : ''; |
|
161 | 161 | $pass = ($user || $pass) ? "$pass@" : ''; |
162 | 162 | $path = isset($parsedComponents['path']) ? $parsedComponents['path'] : ''; |
163 | - $query = isset($parsedComponents['query']) ? '?' . $parsedComponents['query'] : ''; |
|
164 | - $fragment = isset($parsedComponents['fragment']) ? '#' . $parsedComponents['fragment'] : ''; |
|
163 | + $query = isset($parsedComponents['query']) ? '?'.$parsedComponents['query'] : ''; |
|
164 | + $fragment = isset($parsedComponents['fragment']) ? '#'.$parsedComponents['fragment'] : ''; |
|
165 | 165 | return "$scheme$user$pass$host$port$path$query$fragment"; |
166 | 166 | } |
167 | 167 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | public function addHeader($headerName, $value) |
195 | 195 | { |
196 | - $this->options['headers'][] = $headerName . ': ' . $value; |
|
196 | + $this->options['headers'][] = $headerName.': '.$value; |
|
197 | 197 | return $this; |
198 | 198 | } |
199 | 199 | |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | */ |
258 | 258 | public function getIni($field = null) |
259 | 259 | { |
260 | - if(!$field) return $this->options; |
|
260 | + if (!$field) return $this->options; |
|
261 | 261 | $full = self::fullOption($field); |
262 | 262 | return isset($this->options[$full]) ? $this->options[$full] : false; |
263 | 263 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | */ |
329 | 329 | protected function ini($method, $url, $data, array $options = array()) |
330 | 330 | { |
331 | - $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
331 | + $options = array('url' => $url, 'method' => $method, 'data' => $data)+$options; |
|
332 | 332 | $this->addOptions($options); |
333 | 333 | |
334 | 334 | return $this; |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | */ |
341 | 341 | public function addOptions(array $options = array()) |
342 | 342 | { |
343 | - $this->options = $options + $this->options; |
|
343 | + $this->options = $options+$this->options; |
|
344 | 344 | $this->uri = $this->options['url']; |
345 | 345 | return $this; |
346 | 346 | } |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | $response = $this->makeResponse($isMultiCurl); |
443 | 443 | $response->parse(); |
444 | 444 | } catch (\Exception $e) { |
445 | - if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
445 | + if (!isset($response)) $response = Response::create($this, null, null, null, null); |
|
446 | 446 | $response->error = $e->getMessage(); |
447 | 447 | $response->errorCode = 999; |
448 | 448 | } |
@@ -487,11 +487,11 @@ discard block |
||
487 | 487 | $this->retryDuration = abs($this->options['retry_duration']); |
488 | 488 | } |
489 | 489 | |
490 | - if(isset($this->options['expects_mime'])){ |
|
490 | + if (isset($this->options['expects_mime'])) { |
|
491 | 491 | $this->expectsMime($this->options['expects_mime']); |
492 | 492 | } |
493 | 493 | |
494 | - if(isset($this->options['send_mime'])){ |
|
494 | + if (isset($this->options['send_mime'])) { |
|
495 | 495 | $this->sendMime($this->options['send_mime']); |
496 | 496 | } |
497 | 497 | |
@@ -506,11 +506,11 @@ discard block |
||
506 | 506 | $this->serializeBody(); |
507 | 507 | |
508 | 508 | //try fix url |
509 | - if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
509 | + if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://'.$this->options['url']; |
|
510 | 510 | $components = parse_url($this->options['url']); |
511 | - if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
512 | - if($this->withURIQuery){ |
|
513 | - if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
511 | + if (FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '.$this->options['url']); |
|
512 | + if ($this->withURIQuery) { |
|
513 | + if (isset($components['query'])) $components['query'] .= '&'.trim($this->withURIQuery); |
|
514 | 514 | else $components['query'] = trim($this->withURIQuery); |
515 | 515 | } |
516 | 516 | $this->options['url'] = self::combineUrl($components); |
@@ -524,11 +524,11 @@ discard block |
||
524 | 524 | preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
525 | 525 | $host = $matches[1]; |
526 | 526 | if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
527 | - $this->options['headers'] = array('Host: ' . $host); |
|
527 | + $this->options['headers'] = array('Host: '.$host); |
|
528 | 528 | } else { |
529 | - $this->options['headers'][] = 'Host: ' . $host; |
|
529 | + $this->options['headers'][] = 'Host: '.$host; |
|
530 | 530 | } |
531 | - $this->options['url'] = str_replace("//{$host}", '//' . $this->options['ip'], $this->options['url']); |
|
531 | + $this->options['url'] = str_replace("//{$host}", '//'.$this->options['ip'], $this->options['url']); |
|
532 | 532 | unset($host); |
533 | 533 | } |
534 | 534 | //process version |
@@ -546,14 +546,14 @@ discard block |
||
546 | 546 | //convert secs to milliseconds |
547 | 547 | if (defined('CURLOPT_TIMEOUT_MS')) { |
548 | 548 | if (!isset($this->options['timeout_ms'])) { |
549 | - $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
549 | + $this->options['timeout_ms'] = intval($this->options['timeout']*1000); |
|
550 | 550 | } else { |
551 | 551 | $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
552 | 552 | } |
553 | 553 | } |
554 | 554 | |
555 | 555 | $cURLOptions = self::filterAndRaw($this->options); |
556 | - if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
556 | + if (isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body; //use serialized body not raw data |
|
557 | 557 | curl_setopt_array($this->curlHandle, $cURLOptions); |
558 | 558 | |
559 | 559 | return $this; |
@@ -564,10 +564,10 @@ discard block |
||
564 | 564 | if (isset($this->options['data'])) { |
565 | 565 | if (isset($this->sendMime)) { |
566 | 566 | $method = $this->sendMime; |
567 | - if (!method_exists($this, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
567 | + if (!method_exists($this, $method)) throw new InvalidOperationException($method.' is not exists in '.__CLASS__); |
|
568 | 568 | $this->body = $this->$method($this->options['data']); |
569 | 569 | } else { |
570 | - $this->body = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |
|
570 | + $this->body = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |
|
571 | 571 | } |
572 | 572 | |
573 | 573 | } |
@@ -580,7 +580,7 @@ discard block |
||
580 | 580 | public function onEnd(callable $callback) |
581 | 581 | { |
582 | 582 | if (!is_callable($callback)) { |
583 | - throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
583 | + throw new InvalidArgumentException('callback not is callable :'.print_r($callback, 1)); |
|
584 | 584 | } |
585 | 585 | |
586 | 586 | $this->endCallback = $callback; |
@@ -230,7 +230,9 @@ discard block |
||
230 | 230 | } |
231 | 231 | public function expects($mime) |
232 | 232 | { |
233 | - if (empty($mime)) return $this; |
|
233 | + if (empty($mime)) { |
|
234 | + return $this; |
|
235 | + } |
|
234 | 236 | $this->expected_type = Mime::getFullMime($mime); |
235 | 237 | return $this; |
236 | 238 | } |
@@ -257,7 +259,9 @@ discard block |
||
257 | 259 | */ |
258 | 260 | public function getIni($field = null) |
259 | 261 | { |
260 | - if(!$field) return $this->options; |
|
262 | + if(!$field) { |
|
263 | + return $this->options; |
|
264 | + } |
|
261 | 265 | $full = self::fullOption($field); |
262 | 266 | return isset($this->options[$full]) ? $this->options[$full] : false; |
263 | 267 | } |
@@ -437,12 +441,15 @@ discard block |
||
437 | 441 | public function send($isMultiCurl = false) |
438 | 442 | { |
439 | 443 | try { |
440 | - if (!$this->hasInitialized) |
|
441 | - $this->applyOptions(); |
|
444 | + if (!$this->hasInitialized) { |
|
445 | + $this->applyOptions(); |
|
446 | + } |
|
442 | 447 | $response = $this->makeResponse($isMultiCurl); |
443 | 448 | $response->parse(); |
444 | 449 | } catch (\Exception $e) { |
445 | - if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
450 | + if(!isset($response)) { |
|
451 | + $response = Response::create($this, null, null, null, null); |
|
452 | + } |
|
446 | 453 | $response->error = $e->getMessage(); |
447 | 454 | $response->errorCode = 999; |
448 | 455 | } |
@@ -506,12 +513,19 @@ discard block |
||
506 | 513 | $this->serializeBody(); |
507 | 514 | |
508 | 515 | //try fix url |
509 | - if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
516 | + if (strpos($this->options['url'], '://') === FALSE) { |
|
517 | + $this->options['url'] = 'http://' . $this->options['url']; |
|
518 | + } |
|
510 | 519 | $components = parse_url($this->options['url']); |
511 | - if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
520 | + if(FALSE === $components) { |
|
521 | + throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
522 | + } |
|
512 | 523 | if($this->withURIQuery){ |
513 | - if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
514 | - else $components['query'] = trim($this->withURIQuery); |
|
524 | + if(isset($components['query'])) { |
|
525 | + $components['query'] .= '&'. trim($this->withURIQuery); |
|
526 | + } else { |
|
527 | + $components['query'] = trim($this->withURIQuery); |
|
528 | + } |
|
515 | 529 | } |
516 | 530 | $this->options['url'] = self::combineUrl($components); |
517 | 531 | |
@@ -553,7 +567,10 @@ discard block |
||
553 | 567 | } |
554 | 568 | |
555 | 569 | $cURLOptions = self::filterAndRaw($this->options); |
556 | - if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
570 | + if(isset($this->body)) { |
|
571 | + $cURLOptions[CURLOPT_POSTFIELDS] = $this->body; |
|
572 | + } |
|
573 | + //use serialized body not raw data |
|
557 | 574 | curl_setopt_array($this->curlHandle, $cURLOptions); |
558 | 575 | |
559 | 576 | return $this; |
@@ -564,7 +581,9 @@ discard block |
||
564 | 581 | if (isset($this->options['data'])) { |
565 | 582 | if (isset($this->sendMime)) { |
566 | 583 | $method = $this->sendMime; |
567 | - if (!method_exists($this, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
584 | + if (!method_exists($this, $method)) { |
|
585 | + throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
586 | + } |
|
568 | 587 | $this->body = $this->$method($this->options['data']); |
569 | 588 | } else { |
570 | 589 | $this->body = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; |