@@ -13,5 +13,5 @@ |
||
13 | 13 | |
14 | 14 | |
15 | 15 | class InvalidOperationException extends \LogicException{ |
16 | - protected $code = 2; |
|
16 | + protected $code = 2; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -12,6 +12,6 @@ |
||
12 | 12 | namespace MultiHttp\Exception; |
13 | 13 | |
14 | 14 | |
15 | -class InvalidOperationException extends \LogicException{ |
|
15 | +class InvalidOperationException extends \LogicException { |
|
16 | 16 | protected $code = 2; |
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -13,5 +13,5 @@ |
||
13 | 13 | |
14 | 14 | |
15 | 15 | class UnexpectedResponseException extends \UnexpectedValueException { |
16 | - protected $code = 3; |
|
16 | + protected $code = 3; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -13,5 +13,5 @@ |
||
13 | 13 | |
14 | 14 | |
15 | 15 | class InvalidArgumentException extends \LogicException{ |
16 | - protected $code = 1; |
|
16 | + protected $code = 1; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -12,6 +12,6 @@ |
||
12 | 12 | namespace MultiHttp\Exception; |
13 | 13 | |
14 | 14 | |
15 | -class InvalidArgumentException extends \LogicException{ |
|
15 | +class InvalidArgumentException extends \LogicException { |
|
16 | 16 | protected $code = 1; |
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -54,10 +54,11 @@ |
||
54 | 54 | $headers = explode(PHP_EOL, $headers); |
55 | 55 | array_shift($headers); // HTTP HEADER |
56 | 56 | foreach($headers as $h) { |
57 | - if(false !== strpos($h, ':')) |
|
58 | - list($k, $v) = explode(':', $h, 2); |
|
59 | - else |
|
60 | - list($k, $v) = array($h,''); |
|
57 | + if(false !== strpos($h, ':')) { |
|
58 | + list($k, $v) = explode(':', $h, 2); |
|
59 | + } else { |
|
60 | + list($k, $v) = array($h,''); |
|
61 | + } |
|
61 | 62 | |
62 | 63 | $this->header[trim($k)] = trim($v); |
63 | 64 | } |
@@ -15,81 +15,81 @@ |
||
15 | 15 | |
16 | 16 | class Response |
17 | 17 | { |
18 | - public |
|
19 | - $code, |
|
20 | - $errorCode, |
|
21 | - $error, |
|
22 | - $header, |
|
23 | - $body, |
|
24 | - /** |
|
25 | - * @var Request |
|
26 | - */ |
|
27 | - $request, |
|
28 | - $contentType, |
|
29 | - $charset, |
|
30 | - $duration, |
|
31 | - $info; |
|
32 | - protected function __construct() |
|
33 | - { |
|
34 | - } |
|
18 | + public |
|
19 | + $code, |
|
20 | + $errorCode, |
|
21 | + $error, |
|
22 | + $header, |
|
23 | + $body, |
|
24 | + /** |
|
25 | + * @var Request |
|
26 | + */ |
|
27 | + $request, |
|
28 | + $contentType, |
|
29 | + $charset, |
|
30 | + $duration, |
|
31 | + $info; |
|
32 | + protected function __construct() |
|
33 | + { |
|
34 | + } |
|
35 | 35 | |
36 | - public static function create(Request $request, $body, $info, $errorCode, $error){ |
|
37 | - $self = new self; |
|
38 | - $self->request = $request; |
|
39 | - $self->body = $body; |
|
40 | - $self->info = $info; |
|
41 | - $self->errorCode = $errorCode; |
|
42 | - $self->error = $error; |
|
43 | - $self->parse(); |
|
44 | - $self->check(); |
|
45 | - return $self; |
|
46 | - } |
|
47 | - public function check(){ |
|
36 | + public static function create(Request $request, $body, $info, $errorCode, $error){ |
|
37 | + $self = new self; |
|
38 | + $self->request = $request; |
|
39 | + $self->body = $body; |
|
40 | + $self->info = $info; |
|
41 | + $self->errorCode = $errorCode; |
|
42 | + $self->error = $error; |
|
43 | + $self->parse(); |
|
44 | + $self->check(); |
|
45 | + return $self; |
|
46 | + } |
|
47 | + public function check(){ |
|
48 | 48 | |
49 | - } |
|
50 | - public function parse(){ |
|
51 | - //has header |
|
52 | - if($this->request->getIni('header')){ |
|
53 | - $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
|
54 | - $this->body = substr($this->body, $this->info['header_size']); |
|
55 | - $headers = explode(PHP_EOL, $headers); |
|
56 | - array_shift($headers); // HTTP HEADER |
|
57 | - foreach($headers as $h) { |
|
58 | - if(false !== strpos($h, ':')) |
|
59 | - list($k, $v) = explode(':', $h, 2); |
|
60 | - else |
|
61 | - list($k, $v) = array($h,''); |
|
49 | + } |
|
50 | + public function parse(){ |
|
51 | + //has header |
|
52 | + if($this->request->getIni('header')){ |
|
53 | + $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
|
54 | + $this->body = substr($this->body, $this->info['header_size']); |
|
55 | + $headers = explode(PHP_EOL, $headers); |
|
56 | + array_shift($headers); // HTTP HEADER |
|
57 | + foreach($headers as $h) { |
|
58 | + if(false !== strpos($h, ':')) |
|
59 | + list($k, $v) = explode(':', $h, 2); |
|
60 | + else |
|
61 | + list($k, $v) = array($h,''); |
|
62 | 62 | |
63 | - $this->header[trim($k)] = trim($v); |
|
64 | - } |
|
65 | - } |
|
66 | - $this->code = $this->info['http_code']; |
|
67 | - $this->duration = $this->info['total_time']; |
|
68 | - $this->contentType = $this->info['content_type']; |
|
69 | - $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
|
70 | - $content_type = explode(';', $content_type); |
|
71 | - $this->contentType = $content_type[0]; |
|
72 | - if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
|
73 | - list( , $this->charset) = explode('=', $content_type[1]); |
|
74 | - } |
|
75 | - } |
|
63 | + $this->header[trim($k)] = trim($v); |
|
64 | + } |
|
65 | + } |
|
66 | + $this->code = $this->info['http_code']; |
|
67 | + $this->duration = $this->info['total_time']; |
|
68 | + $this->contentType = $this->info['content_type']; |
|
69 | + $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
|
70 | + $content_type = explode(';', $content_type); |
|
71 | + $this->contentType = $content_type[0]; |
|
72 | + if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
|
73 | + list( , $this->charset) = explode('=', $content_type[1]); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Status Code Definitions |
|
79 | - * |
|
80 | - * Informational 1xx |
|
81 | - * Successful 2xx |
|
82 | - * Redirection 3xx |
|
83 | - * Client Error 4xx |
|
84 | - * Server Error 5xx |
|
85 | - * |
|
86 | - * http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
|
87 | - * |
|
88 | - * @return bool Did we receive a 4xx or 5xx? |
|
89 | - */ |
|
90 | - public function hasErrors() |
|
91 | - { |
|
92 | - return $this->code == 0 || $this->code >= 400; |
|
93 | - } |
|
77 | + /** |
|
78 | + * Status Code Definitions |
|
79 | + * |
|
80 | + * Informational 1xx |
|
81 | + * Successful 2xx |
|
82 | + * Redirection 3xx |
|
83 | + * Client Error 4xx |
|
84 | + * Server Error 5xx |
|
85 | + * |
|
86 | + * http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
|
87 | + * |
|
88 | + * @return bool Did we receive a 4xx or 5xx? |
|
89 | + */ |
|
90 | + public function hasErrors() |
|
91 | + { |
|
92 | + return $this->code == 0 || $this->code >= 400; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | } |
96 | 96 | \ No newline at end of file |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | { |
34 | 34 | } |
35 | 35 | |
36 | - public static function create(Request $request, $body, $info, $errorCode, $error){ |
|
36 | + public static function create(Request $request, $body, $info, $errorCode, $error) { |
|
37 | 37 | $self = new self; |
38 | 38 | $self->request = $request; |
39 | 39 | $self->body = $body; |
@@ -44,21 +44,21 @@ discard block |
||
44 | 44 | $self->check(); |
45 | 45 | return $self; |
46 | 46 | } |
47 | - public function check(){ |
|
47 | + public function check() { |
|
48 | 48 | |
49 | 49 | } |
50 | - public function parse(){ |
|
50 | + public function parse() { |
|
51 | 51 | //has header |
52 | - if($this->request->getIni('header')){ |
|
52 | + if ($this->request->getIni('header')) { |
|
53 | 53 | $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
54 | 54 | $this->body = substr($this->body, $this->info['header_size']); |
55 | 55 | $headers = explode(PHP_EOL, $headers); |
56 | 56 | array_shift($headers); // HTTP HEADER |
57 | - foreach($headers as $h) { |
|
58 | - if(false !== strpos($h, ':')) |
|
57 | + foreach ($headers as $h) { |
|
58 | + if (false !== strpos($h, ':')) |
|
59 | 59 | list($k, $v) = explode(':', $h, 2); |
60 | 60 | else |
61 | - list($k, $v) = array($h,''); |
|
61 | + list($k, $v) = array($h, ''); |
|
62 | 62 | |
63 | 63 | $this->header[trim($k)] = trim($v); |
64 | 64 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | $content_type = explode(';', $content_type); |
71 | 71 | $this->contentType = $content_type[0]; |
72 | 72 | if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
73 | - list( , $this->charset) = explode('=', $content_type[1]); |
|
73 | + list(, $this->charset) = explode('=', $content_type[1]); |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | return $this; |
188 | 188 | } |
189 | 189 | /** |
190 | - * @param $field alias or field name |
|
190 | + * @param string $field alias or field name |
|
191 | 191 | * @return bool|mixed |
192 | 192 | */ |
193 | 193 | public function getIni($field) |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | |
199 | 199 | /** |
200 | 200 | * @param $key |
201 | - * @return mixed |
|
201 | + * @return string |
|
202 | 202 | */ |
203 | 203 | protected static function optionAlias($key) |
204 | 204 | { |
@@ -152,7 +152,9 @@ discard block |
||
152 | 152 | } |
153 | 153 | public function mime($mime) |
154 | 154 | { |
155 | - if (empty($mime)) return $this; |
|
155 | + if (empty($mime)) { |
|
156 | + return $this; |
|
157 | + } |
|
156 | 158 | $this->content_type = $this->expected_type = Mime::getFullMime($mime); |
157 | 159 | if ($this->isUpload()) { |
158 | 160 | $this->neverSerializePayload(); |
@@ -182,7 +184,9 @@ discard block |
||
182 | 184 | } |
183 | 185 | public function expects($mime) |
184 | 186 | { |
185 | - if (empty($mime)) return $this; |
|
187 | + if (empty($mime)) { |
|
188 | + return $this; |
|
189 | + } |
|
186 | 190 | $this->expected_type = Mime::getFullMime($mime); |
187 | 191 | return $this; |
188 | 192 | } |
@@ -287,8 +291,9 @@ discard block |
||
287 | 291 | */ |
288 | 292 | public function send() |
289 | 293 | { |
290 | - if (!$this->hasInitialized) |
|
291 | - $this->applyOptions(); |
|
294 | + if (!$this->hasInitialized) { |
|
295 | + $this->applyOptions(); |
|
296 | + } |
|
292 | 297 | $response = $this->makeResponse(); |
293 | 298 | if ($this->endCallback) { |
294 | 299 | $func = $this->endCallback; |
@@ -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 | } |
@@ -9,53 +9,53 @@ |
||
9 | 9 | */ |
10 | 10 | class Mime |
11 | 11 | { |
12 | - const JSON = 'application/json'; |
|
13 | - const XML = 'application/xml'; |
|
14 | - const XHTML = 'application/html+xml'; |
|
15 | - const FORM = 'application/x-www-form-urlencoded'; |
|
16 | - const UPLOAD = 'multipart/form-data'; |
|
17 | - const PLAIN = 'text/plain'; |
|
18 | - const JS = 'text/javascript'; |
|
19 | - const HTML = 'text/html'; |
|
20 | - const YAML = 'application/x-yaml'; |
|
21 | - const CSV = 'text/csv'; |
|
12 | + const JSON = 'application/json'; |
|
13 | + const XML = 'application/xml'; |
|
14 | + const XHTML = 'application/html+xml'; |
|
15 | + const FORM = 'application/x-www-form-urlencoded'; |
|
16 | + const UPLOAD = 'multipart/form-data'; |
|
17 | + const PLAIN = 'text/plain'; |
|
18 | + const JS = 'text/javascript'; |
|
19 | + const HTML = 'text/html'; |
|
20 | + const YAML = 'application/x-yaml'; |
|
21 | + const CSV = 'text/csv'; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Map short name for a mime type |
|
25 | - * to a full proper mime type |
|
26 | - */ |
|
27 | - public static $mimes = array( |
|
28 | - 'json' => self::JSON, |
|
29 | - 'xml' => self::XML, |
|
30 | - 'form' => self::FORM, |
|
31 | - 'plain' => self::PLAIN, |
|
32 | - 'text' => self::PLAIN, |
|
33 | - 'upload' => self::UPLOAD, |
|
34 | - 'html' => self::HTML, |
|
35 | - 'xhtml' => self::XHTML, |
|
36 | - 'js' => self::JS, |
|
37 | - 'javascript'=> self::JS, |
|
38 | - 'yaml' => self::YAML, |
|
39 | - 'csv' => self::CSV, |
|
40 | - ); |
|
23 | + /** |
|
24 | + * Map short name for a mime type |
|
25 | + * to a full proper mime type |
|
26 | + */ |
|
27 | + public static $mimes = array( |
|
28 | + 'json' => self::JSON, |
|
29 | + 'xml' => self::XML, |
|
30 | + 'form' => self::FORM, |
|
31 | + 'plain' => self::PLAIN, |
|
32 | + 'text' => self::PLAIN, |
|
33 | + 'upload' => self::UPLOAD, |
|
34 | + 'html' => self::HTML, |
|
35 | + 'xhtml' => self::XHTML, |
|
36 | + 'js' => self::JS, |
|
37 | + 'javascript'=> self::JS, |
|
38 | + 'yaml' => self::YAML, |
|
39 | + 'csv' => self::CSV, |
|
40 | + ); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Get the full Mime Type name from a "short name". |
|
44 | - * Returns the short if no mapping was found. |
|
45 | - * @param string $short_name common name for mime type (e.g. json) |
|
46 | - * @return string full mime type (e.g. application/json) |
|
47 | - */ |
|
48 | - public static function getFullMime($short_name) |
|
49 | - { |
|
50 | - return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name; |
|
51 | - } |
|
42 | + /** |
|
43 | + * Get the full Mime Type name from a "short name". |
|
44 | + * Returns the short if no mapping was found. |
|
45 | + * @param string $short_name common name for mime type (e.g. json) |
|
46 | + * @return string full mime type (e.g. application/json) |
|
47 | + */ |
|
48 | + public static function getFullMime($short_name) |
|
49 | + { |
|
50 | + return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $short_name |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public static function supportsMimeType($short_name) |
|
58 | - { |
|
59 | - return array_key_exists($short_name, self::$mimes); |
|
60 | - } |
|
53 | + /** |
|
54 | + * @param string $short_name |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public static function supportsMimeType($short_name) |
|
58 | + { |
|
59 | + return array_key_exists($short_name, self::$mimes); |
|
60 | + } |
|
61 | 61 | } |
@@ -12,26 +12,26 @@ |
||
12 | 12 | namespace MultiHttp; |
13 | 13 | |
14 | 14 | trait RequestTrait { |
15 | - protected $expectContentType = null; |
|
16 | - public function expectsJson(){ |
|
17 | - $this->expectContentType = Mime::JSON; |
|
18 | - } |
|
19 | - public function expectsXml(){ |
|
20 | - $this->expectContentType = Mime::XML; |
|
21 | - } |
|
15 | + protected $expectContentType = null; |
|
16 | + public function expectsJson(){ |
|
17 | + $this->expectContentType = Mime::JSON; |
|
18 | + } |
|
19 | + public function expectsXml(){ |
|
20 | + $this->expectContentType = Mime::XML; |
|
21 | + } |
|
22 | 22 | |
23 | - protected function json(){ |
|
23 | + protected function json(){ |
|
24 | 24 | |
25 | - } |
|
26 | - protected function unJson(){ |
|
25 | + } |
|
26 | + protected function unJson(){ |
|
27 | 27 | |
28 | - } |
|
28 | + } |
|
29 | 29 | |
30 | - protected function xml(){ |
|
30 | + protected function xml(){ |
|
31 | 31 | |
32 | - } |
|
33 | - protected function unXml(){ |
|
32 | + } |
|
33 | + protected function unXml(){ |
|
34 | 34 | |
35 | - } |
|
35 | + } |
|
36 | 36 | } |
37 | 37 |
@@ -13,24 +13,24 @@ |
||
13 | 13 | |
14 | 14 | trait RequestTrait { |
15 | 15 | protected $expectContentType = null; |
16 | - public function expectsJson(){ |
|
16 | + public function expectsJson() { |
|
17 | 17 | $this->expectContentType = Mime::JSON; |
18 | 18 | } |
19 | - public function expectsXml(){ |
|
19 | + public function expectsXml() { |
|
20 | 20 | $this->expectContentType = Mime::XML; |
21 | 21 | } |
22 | 22 | |
23 | - protected function json(){ |
|
23 | + protected function json() { |
|
24 | 24 | |
25 | 25 | } |
26 | - protected function unJson(){ |
|
26 | + protected function unJson() { |
|
27 | 27 | |
28 | 28 | } |
29 | 29 | |
30 | - protected function xml(){ |
|
30 | + protected function xml() { |
|
31 | 31 | |
32 | 32 | } |
33 | - protected function unXml(){ |
|
33 | + protected function unXml() { |
|
34 | 34 | |
35 | 35 | } |
36 | 36 | } |
@@ -19,70 +19,70 @@ |
||
19 | 19 | */ |
20 | 20 | trait JsonTrait |
21 | 21 | { |
22 | - /** |
|
23 | - * @return Request |
|
24 | - */ |
|
25 | - public function expectsJson() |
|
26 | - { |
|
27 | - return $this->expectsMime('json'); |
|
28 | - } |
|
22 | + /** |
|
23 | + * @return Request |
|
24 | + */ |
|
25 | + public function expectsJson() |
|
26 | + { |
|
27 | + return $this->expectsMime('json'); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return Request |
|
32 | - */ |
|
33 | - public function sendJson() |
|
34 | - { |
|
35 | - return $this->sendMime('json'); |
|
36 | - } |
|
30 | + /** |
|
31 | + * @return Request |
|
32 | + */ |
|
33 | + public function sendJson() |
|
34 | + { |
|
35 | + return $this->sendMime('json'); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param $body |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function json($body) |
|
43 | - { |
|
44 | - return json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
45 | - } |
|
38 | + /** |
|
39 | + * @param $body |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function json($body) |
|
43 | + { |
|
44 | + return json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param $body |
|
49 | - * @return mixed |
|
50 | - */ |
|
51 | - public function unJson($body) |
|
52 | - { |
|
53 | - $parsed = json_decode($body, true); |
|
54 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
55 | - return $parsed; |
|
56 | - } |
|
47 | + /** |
|
48 | + * @param $body |
|
49 | + * @return mixed |
|
50 | + */ |
|
51 | + public function unJson($body) |
|
52 | + { |
|
53 | + $parsed = json_decode($body, true); |
|
54 | + if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
55 | + return $parsed; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - private static function jsonLastErrorMsg(){ |
|
62 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
63 | - switch (json_last_error()) { |
|
64 | - case JSON_ERROR_NONE: |
|
65 | - return ' - No errors'; |
|
66 | - break; |
|
67 | - case JSON_ERROR_DEPTH: |
|
68 | - return ' - Maximum stack depth exceeded'; |
|
69 | - break; |
|
70 | - case JSON_ERROR_STATE_MISMATCH: |
|
71 | - return ' - Underflow or the modes mismatch'; |
|
72 | - break; |
|
73 | - case JSON_ERROR_CTRL_CHAR: |
|
74 | - return ' - Unexpected control character found'; |
|
75 | - break; |
|
76 | - case JSON_ERROR_SYNTAX: |
|
77 | - return ' - Syntax error, malformed JSON'; |
|
78 | - break; |
|
79 | - case JSON_ERROR_UTF8: |
|
80 | - return ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
81 | - break; |
|
82 | - default: |
|
83 | - return ' - Unknown error'; |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + private static function jsonLastErrorMsg(){ |
|
62 | + if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
63 | + switch (json_last_error()) { |
|
64 | + case JSON_ERROR_NONE: |
|
65 | + return ' - No errors'; |
|
66 | + break; |
|
67 | + case JSON_ERROR_DEPTH: |
|
68 | + return ' - Maximum stack depth exceeded'; |
|
69 | + break; |
|
70 | + case JSON_ERROR_STATE_MISMATCH: |
|
71 | + return ' - Underflow or the modes mismatch'; |
|
72 | + break; |
|
73 | + case JSON_ERROR_CTRL_CHAR: |
|
74 | + return ' - Unexpected control character found'; |
|
75 | + break; |
|
76 | + case JSON_ERROR_SYNTAX: |
|
77 | + return ' - Syntax error, malformed JSON'; |
|
78 | + break; |
|
79 | + case JSON_ERROR_UTF8: |
|
80 | + return ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
81 | + break; |
|
82 | + default: |
|
83 | + return ' - Unknown error'; |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | } |
88 | 88 |
@@ -51,15 +51,15 @@ |
||
51 | 51 | public function unJson($body) |
52 | 52 | { |
53 | 53 | $parsed = json_decode($body, true); |
54 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
54 | + if (json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '.self::jsonLastErrorMsg().', raw body: '.$body); |
|
55 | 55 | return $parsed; |
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @return string |
60 | 60 | */ |
61 | - private static function jsonLastErrorMsg(){ |
|
62 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
61 | + private static function jsonLastErrorMsg() { |
|
62 | + if (function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
63 | 63 | switch (json_last_error()) { |
64 | 64 | case JSON_ERROR_NONE: |
65 | 65 | return ' - No errors'; |
@@ -51,7 +51,9 @@ discard block |
||
51 | 51 | public function unJson($body) |
52 | 52 | { |
53 | 53 | $parsed = json_decode($body, true); |
54 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
54 | + if(json_last_error() !== JSON_ERROR_NONE) { |
|
55 | + throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
56 | + } |
|
55 | 57 | return $parsed; |
56 | 58 | } |
57 | 59 | |
@@ -59,7 +61,9 @@ discard block |
||
59 | 61 | * @return string |
60 | 62 | */ |
61 | 63 | private static function jsonLastErrorMsg(){ |
62 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
64 | + if(function_exists('json_last_error_msg')) { |
|
65 | + return json_last_error_msg(); |
|
66 | + } |
|
63 | 67 | switch (json_last_error()) { |
64 | 68 | case JSON_ERROR_NONE: |
65 | 69 | return ' - No errors'; |
@@ -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); |