@@ -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 | { |
@@ -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; |
@@ -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'; |
@@ -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 | } |
@@ -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); |