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