@@ -15,453 +15,453 @@ |
||
15 | 15 | |
16 | 16 | class Request extends Http |
17 | 17 | { |
18 | - const MAX_REDIRECTS_DEFAULT = 10; |
|
19 | - protected static $curlAlias = array( |
|
20 | - 'url' => 'CURLOPT_URL', |
|
21 | - 'uri' => 'CURLOPT_URL', |
|
22 | - 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
23 | - 'method' => 'CURLOPT_CUSTOMREQUEST', |
|
24 | - 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
|
25 | - 'ua' => 'CURLOPT_USERAGENT', |
|
26 | - 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
|
27 | - 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
|
28 | - 'referer' => 'CURLOPT_REFERER', |
|
29 | - 'binary' => 'CURLOPT_BINARYTRANSFER', |
|
30 | - 'port' => 'CURLOPT_PORT', |
|
31 | - 'header' => 'CURLOPT_HEADER', // TRUE:include header |
|
32 | - 'headers' => 'CURLOPT_HTTPHEADER', // array |
|
33 | - 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
|
34 | - 'upload' => 'CURLOPT_INFILE', // reading file stream |
|
35 | - 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
|
36 | - 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
|
37 | - 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
|
38 | - ); |
|
39 | - protected static $logger; |
|
40 | - public $curlHandle; |
|
41 | - public |
|
42 | - $uri, |
|
43 | - $timeout, |
|
44 | - $maxRedirects, |
|
45 | - $followRedirects; |
|
46 | - public $cert; |
|
47 | - public $key; |
|
48 | - public $passphrase; |
|
49 | - public $encoding; |
|
50 | - public $payload; |
|
51 | - public $retryTimes; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var int seconds |
|
55 | - */ |
|
56 | - public $retryDuration; |
|
57 | - protected $options = array( |
|
58 | - 'CURLOPT_MAXREDIRS' => 10, |
|
59 | - 'header' => true, |
|
60 | - 'method' => self::GET, |
|
61 | - 'transfer' => true, |
|
62 | - 'follow_location' => true, |
|
63 | - 'timeout' => 0, |
|
18 | + const MAX_REDIRECTS_DEFAULT = 10; |
|
19 | + protected static $curlAlias = array( |
|
20 | + 'url' => 'CURLOPT_URL', |
|
21 | + 'uri' => 'CURLOPT_URL', |
|
22 | + 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
23 | + 'method' => 'CURLOPT_CUSTOMREQUEST', |
|
24 | + 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
|
25 | + 'ua' => 'CURLOPT_USERAGENT', |
|
26 | + 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
|
27 | + 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
|
28 | + 'referer' => 'CURLOPT_REFERER', |
|
29 | + 'binary' => 'CURLOPT_BINARYTRANSFER', |
|
30 | + 'port' => 'CURLOPT_PORT', |
|
31 | + 'header' => 'CURLOPT_HEADER', // TRUE:include header |
|
32 | + 'headers' => 'CURLOPT_HTTPHEADER', // array |
|
33 | + 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
|
34 | + 'upload' => 'CURLOPT_INFILE', // reading file stream |
|
35 | + 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
|
36 | + 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
|
37 | + 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
|
38 | + ); |
|
39 | + protected static $logger; |
|
40 | + public $curlHandle; |
|
41 | + public |
|
42 | + $uri, |
|
43 | + $timeout, |
|
44 | + $maxRedirects, |
|
45 | + $followRedirects; |
|
46 | + public $cert; |
|
47 | + public $key; |
|
48 | + public $passphrase; |
|
49 | + public $encoding; |
|
50 | + public $payload; |
|
51 | + public $retryTimes; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var int seconds |
|
55 | + */ |
|
56 | + public $retryDuration; |
|
57 | + protected $options = array( |
|
58 | + 'CURLOPT_MAXREDIRS' => 10, |
|
59 | + 'header' => true, |
|
60 | + 'method' => self::GET, |
|
61 | + 'transfer' => true, |
|
62 | + 'follow_location' => true, |
|
63 | + 'timeout' => 0, |
|
64 | 64 | // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
65 | - 'retry_times' => 1,//redo task when failed |
|
66 | - 'retry_duration' => 0,//in seconds |
|
67 | - ); |
|
68 | - protected $endCallback; |
|
69 | - protected $withURIQuery; |
|
70 | - protected $hasInitialized = false; |
|
71 | - |
|
72 | - protected function __construct() |
|
73 | - { |
|
74 | - |
|
75 | - } |
|
76 | - |
|
77 | - public static function create() |
|
78 | - { |
|
79 | - return new self; |
|
80 | - } |
|
81 | - |
|
82 | - public static function setLogger($logger) |
|
83 | - { |
|
84 | - self::$logger = $logger; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Specify timeout |
|
89 | - * @param float|int $timeout seconds to timeout the HTTP call |
|
90 | - * @return Request |
|
91 | - */ |
|
92 | - public function timeout($timeout) |
|
93 | - { |
|
94 | - $this->timeout = $timeout; |
|
95 | - return $this; |
|
96 | - } |
|
97 | - |
|
98 | - public function noFollow() |
|
99 | - { |
|
100 | - return $this->follow(0); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * If the response is a 301 or 302 redirect, automatically |
|
105 | - * send off another request to that location |
|
106 | - * @param int $follow follow or not to follow or maximal number of redirects |
|
107 | - * @return Request |
|
108 | - */ |
|
109 | - public function follow($follow) |
|
110 | - { |
|
111 | - $this->maxRedirects = abs($follow); |
|
112 | - $this->followRedirects = $follow > 0; |
|
113 | - return $this; |
|
114 | - } |
|
115 | - |
|
116 | - public function endCallback() |
|
117 | - { |
|
118 | - return $this->endCallback; |
|
119 | - } |
|
120 | - |
|
121 | - public function hasEndCallback() |
|
122 | - { |
|
123 | - return isset($this->endCallback); |
|
124 | - } |
|
125 | - |
|
126 | - public function uri($uri) |
|
127 | - { |
|
128 | - $this->uri = $uri; |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - public function hasCert() |
|
133 | - { |
|
134 | - return isset($this->cert) && isset($this->key); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Use Client Side Cert Authentication |
|
139 | - * @param string $key file path to client key |
|
140 | - * @param string $cert file path to client cert |
|
141 | - * @param string $passphrase for client key |
|
142 | - * @param string $encoding default PEM |
|
143 | - * @return Request |
|
144 | - */ |
|
145 | - public function cert($cert, $key, $passphrase = null, $encoding = 'PEM') |
|
146 | - { |
|
147 | - $this->cert = $cert; |
|
148 | - $this->key = $key; |
|
149 | - $this->passphrase = $passphrase; |
|
150 | - $this->encoding = $encoding; |
|
151 | - return $this; |
|
152 | - } |
|
153 | - |
|
154 | - public function body($payload, $mimeType = null) |
|
155 | - { |
|
156 | - $this->mime($mimeType); |
|
157 | - $this->payload = $payload; |
|
158 | - // Iserntentially don't call _serializePayload yet. Wait until |
|
159 | - // we actually send off the request to convert payload to string. |
|
160 | - // At that time, the `serialized_payload` is set accordingly. |
|
161 | - return $this; |
|
162 | - } |
|
163 | - public function mime($mime) |
|
164 | - { |
|
165 | - if (empty($mime)) return $this; |
|
166 | - $this->content_type = $this->expected_type = Mime::getFullMime($mime); |
|
167 | - if ($this->isUpload()) { |
|
168 | - $this->neverSerializePayload(); |
|
169 | - } |
|
170 | - return $this; |
|
171 | - } |
|
172 | - public function addHeader($header_name, $value) |
|
173 | - { |
|
174 | - $this->headers[$header_name] = $value; |
|
175 | - return $this; |
|
176 | - } |
|
177 | - |
|
178 | - public function addHeaders(array $headers) |
|
179 | - { |
|
180 | - foreach ($headers as $header => $value) { |
|
181 | - $this->addHeader($header, $value); |
|
182 | - } |
|
183 | - return $this; |
|
184 | - } |
|
185 | - public function expectsType($mime) |
|
186 | - { |
|
187 | - return $this->expects($mime); |
|
188 | - } |
|
189 | - public function sendType($mime) |
|
190 | - { |
|
191 | - return $this->contentType = $mime; |
|
192 | - } |
|
193 | - public function expects($mime) |
|
194 | - { |
|
195 | - if (empty($mime)) return $this; |
|
196 | - $this->expected_type = Mime::getFullMime($mime); |
|
197 | - return $this; |
|
198 | - } |
|
199 | - /** |
|
200 | - * @param $field alias or field name |
|
201 | - * @return bool|mixed |
|
202 | - */ |
|
203 | - public function getIni($field) |
|
204 | - { |
|
205 | - $alias = self::optionAlias($field); |
|
206 | - return isset($this->options[$alias]) ? $this->options[$alias] : false; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @param $key |
|
211 | - * @return mixed |
|
212 | - */ |
|
213 | - protected static function optionAlias($key) |
|
214 | - { |
|
215 | - $alias = false; |
|
216 | - if (isset(self::$curlAlias[$key])) { |
|
217 | - $alias = self::$curlAlias[$key]; |
|
218 | - } elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) { |
|
219 | - $alias = $key; |
|
220 | - } |
|
221 | - return $alias; |
|
222 | - } |
|
223 | - |
|
224 | - public function addQuery($data) |
|
225 | - { |
|
226 | - if (!empty($data)) { |
|
227 | - if (is_array($data)) { |
|
228 | - $this->withURIQuery = http_build_query($data); |
|
229 | - } else if (is_string($data)) { |
|
230 | - $this->withURIQuery = $data; |
|
231 | - } else { |
|
232 | - throw new InvalidArgumentException('data must be array or string'); |
|
233 | - } |
|
234 | - } |
|
235 | - return $this; |
|
236 | - } |
|
237 | - |
|
238 | - public function post($uri, $payload = null, array $options = array()) |
|
239 | - { |
|
240 | - return $this->ini(Http::POST, $uri, $payload, $options); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param $uri |
|
245 | - * @param null $payload |
|
246 | - * @param array $options |
|
247 | - * @param null $response |
|
248 | - * @return string |
|
249 | - */ |
|
250 | - public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
|
251 | - { |
|
252 | - $response = $this->post($uri, $payload, $options)->send(); |
|
253 | - return $response->body; |
|
254 | - } |
|
255 | - /* no body */ |
|
256 | - |
|
257 | - protected function ini($method, $url, $data , array $options = array()) |
|
258 | - { |
|
259 | - $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
260 | - $this->addOptions($options); |
|
261 | - |
|
262 | - return $this; |
|
263 | - } |
|
264 | - |
|
265 | - public function addOptions(array $options = array()) |
|
266 | - { |
|
267 | - $this->options = $options + $this->options; |
|
268 | - $this->uri = $this->options['url']; |
|
269 | - return $this; |
|
270 | - } |
|
271 | - |
|
272 | - function put($uri, $payload = null, array $options = array()) |
|
273 | - { |
|
274 | - return $this->ini(Http::PUT, $uri, $payload, $options); |
|
275 | - } |
|
276 | - |
|
277 | - function patch($uri, $payload = null, array $options = array()) |
|
278 | - { |
|
279 | - return $this->ini(Http::PATCH, $uri, $payload, $options); |
|
280 | - } |
|
281 | - |
|
282 | - public function get($uri, array $options = array()) |
|
283 | - { |
|
284 | - return $this->ini(Http::GET, $uri, array(), $options); |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * @param $uri |
|
289 | - * @param array $options |
|
290 | - * @param null $response |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - public function quickGet($uri, array $options = array(), &$response = null) |
|
294 | - { |
|
295 | - $response = $this->get($uri, $options)->send(); |
|
296 | - return $response->body; |
|
297 | - } |
|
298 | - |
|
299 | - function options($uri, array $options = array()) |
|
300 | - { |
|
301 | - return $this->ini(Http::OPTIONS, $uri, array(), $options); |
|
302 | - } |
|
303 | - |
|
304 | - function head($uri, array $options = array()) |
|
305 | - { |
|
306 | - return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options); |
|
307 | - } |
|
308 | - |
|
309 | - function delete($uri, array $options = array()) |
|
310 | - { |
|
311 | - return $this->ini(Http::DELETE, $uri, array(), $options); |
|
312 | - } |
|
313 | - |
|
314 | - function trace($uri, array $options = array()) |
|
315 | - { |
|
316 | - return $this->ini(Http::TRACE, $uri, array(), $options); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @return Response |
|
321 | - */ |
|
322 | - public function send() |
|
323 | - { |
|
324 | - if (!$this->hasInitialized) |
|
325 | - $this->applyOptions(); |
|
326 | - $response = $this->makeResponse(); |
|
327 | - if ($this->endCallback) { |
|
328 | - $func = $this->endCallback; |
|
329 | - $func($response); |
|
330 | - } |
|
331 | - return $response; |
|
332 | - } |
|
333 | - |
|
334 | - public function applyOptions() |
|
335 | - { |
|
336 | - $curl = curl_init(); |
|
337 | - $this->curlHandle = $curl; |
|
338 | - $this->prepare(); |
|
339 | - $this->hasInitialized = true; |
|
340 | - return $this; |
|
341 | - } |
|
342 | - |
|
343 | - protected function prepare() |
|
344 | - { |
|
345 | - if (empty($this->options['url'])) { |
|
346 | - throw new InvalidArgumentException('url can not empty'); |
|
347 | - } |
|
348 | - |
|
349 | - if (isset($this->options['retry_times'])) { |
|
350 | - $this->retryTimes = abs($this->options['retry_times']); |
|
351 | - } |
|
352 | - |
|
353 | - if (isset($this->options['retry_duration'])) { |
|
354 | - $this->retryDuration = abs($this->options['retry_duration']); |
|
355 | - } |
|
356 | - |
|
357 | - if (isset($this->options['data'])) { |
|
358 | - $this->options['data'] = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data'];//for better compatibility |
|
359 | - } |
|
360 | - if (isset($this->withURIQuery)) { |
|
361 | - $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; |
|
362 | - $this->options['url'] .= $this->withURIQuery; |
|
363 | - } |
|
364 | - if (isset($this->options['callback'])) { |
|
365 | - $this->onEnd($this->options['callback']); |
|
366 | - unset($this->options['callback']); |
|
367 | - } |
|
368 | - //swap ip and host |
|
369 | - if (!empty($this->options['ip'])) { |
|
370 | - $matches = array(); |
|
371 | - preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
|
372 | - $host = $matches[1]; |
|
373 | - if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
|
374 | - $this->options['headers'] = array('Host: ' . $host); |
|
375 | - } else { |
|
376 | - $this->options['headers'][] = 'Host: ' . $host; |
|
377 | - } |
|
378 | - $this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//' . $this->options['ip'], $this->options['url']); |
|
379 | - unset($this->options['ip']); |
|
380 | - unset($host); |
|
381 | - } |
|
382 | - //process version |
|
383 | - if (!empty($this->options['http_version'])) { |
|
384 | - $version = $this->options['http_version']; |
|
385 | - if ($version == '1.0') { |
|
386 | - $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_0; |
|
387 | - } elseif ($version == '1.1') { |
|
388 | - $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1; |
|
389 | - } |
|
390 | - |
|
391 | - unset($version); |
|
392 | - } |
|
393 | - |
|
394 | - //convert secs to milliseconds |
|
395 | - if (defined('CURLOPT_TIMEOUT_MS')) { |
|
396 | - if (!isset($this->options['timeout_ms'])) { |
|
397 | - $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
398 | - } else { |
|
399 | - $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - $cURLOptions = self::filterAndRaw($this->options); |
|
404 | - |
|
405 | - curl_setopt_array($this->curlHandle, $cURLOptions); |
|
406 | - |
|
407 | - return $this; |
|
408 | - } |
|
409 | - |
|
410 | - public function onEnd(callable $callback) |
|
411 | - { |
|
412 | - if (!is_callable($callback)) { |
|
413 | - throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
414 | - } |
|
415 | - |
|
416 | - $this->endCallback = $callback; |
|
417 | - return $this; |
|
418 | - } |
|
419 | - |
|
420 | - protected static function filterAndRaw(array &$options) |
|
421 | - { |
|
422 | - $opts = array(); |
|
423 | - foreach ($options as $key => $val) { |
|
424 | - $alias = self::optionAlias($key); |
|
425 | - $options[$alias] = $val; |
|
426 | - if ($alias) { |
|
427 | - $opts[constant($alias)] = $val; |
|
428 | - } |
|
429 | - unset($options[$key]); |
|
430 | - } |
|
431 | - return $opts; |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - public function makeResponse($isMultiCurl = false) |
|
436 | - { |
|
437 | - $handle = $this->curlHandle; |
|
438 | - $body = $errno = null; |
|
439 | - Helper::retry($this->retryTimes, function()use(&$body, &$errno, $isMultiCurl, $handle){ |
|
440 | - $body = $isMultiCurl ? curl_multi_getcontent($handle) : curl_exec($handle); |
|
441 | - $errno = curl_errno($handle); |
|
442 | - var_dump(curl_error($handle), time()); |
|
443 | - ob_flush(); |
|
444 | - flush(); |
|
445 | - return 0 == $errno; |
|
446 | - }, $this->retryDuration); |
|
447 | - |
|
448 | - $info = curl_getinfo($this->curlHandle); |
|
449 | - $error = curl_error($this->curlHandle); |
|
450 | - $response = Response::create($this, $body, $info, $errno, $error); |
|
451 | - if (!is_null(self::$logger)) { |
|
452 | - self::log($response); |
|
453 | - } |
|
454 | - |
|
455 | - return $response; |
|
456 | - } |
|
457 | - |
|
458 | - private static function log(Response $response) |
|
459 | - { |
|
460 | - if ($response->hasErrors()) { |
|
461 | - self::$logger->error($response->request->getURI() . "\t" . $response->error, array( |
|
462 | - 'response' => print_r($response, 1), |
|
463 | - )); |
|
464 | - } |
|
465 | - |
|
466 | - } |
|
65 | + 'retry_times' => 1,//redo task when failed |
|
66 | + 'retry_duration' => 0,//in seconds |
|
67 | + ); |
|
68 | + protected $endCallback; |
|
69 | + protected $withURIQuery; |
|
70 | + protected $hasInitialized = false; |
|
71 | + |
|
72 | + protected function __construct() |
|
73 | + { |
|
74 | + |
|
75 | + } |
|
76 | + |
|
77 | + public static function create() |
|
78 | + { |
|
79 | + return new self; |
|
80 | + } |
|
81 | + |
|
82 | + public static function setLogger($logger) |
|
83 | + { |
|
84 | + self::$logger = $logger; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Specify timeout |
|
89 | + * @param float|int $timeout seconds to timeout the HTTP call |
|
90 | + * @return Request |
|
91 | + */ |
|
92 | + public function timeout($timeout) |
|
93 | + { |
|
94 | + $this->timeout = $timeout; |
|
95 | + return $this; |
|
96 | + } |
|
97 | + |
|
98 | + public function noFollow() |
|
99 | + { |
|
100 | + return $this->follow(0); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * If the response is a 301 or 302 redirect, automatically |
|
105 | + * send off another request to that location |
|
106 | + * @param int $follow follow or not to follow or maximal number of redirects |
|
107 | + * @return Request |
|
108 | + */ |
|
109 | + public function follow($follow) |
|
110 | + { |
|
111 | + $this->maxRedirects = abs($follow); |
|
112 | + $this->followRedirects = $follow > 0; |
|
113 | + return $this; |
|
114 | + } |
|
115 | + |
|
116 | + public function endCallback() |
|
117 | + { |
|
118 | + return $this->endCallback; |
|
119 | + } |
|
120 | + |
|
121 | + public function hasEndCallback() |
|
122 | + { |
|
123 | + return isset($this->endCallback); |
|
124 | + } |
|
125 | + |
|
126 | + public function uri($uri) |
|
127 | + { |
|
128 | + $this->uri = $uri; |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + public function hasCert() |
|
133 | + { |
|
134 | + return isset($this->cert) && isset($this->key); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Use Client Side Cert Authentication |
|
139 | + * @param string $key file path to client key |
|
140 | + * @param string $cert file path to client cert |
|
141 | + * @param string $passphrase for client key |
|
142 | + * @param string $encoding default PEM |
|
143 | + * @return Request |
|
144 | + */ |
|
145 | + public function cert($cert, $key, $passphrase = null, $encoding = 'PEM') |
|
146 | + { |
|
147 | + $this->cert = $cert; |
|
148 | + $this->key = $key; |
|
149 | + $this->passphrase = $passphrase; |
|
150 | + $this->encoding = $encoding; |
|
151 | + return $this; |
|
152 | + } |
|
153 | + |
|
154 | + public function body($payload, $mimeType = null) |
|
155 | + { |
|
156 | + $this->mime($mimeType); |
|
157 | + $this->payload = $payload; |
|
158 | + // Iserntentially don't call _serializePayload yet. Wait until |
|
159 | + // we actually send off the request to convert payload to string. |
|
160 | + // At that time, the `serialized_payload` is set accordingly. |
|
161 | + return $this; |
|
162 | + } |
|
163 | + public function mime($mime) |
|
164 | + { |
|
165 | + if (empty($mime)) return $this; |
|
166 | + $this->content_type = $this->expected_type = Mime::getFullMime($mime); |
|
167 | + if ($this->isUpload()) { |
|
168 | + $this->neverSerializePayload(); |
|
169 | + } |
|
170 | + return $this; |
|
171 | + } |
|
172 | + public function addHeader($header_name, $value) |
|
173 | + { |
|
174 | + $this->headers[$header_name] = $value; |
|
175 | + return $this; |
|
176 | + } |
|
177 | + |
|
178 | + public function addHeaders(array $headers) |
|
179 | + { |
|
180 | + foreach ($headers as $header => $value) { |
|
181 | + $this->addHeader($header, $value); |
|
182 | + } |
|
183 | + return $this; |
|
184 | + } |
|
185 | + public function expectsType($mime) |
|
186 | + { |
|
187 | + return $this->expects($mime); |
|
188 | + } |
|
189 | + public function sendType($mime) |
|
190 | + { |
|
191 | + return $this->contentType = $mime; |
|
192 | + } |
|
193 | + public function expects($mime) |
|
194 | + { |
|
195 | + if (empty($mime)) return $this; |
|
196 | + $this->expected_type = Mime::getFullMime($mime); |
|
197 | + return $this; |
|
198 | + } |
|
199 | + /** |
|
200 | + * @param $field alias or field name |
|
201 | + * @return bool|mixed |
|
202 | + */ |
|
203 | + public function getIni($field) |
|
204 | + { |
|
205 | + $alias = self::optionAlias($field); |
|
206 | + return isset($this->options[$alias]) ? $this->options[$alias] : false; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @param $key |
|
211 | + * @return mixed |
|
212 | + */ |
|
213 | + protected static function optionAlias($key) |
|
214 | + { |
|
215 | + $alias = false; |
|
216 | + if (isset(self::$curlAlias[$key])) { |
|
217 | + $alias = self::$curlAlias[$key]; |
|
218 | + } elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) { |
|
219 | + $alias = $key; |
|
220 | + } |
|
221 | + return $alias; |
|
222 | + } |
|
223 | + |
|
224 | + public function addQuery($data) |
|
225 | + { |
|
226 | + if (!empty($data)) { |
|
227 | + if (is_array($data)) { |
|
228 | + $this->withURIQuery = http_build_query($data); |
|
229 | + } else if (is_string($data)) { |
|
230 | + $this->withURIQuery = $data; |
|
231 | + } else { |
|
232 | + throw new InvalidArgumentException('data must be array or string'); |
|
233 | + } |
|
234 | + } |
|
235 | + return $this; |
|
236 | + } |
|
237 | + |
|
238 | + public function post($uri, $payload = null, array $options = array()) |
|
239 | + { |
|
240 | + return $this->ini(Http::POST, $uri, $payload, $options); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param $uri |
|
245 | + * @param null $payload |
|
246 | + * @param array $options |
|
247 | + * @param null $response |
|
248 | + * @return string |
|
249 | + */ |
|
250 | + public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
|
251 | + { |
|
252 | + $response = $this->post($uri, $payload, $options)->send(); |
|
253 | + return $response->body; |
|
254 | + } |
|
255 | + /* no body */ |
|
256 | + |
|
257 | + protected function ini($method, $url, $data , array $options = array()) |
|
258 | + { |
|
259 | + $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
260 | + $this->addOptions($options); |
|
261 | + |
|
262 | + return $this; |
|
263 | + } |
|
264 | + |
|
265 | + public function addOptions(array $options = array()) |
|
266 | + { |
|
267 | + $this->options = $options + $this->options; |
|
268 | + $this->uri = $this->options['url']; |
|
269 | + return $this; |
|
270 | + } |
|
271 | + |
|
272 | + function put($uri, $payload = null, array $options = array()) |
|
273 | + { |
|
274 | + return $this->ini(Http::PUT, $uri, $payload, $options); |
|
275 | + } |
|
276 | + |
|
277 | + function patch($uri, $payload = null, array $options = array()) |
|
278 | + { |
|
279 | + return $this->ini(Http::PATCH, $uri, $payload, $options); |
|
280 | + } |
|
281 | + |
|
282 | + public function get($uri, array $options = array()) |
|
283 | + { |
|
284 | + return $this->ini(Http::GET, $uri, array(), $options); |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * @param $uri |
|
289 | + * @param array $options |
|
290 | + * @param null $response |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + public function quickGet($uri, array $options = array(), &$response = null) |
|
294 | + { |
|
295 | + $response = $this->get($uri, $options)->send(); |
|
296 | + return $response->body; |
|
297 | + } |
|
298 | + |
|
299 | + function options($uri, array $options = array()) |
|
300 | + { |
|
301 | + return $this->ini(Http::OPTIONS, $uri, array(), $options); |
|
302 | + } |
|
303 | + |
|
304 | + function head($uri, array $options = array()) |
|
305 | + { |
|
306 | + return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options); |
|
307 | + } |
|
308 | + |
|
309 | + function delete($uri, array $options = array()) |
|
310 | + { |
|
311 | + return $this->ini(Http::DELETE, $uri, array(), $options); |
|
312 | + } |
|
313 | + |
|
314 | + function trace($uri, array $options = array()) |
|
315 | + { |
|
316 | + return $this->ini(Http::TRACE, $uri, array(), $options); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @return Response |
|
321 | + */ |
|
322 | + public function send() |
|
323 | + { |
|
324 | + if (!$this->hasInitialized) |
|
325 | + $this->applyOptions(); |
|
326 | + $response = $this->makeResponse(); |
|
327 | + if ($this->endCallback) { |
|
328 | + $func = $this->endCallback; |
|
329 | + $func($response); |
|
330 | + } |
|
331 | + return $response; |
|
332 | + } |
|
333 | + |
|
334 | + public function applyOptions() |
|
335 | + { |
|
336 | + $curl = curl_init(); |
|
337 | + $this->curlHandle = $curl; |
|
338 | + $this->prepare(); |
|
339 | + $this->hasInitialized = true; |
|
340 | + return $this; |
|
341 | + } |
|
342 | + |
|
343 | + protected function prepare() |
|
344 | + { |
|
345 | + if (empty($this->options['url'])) { |
|
346 | + throw new InvalidArgumentException('url can not empty'); |
|
347 | + } |
|
348 | + |
|
349 | + if (isset($this->options['retry_times'])) { |
|
350 | + $this->retryTimes = abs($this->options['retry_times']); |
|
351 | + } |
|
352 | + |
|
353 | + if (isset($this->options['retry_duration'])) { |
|
354 | + $this->retryDuration = abs($this->options['retry_duration']); |
|
355 | + } |
|
356 | + |
|
357 | + if (isset($this->options['data'])) { |
|
358 | + $this->options['data'] = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data'];//for better compatibility |
|
359 | + } |
|
360 | + if (isset($this->withURIQuery)) { |
|
361 | + $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; |
|
362 | + $this->options['url'] .= $this->withURIQuery; |
|
363 | + } |
|
364 | + if (isset($this->options['callback'])) { |
|
365 | + $this->onEnd($this->options['callback']); |
|
366 | + unset($this->options['callback']); |
|
367 | + } |
|
368 | + //swap ip and host |
|
369 | + if (!empty($this->options['ip'])) { |
|
370 | + $matches = array(); |
|
371 | + preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
|
372 | + $host = $matches[1]; |
|
373 | + if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
|
374 | + $this->options['headers'] = array('Host: ' . $host); |
|
375 | + } else { |
|
376 | + $this->options['headers'][] = 'Host: ' . $host; |
|
377 | + } |
|
378 | + $this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//' . $this->options['ip'], $this->options['url']); |
|
379 | + unset($this->options['ip']); |
|
380 | + unset($host); |
|
381 | + } |
|
382 | + //process version |
|
383 | + if (!empty($this->options['http_version'])) { |
|
384 | + $version = $this->options['http_version']; |
|
385 | + if ($version == '1.0') { |
|
386 | + $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_0; |
|
387 | + } elseif ($version == '1.1') { |
|
388 | + $this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1; |
|
389 | + } |
|
390 | + |
|
391 | + unset($version); |
|
392 | + } |
|
393 | + |
|
394 | + //convert secs to milliseconds |
|
395 | + if (defined('CURLOPT_TIMEOUT_MS')) { |
|
396 | + if (!isset($this->options['timeout_ms'])) { |
|
397 | + $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
398 | + } else { |
|
399 | + $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + $cURLOptions = self::filterAndRaw($this->options); |
|
404 | + |
|
405 | + curl_setopt_array($this->curlHandle, $cURLOptions); |
|
406 | + |
|
407 | + return $this; |
|
408 | + } |
|
409 | + |
|
410 | + public function onEnd(callable $callback) |
|
411 | + { |
|
412 | + if (!is_callable($callback)) { |
|
413 | + throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
414 | + } |
|
415 | + |
|
416 | + $this->endCallback = $callback; |
|
417 | + return $this; |
|
418 | + } |
|
419 | + |
|
420 | + protected static function filterAndRaw(array &$options) |
|
421 | + { |
|
422 | + $opts = array(); |
|
423 | + foreach ($options as $key => $val) { |
|
424 | + $alias = self::optionAlias($key); |
|
425 | + $options[$alias] = $val; |
|
426 | + if ($alias) { |
|
427 | + $opts[constant($alias)] = $val; |
|
428 | + } |
|
429 | + unset($options[$key]); |
|
430 | + } |
|
431 | + return $opts; |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + public function makeResponse($isMultiCurl = false) |
|
436 | + { |
|
437 | + $handle = $this->curlHandle; |
|
438 | + $body = $errno = null; |
|
439 | + Helper::retry($this->retryTimes, function()use(&$body, &$errno, $isMultiCurl, $handle){ |
|
440 | + $body = $isMultiCurl ? curl_multi_getcontent($handle) : curl_exec($handle); |
|
441 | + $errno = curl_errno($handle); |
|
442 | + var_dump(curl_error($handle), time()); |
|
443 | + ob_flush(); |
|
444 | + flush(); |
|
445 | + return 0 == $errno; |
|
446 | + }, $this->retryDuration); |
|
447 | + |
|
448 | + $info = curl_getinfo($this->curlHandle); |
|
449 | + $error = curl_error($this->curlHandle); |
|
450 | + $response = Response::create($this, $body, $info, $errno, $error); |
|
451 | + if (!is_null(self::$logger)) { |
|
452 | + self::log($response); |
|
453 | + } |
|
454 | + |
|
455 | + return $response; |
|
456 | + } |
|
457 | + |
|
458 | + private static function log(Response $response) |
|
459 | + { |
|
460 | + if ($response->hasErrors()) { |
|
461 | + self::$logger->error($response->request->getURI() . "\t" . $response->error, array( |
|
462 | + 'response' => print_r($response, 1), |
|
463 | + )); |
|
464 | + } |
|
465 | + |
|
466 | + } |
|
467 | 467 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | protected static $curlAlias = array( |
20 | 20 | 'url' => 'CURLOPT_URL', |
21 | 21 | 'uri' => 'CURLOPT_URL', |
22 | - 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
|
22 | + 'debug' => 'CURLOPT_VERBOSE', //for debug verbose |
|
23 | 23 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
24 | 24 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
25 | 25 | 'ua' => 'CURLOPT_USERAGENT', |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | 'follow_location' => true, |
63 | 63 | 'timeout' => 0, |
64 | 64 | // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
65 | - 'retry_times' => 1,//redo task when failed |
|
66 | - 'retry_duration' => 0,//in seconds |
|
65 | + 'retry_times' => 1, //redo task when failed |
|
66 | + 'retry_duration' => 0, //in seconds |
|
67 | 67 | ); |
68 | 68 | protected $endCallback; |
69 | 69 | protected $withURIQuery; |
@@ -254,9 +254,9 @@ discard block |
||
254 | 254 | } |
255 | 255 | /* no body */ |
256 | 256 | |
257 | - protected function ini($method, $url, $data , array $options = array()) |
|
257 | + protected function ini($method, $url, $data, array $options = array()) |
|
258 | 258 | { |
259 | - $options = array('url' => $url, 'method' => $method, 'data' => $data) + $options; |
|
259 | + $options = array('url' => $url, 'method' => $method, 'data' => $data)+$options; |
|
260 | 260 | $this->addOptions($options); |
261 | 261 | |
262 | 262 | return $this; |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | |
265 | 265 | public function addOptions(array $options = array()) |
266 | 266 | { |
267 | - $this->options = $options + $this->options; |
|
267 | + $this->options = $options+$this->options; |
|
268 | 268 | $this->uri = $this->options['url']; |
269 | 269 | return $this; |
270 | 270 | } |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | } |
356 | 356 | |
357 | 357 | if (isset($this->options['data'])) { |
358 | - $this->options['data'] = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data'];//for better compatibility |
|
358 | + $this->options['data'] = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; //for better compatibility |
|
359 | 359 | } |
360 | 360 | if (isset($this->withURIQuery)) { |
361 | 361 | $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; |
@@ -371,11 +371,11 @@ discard block |
||
371 | 371 | preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches); |
372 | 372 | $host = $matches[1]; |
373 | 373 | if (empty($this->options['headers']) || !is_array($this->options['headers'])) { |
374 | - $this->options['headers'] = array('Host: ' . $host); |
|
374 | + $this->options['headers'] = array('Host: '.$host); |
|
375 | 375 | } else { |
376 | - $this->options['headers'][] = 'Host: ' . $host; |
|
376 | + $this->options['headers'][] = 'Host: '.$host; |
|
377 | 377 | } |
378 | - $this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//' . $this->options['ip'], $this->options['url']); |
|
378 | + $this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//'.$this->options['ip'], $this->options['url']); |
|
379 | 379 | unset($this->options['ip']); |
380 | 380 | unset($host); |
381 | 381 | } |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | //convert secs to milliseconds |
395 | 395 | if (defined('CURLOPT_TIMEOUT_MS')) { |
396 | 396 | if (!isset($this->options['timeout_ms'])) { |
397 | - $this->options['timeout_ms'] = intval($this->options['timeout'] * 1000); |
|
397 | + $this->options['timeout_ms'] = intval($this->options['timeout']*1000); |
|
398 | 398 | } else { |
399 | 399 | $this->options['timeout_ms'] = intval($this->options['timeout_ms']); |
400 | 400 | } |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | public function onEnd(callable $callback) |
411 | 411 | { |
412 | 412 | if (!is_callable($callback)) { |
413 | - throw new InvalidArgumentException('callback not is callable :' . print_r($callback, 1)); |
|
413 | + throw new InvalidArgumentException('callback not is callable :'.print_r($callback, 1)); |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | $this->endCallback = $callback; |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | private static function log(Response $response) |
459 | 459 | { |
460 | 460 | if ($response->hasErrors()) { |
461 | - self::$logger->error($response->request->getURI() . "\t" . $response->error, array( |
|
461 | + self::$logger->error($response->request->getURI()."\t".$response->error, array( |
|
462 | 462 | 'response' => print_r($response, 1), |
463 | 463 | )); |
464 | 464 | } |
@@ -15,22 +15,22 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class MultiRequest |
17 | 17 | { |
18 | - protected static $requestPool = array(); |
|
19 | - protected static $multiHandler; |
|
20 | - private static $instance; |
|
18 | + protected static $requestPool = array(); |
|
19 | + protected static $multiHandler; |
|
20 | + private static $instance; |
|
21 | 21 | |
22 | - protected function __construct() |
|
23 | - { |
|
24 | - } |
|
22 | + protected function __construct() |
|
23 | + { |
|
24 | + } |
|
25 | 25 | |
26 | - public static function create() |
|
27 | - { |
|
28 | - if (!(self::$instance instanceof self)) { |
|
29 | - self::$instance = new self; |
|
30 | - } |
|
31 | - self::prepare(); |
|
32 | - return self::$instance; |
|
33 | - } |
|
26 | + public static function create() |
|
27 | + { |
|
28 | + if (!(self::$instance instanceof self)) { |
|
29 | + self::$instance = new self; |
|
30 | + } |
|
31 | + self::prepare(); |
|
32 | + return self::$instance; |
|
33 | + } |
|
34 | 34 | // protected $isLazy = false; |
35 | 35 | // public function lazyStart(){ |
36 | 36 | // $this->isLazy = true; |
@@ -39,83 +39,83 @@ discard block |
||
39 | 39 | // $this->isLazy = false; |
40 | 40 | // return $this->execute(); |
41 | 41 | // } |
42 | - protected function prepare() |
|
43 | - { |
|
42 | + protected function prepare() |
|
43 | + { |
|
44 | 44 | // if(is_null(self::$multiHandler) || !$this->isLazy){ |
45 | - self::$multiHandler = curl_multi_init(); |
|
45 | + self::$multiHandler = curl_multi_init(); |
|
46 | 46 | // } |
47 | - } |
|
47 | + } |
|
48 | 48 | |
49 | - public function add($method, $uri, $payload, array $options = array()) |
|
50 | - { |
|
51 | - $options = array( |
|
52 | - 'method' => $method, |
|
53 | - 'url' => $uri, |
|
54 | - 'data' => $payload, |
|
55 | - ) + $options; |
|
56 | - $this->addOptions(array($options)); |
|
57 | - return $this; |
|
58 | - } |
|
49 | + public function add($method, $uri, $payload, array $options = array()) |
|
50 | + { |
|
51 | + $options = array( |
|
52 | + 'method' => $method, |
|
53 | + 'url' => $uri, |
|
54 | + 'data' => $payload, |
|
55 | + ) + $options; |
|
56 | + $this->addOptions(array($options)); |
|
57 | + return $this; |
|
58 | + } |
|
59 | 59 | |
60 | 60 | |
61 | - /** |
|
62 | - * @param array $URLOptions |
|
63 | - * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d')) |
|
64 | - * @return $this |
|
65 | - */ |
|
66 | - public function addOptions(array $URLOptions) |
|
67 | - { |
|
68 | - foreach ($URLOptions as $options) { |
|
69 | - $request = Request::create()->addOptions($options)->applyOptions(); |
|
70 | - if (isset($options['callback'])) { |
|
71 | - $request->onEnd($options['callback']); |
|
72 | - } |
|
73 | - $this->import($request); |
|
74 | - } |
|
75 | - return $this; |
|
76 | - } |
|
61 | + /** |
|
62 | + * @param array $URLOptions |
|
63 | + * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d')) |
|
64 | + * @return $this |
|
65 | + */ |
|
66 | + public function addOptions(array $URLOptions) |
|
67 | + { |
|
68 | + foreach ($URLOptions as $options) { |
|
69 | + $request = Request::create()->addOptions($options)->applyOptions(); |
|
70 | + if (isset($options['callback'])) { |
|
71 | + $request->onEnd($options['callback']); |
|
72 | + } |
|
73 | + $this->import($request); |
|
74 | + } |
|
75 | + return $this; |
|
76 | + } |
|
77 | 77 | |
78 | - public function import(Request $request) |
|
79 | - { |
|
80 | - if (!is_resource($request->curlHandle)) { |
|
81 | - throw new InvalidArgumentException('Request curl handle is not initialized'); |
|
82 | - } |
|
83 | - curl_multi_add_handle(self::$multiHandler, $request->curlHandle); |
|
84 | - self::$requestPool[] = $request; |
|
85 | - return $this; |
|
86 | - } |
|
78 | + public function import(Request $request) |
|
79 | + { |
|
80 | + if (!is_resource($request->curlHandle)) { |
|
81 | + throw new InvalidArgumentException('Request curl handle is not initialized'); |
|
82 | + } |
|
83 | + curl_multi_add_handle(self::$multiHandler, $request->curlHandle); |
|
84 | + self::$requestPool[] = $request; |
|
85 | + return $this; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return array(Response) |
|
90 | - */ |
|
91 | - public function execute() |
|
92 | - { |
|
88 | + /** |
|
89 | + * @return array(Response) |
|
90 | + */ |
|
91 | + public function execute() |
|
92 | + { |
|
93 | 93 | // if($this->isLazy) return array(); |
94 | - $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
95 | - do { |
|
96 | - curl_multi_exec(self::$multiHandler, $active); |
|
97 | - // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
|
98 | - // https://bugs.php.net/bug.php?id=63411 |
|
99 | - if (curl_multi_select(self::$multiHandler) == -1) { |
|
100 | - usleep($sleepTime); |
|
101 | - } |
|
102 | - usleep($sleepTime); |
|
103 | - } while ($active); |
|
104 | - $return = array(); |
|
105 | - foreach (self::$requestPool as $request) { |
|
106 | - $response = $request->makeResponse(true); |
|
107 | - $func = $response->request->endCallback(); |
|
108 | - if (isset($func)) { |
|
109 | - $func($response); |
|
110 | - } |
|
111 | - $return[] = $response; |
|
112 | - curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); |
|
113 | - curl_close($request->curlHandle); |
|
114 | - } |
|
115 | - curl_multi_close(self::$multiHandler); |
|
116 | - self::$requestPool = array(); |
|
117 | - self::$multiHandler = null; |
|
118 | - return $return; |
|
119 | - } |
|
94 | + $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
95 | + do { |
|
96 | + curl_multi_exec(self::$multiHandler, $active); |
|
97 | + // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
|
98 | + // https://bugs.php.net/bug.php?id=63411 |
|
99 | + if (curl_multi_select(self::$multiHandler) == -1) { |
|
100 | + usleep($sleepTime); |
|
101 | + } |
|
102 | + usleep($sleepTime); |
|
103 | + } while ($active); |
|
104 | + $return = array(); |
|
105 | + foreach (self::$requestPool as $request) { |
|
106 | + $response = $request->makeResponse(true); |
|
107 | + $func = $response->request->endCallback(); |
|
108 | + if (isset($func)) { |
|
109 | + $func($response); |
|
110 | + } |
|
111 | + $return[] = $response; |
|
112 | + curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); |
|
113 | + curl_close($request->curlHandle); |
|
114 | + } |
|
115 | + curl_multi_close(self::$multiHandler); |
|
116 | + self::$requestPool = array(); |
|
117 | + self::$multiHandler = null; |
|
118 | + return $return; |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | 'method' => $method, |
53 | 53 | 'url' => $uri, |
54 | 54 | 'data' => $payload, |
55 | - ) + $options; |
|
55 | + )+$options; |
|
56 | 56 | $this->addOptions(array($options)); |
57 | 57 | return $this; |
58 | 58 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | public function execute() |
92 | 92 | { |
93 | 93 | // if($this->isLazy) return array(); |
94 | - $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
94 | + $sleepTime = 1000; //microsecond, prevent CPU 100% |
|
95 | 95 | do { |
96 | 96 | curl_multi_exec(self::$multiHandler, $active); |
97 | 97 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | usleep($sleepTime); |
101 | 101 | } |
102 | 102 | usleep($sleepTime); |
103 | - } while ($active); |
|
103 | + }while ($active); |
|
104 | 104 | $return = array(); |
105 | 105 | foreach (self::$requestPool as $request) { |
106 | 106 | $response = $request->makeResponse(true); |