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