| @@ -13,5 +13,5 @@ | ||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 |  class InvalidOperationException extends \LogicException{ | 
| 16 | - protected $code = 2; | |
| 16 | + protected $code = 2; | |
| 17 | 17 | } | 
| 18 | 18 | \ No newline at end of file | 
| @@ -12,6 +12,6 @@ | ||
| 12 | 12 | namespace MultiHttp\Exception; | 
| 13 | 13 | |
| 14 | 14 | |
| 15 | -class InvalidOperationException extends \LogicException{ | |
| 15 | +class InvalidOperationException extends \LogicException { | |
| 16 | 16 | protected $code = 2; | 
| 17 | 17 | } | 
| 18 | 18 | \ No newline at end of file | 
| @@ -13,5 +13,5 @@ | ||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 |  class UnexpectedResponseException extends \UnexpectedValueException { | 
| 16 | - protected $code = 3; | |
| 16 | + protected $code = 3; | |
| 17 | 17 | } | 
| 18 | 18 | \ No newline at end of file | 
| @@ -13,5 +13,5 @@ | ||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 |  class InvalidArgumentException extends \LogicException{ | 
| 16 | - protected $code = 1; | |
| 16 | + protected $code = 1; | |
| 17 | 17 | } | 
| 18 | 18 | \ No newline at end of file | 
| @@ -12,6 +12,6 @@ | ||
| 12 | 12 | namespace MultiHttp\Exception; | 
| 13 | 13 | |
| 14 | 14 | |
| 15 | -class InvalidArgumentException extends \LogicException{ | |
| 15 | +class InvalidArgumentException extends \LogicException { | |
| 16 | 16 | protected $code = 1; | 
| 17 | 17 | } | 
| 18 | 18 | \ No newline at end of file | 
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | |
| 40 | 40 | abstract function put($uri, array $payload = array(), array $options = array()); | 
| 41 | 41 | |
| 42 | - abstract function get($uri, array $options = array()); | |
| 42 | + abstract function get($uri, array $options = array()); | |
| 43 | 43 | |
| 44 | 44 | abstract function head($uri, array $options = array()); | 
| 45 | 45 | |
| @@ -14,44 +14,44 @@ | ||
| 14 | 14 | |
| 15 | 15 | abstract class Http | 
| 16 | 16 |  { | 
| 17 | - const HEAD = 'HEAD'; | |
| 18 | - const GET = 'GET'; | |
| 19 | - const POST = 'POST'; | |
| 20 | - const PUT = 'PUT'; | |
| 21 | - const DELETE = 'DELETE'; | |
| 22 | - const PATCH = 'PATCH'; | |
| 23 | - const OPTIONS = 'OPTIONS'; | |
| 24 | - const TRACE = 'TRACE'; | |
| 25 | - public static $methods = array( | |
| 26 | - 'HEAD' => self::HEAD, | |
| 27 | - 'GET' => self::GET, | |
| 28 | - 'POST' => self::POST, | |
| 29 | - 'PUT' => self::PUT, | |
| 30 | - 'DELETE' => self::DELETE, | |
| 31 | - 'PATCH' => self::PATCH, | |
| 32 | - 'OPTIONS' => self::OPTIONS, | |
| 33 | - 'TRACE' => self::TRACE, | |
| 34 | - ); | |
| 17 | + const HEAD = 'HEAD'; | |
| 18 | + const GET = 'GET'; | |
| 19 | + const POST = 'POST'; | |
| 20 | + const PUT = 'PUT'; | |
| 21 | + const DELETE = 'DELETE'; | |
| 22 | + const PATCH = 'PATCH'; | |
| 23 | + const OPTIONS = 'OPTIONS'; | |
| 24 | + const TRACE = 'TRACE'; | |
| 25 | + public static $methods = array( | |
| 26 | + 'HEAD' => self::HEAD, | |
| 27 | + 'GET' => self::GET, | |
| 28 | + 'POST' => self::POST, | |
| 29 | + 'PUT' => self::PUT, | |
| 30 | + 'DELETE' => self::DELETE, | |
| 31 | + 'PATCH' => self::PATCH, | |
| 32 | + 'OPTIONS' => self::OPTIONS, | |
| 33 | + 'TRACE' => self::TRACE, | |
| 34 | + ); | |
| 35 | 35 | |
| 36 | - abstract function post($uri, array $payload = array(), array $options = array()); | |
| 36 | + abstract function post($uri, array $payload = array(), array $options = array()); | |
| 37 | 37 | |
| 38 | - abstract function patch($uri, array $payload = array(), array $options = array()); | |
| 38 | + abstract function patch($uri, array $payload = array(), array $options = array()); | |
| 39 | 39 | |
| 40 | - abstract function put($uri, array $payload = array(), array $options = array()); | |
| 40 | + abstract function put($uri, array $payload = array(), array $options = array()); | |
| 41 | 41 | |
| 42 | - abstract function get($uri, array $options = array()); | |
| 42 | + abstract function get($uri, array $options = array()); | |
| 43 | 43 | |
| 44 | - abstract function head($uri, array $options = array()); | |
| 44 | + abstract function head($uri, array $options = array()); | |
| 45 | 45 | |
| 46 | - abstract function delete($uri, array $options = array()); | |
| 46 | + abstract function delete($uri, array $options = array()); | |
| 47 | 47 | |
| 48 | - abstract function options($uri, array $options = array()); | |
| 48 | + abstract function options($uri, array $options = array()); | |
| 49 | 49 | |
| 50 | - abstract function trace($uri, array $options = array()); | |
| 50 | + abstract function trace($uri, array $options = array()); | |
| 51 | 51 | |
| 52 | - public static function isHaveBody() | |
| 53 | -    { | |
| 54 | - return array(self::POST, self::PUT, self::PATCH); | |
| 55 | - } | |
| 52 | + public static function isHaveBody() | |
| 53 | +	{ | |
| 54 | + return array(self::POST, self::PUT, self::PATCH); | |
| 55 | + } | |
| 56 | 56 | |
| 57 | 57 | } | 
| 58 | 58 | \ No newline at end of file | 
| @@ -15,80 +15,80 @@ | ||
| 15 | 15 | |
| 16 | 16 | class Response | 
| 17 | 17 |  { | 
| 18 | - public | |
| 19 | - $code, | |
| 20 | - $errorCode, | |
| 21 | - $error, | |
| 22 | - $header, | |
| 23 | - $body, | |
| 24 | - /** | |
| 25 | - * @var Request | |
| 26 | - */ | |
| 27 | - $request, | |
| 28 | - $contentType, | |
| 29 | - $charset, | |
| 30 | - $duration, | |
| 31 | - $info; | |
| 32 | - protected function __construct() | |
| 33 | -    { | |
| 34 | - } | |
| 18 | + public | |
| 19 | + $code, | |
| 20 | + $errorCode, | |
| 21 | + $error, | |
| 22 | + $header, | |
| 23 | + $body, | |
| 24 | + /** | |
| 25 | + * @var Request | |
| 26 | + */ | |
| 27 | + $request, | |
| 28 | + $contentType, | |
| 29 | + $charset, | |
| 30 | + $duration, | |
| 31 | + $info; | |
| 32 | + protected function __construct() | |
| 33 | +	{ | |
| 34 | + } | |
| 35 | 35 | |
| 36 | -    public static function create(Request $request, $body, $info, $errorCode, $error){ | |
| 37 | - $self = new self; | |
| 38 | - $self->request = $request; | |
| 39 | - $self->body = $body; | |
| 40 | - $self->info = $info; | |
| 41 | - $self->errorCode = $errorCode; | |
| 42 | - $self->error = $error; | |
| 43 | - $self->parse(); | |
| 44 | - $self->check(); | |
| 45 | - return $self; | |
| 46 | - } | |
| 47 | -    public function check(){ | |
| 36 | +	public static function create(Request $request, $body, $info, $errorCode, $error){ | |
| 37 | + $self = new self; | |
| 38 | + $self->request = $request; | |
| 39 | + $self->body = $body; | |
| 40 | + $self->info = $info; | |
| 41 | + $self->errorCode = $errorCode; | |
| 42 | + $self->error = $error; | |
| 43 | + $self->parse(); | |
| 44 | + $self->check(); | |
| 45 | + return $self; | |
| 46 | + } | |
| 47 | +	public function check(){ | |
| 48 | 48 | |
| 49 | - } | |
| 50 | -    public function parse(){ | |
| 51 | -        if($this->body && $this->request->getIni('header')){//has header | |
| 52 | - $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); | |
| 53 | - $this->body = substr($this->body, $this->info['header_size']); | |
| 54 | - $headers = explode(PHP_EOL, $headers); | |
| 55 | - array_shift($headers); // HTTP HEADER | |
| 56 | -            foreach($headers as $h) { | |
| 57 | - if(false !== strpos($h, ':')) | |
| 58 | -                    list($k, $v) = explode(':', $h, 2); | |
| 59 | - else | |
| 60 | - list($k, $v) = array($h,''); | |
| 49 | + } | |
| 50 | +	public function parse(){ | |
| 51 | +		if($this->body && $this->request->getIni('header')){//has header | |
| 52 | + $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); | |
| 53 | + $this->body = substr($this->body, $this->info['header_size']); | |
| 54 | + $headers = explode(PHP_EOL, $headers); | |
| 55 | + array_shift($headers); // HTTP HEADER | |
| 56 | +			foreach($headers as $h) { | |
| 57 | + if(false !== strpos($h, ':')) | |
| 58 | +					list($k, $v) = explode(':', $h, 2); | |
| 59 | + else | |
| 60 | + list($k, $v) = array($h,''); | |
| 61 | 61 | |
| 62 | - $this->header[trim($k)] = trim($v); | |
| 63 | - } | |
| 64 | - } | |
| 65 | - $this->code = $this->info['http_code']; | |
| 66 | - $this->duration = $this->info['total_time']; | |
| 67 | - $this->contentType = $this->info['content_type']; | |
| 68 | - $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; | |
| 69 | -        $content_type = explode(';', $content_type); | |
| 70 | - $this->contentType = $content_type[0]; | |
| 71 | -        if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { | |
| 72 | -            list( , $this->charset) = explode('=', $content_type[1]); | |
| 73 | - } | |
| 74 | - } | |
| 62 | + $this->header[trim($k)] = trim($v); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + $this->code = $this->info['http_code']; | |
| 66 | + $this->duration = $this->info['total_time']; | |
| 67 | + $this->contentType = $this->info['content_type']; | |
| 68 | + $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; | |
| 69 | +		$content_type = explode(';', $content_type); | |
| 70 | + $this->contentType = $content_type[0]; | |
| 71 | +		if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { | |
| 72 | +			list( , $this->charset) = explode('=', $content_type[1]); | |
| 73 | + } | |
| 74 | + } | |
| 75 | 75 | |
| 76 | - /** | |
| 77 | - * Status Code Definitions | |
| 78 | - * | |
| 79 | - * Informational 1xx | |
| 80 | - * Successful 2xx | |
| 81 | - * Redirection 3xx | |
| 82 | - * Client Error 4xx | |
| 83 | - * Server Error 5xx | |
| 84 | - * | |
| 85 | - * http://pretty-rfc.herokuapp.com/RFC2616#status.codes | |
| 86 | - * | |
| 87 | - * @return bool Did we receive a 4xx or 5xx? | |
| 88 | - */ | |
| 89 | - public function hasErrors() | |
| 90 | -    { | |
| 91 | - return $this->code == 0 || $this->code >= 400; | |
| 92 | - } | |
| 76 | + /** | |
| 77 | + * Status Code Definitions | |
| 78 | + * | |
| 79 | + * Informational 1xx | |
| 80 | + * Successful 2xx | |
| 81 | + * Redirection 3xx | |
| 82 | + * Client Error 4xx | |
| 83 | + * Server Error 5xx | |
| 84 | + * | |
| 85 | + * http://pretty-rfc.herokuapp.com/RFC2616#status.codes | |
| 86 | + * | |
| 87 | + * @return bool Did we receive a 4xx or 5xx? | |
| 88 | + */ | |
| 89 | + public function hasErrors() | |
| 90 | +	{ | |
| 91 | + return $this->code == 0 || $this->code >= 400; | |
| 92 | + } | |
| 93 | 93 | |
| 94 | 94 | } | 
| 95 | 95 | \ No newline at end of file | 
| @@ -33,7 +33,7 @@ discard block | ||
| 33 | 33 |      { | 
| 34 | 34 | } | 
| 35 | 35 | |
| 36 | -    public static function create(Request $request, $body, $info, $errorCode, $error){ | |
| 36 | +    public static function create(Request $request, $body, $info, $errorCode, $error) { | |
| 37 | 37 | $self = new self; | 
| 38 | 38 | $self->request = $request; | 
| 39 | 39 | $self->body = $body; | 
| @@ -44,20 +44,20 @@ discard block | ||
| 44 | 44 | $self->check(); | 
| 45 | 45 | return $self; | 
| 46 | 46 | } | 
| 47 | -    public function check(){ | |
| 47 | +    public function check() { | |
| 48 | 48 | |
| 49 | 49 | } | 
| 50 | -    public function parse(){ | |
| 51 | -        if($this->body && $this->request->getIni('header')){//has header | |
| 50 | +    public function parse() { | |
| 51 | +        if ($this->body && $this->request->getIni('header')) {//has header | |
| 52 | 52 | $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); | 
| 53 | 53 | $this->body = substr($this->body, $this->info['header_size']); | 
| 54 | 54 | $headers = explode(PHP_EOL, $headers); | 
| 55 | 55 | array_shift($headers); // HTTP HEADER | 
| 56 | -            foreach($headers as $h) { | |
| 57 | - if(false !== strpos($h, ':')) | |
| 56 | +            foreach ($headers as $h) { | |
| 57 | + if (false !== strpos($h, ':')) | |
| 58 | 58 |                      list($k, $v) = explode(':', $h, 2); | 
| 59 | 59 | else | 
| 60 | - list($k, $v) = array($h,''); | |
| 60 | + list($k, $v) = array($h, ''); | |
| 61 | 61 | |
| 62 | 62 | $this->header[trim($k)] = trim($v); | 
| 63 | 63 | } | 
| @@ -69,7 +69,7 @@ discard block | ||
| 69 | 69 |          $content_type = explode(';', $content_type); | 
| 70 | 70 | $this->contentType = $content_type[0]; | 
| 71 | 71 |          if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { | 
| 72 | -            list( , $this->charset) = explode('=', $content_type[1]); | |
| 72 | +            list(, $this->charset) = explode('=', $content_type[1]); | |
| 73 | 73 | } | 
| 74 | 74 | } | 
| 75 | 75 | |
| @@ -17,7 +17,7 @@ discard block | ||
| 17 | 17 |  class Request extends Http { | 
| 18 | 18 | protected static $curlAlias = array( | 
| 19 | 19 | 'url' => 'CURLOPT_URL', | 
| 20 | - 'debug' => 'CURLOPT_VERBOSE',//for debug verbose | |
| 20 | + 'debug' => 'CURLOPT_VERBOSE', //for debug verbose | |
| 21 | 21 | 'method' => 'CURLOPT_CUSTOMREQUEST', | 
| 22 | 22 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' | 
| 23 | 23 | 'ua' => 'CURLOPT_USERAGENT', | 
| @@ -85,11 +85,11 @@ discard block | ||
| 85 | 85 |  		if (null === ($rawField = constant($alias))) {throw new InvalidArgumentException('field is invalid'); | 
| 86 | 86 | } | 
| 87 | 87 | |
| 88 | - return isset($this->options[$rawField])?$this->options[$rawField]:false; | |
| 88 | + return isset($this->options[$rawField]) ? $this->options[$rawField] : false; | |
| 89 | 89 | } | 
| 90 | 90 | |
| 91 | 91 |  	public function hasInitialized() { | 
| 92 | - return $this->curlHandle?true:false; | |
| 92 | + return $this->curlHandle ? true : false; | |
| 93 | 93 | } | 
| 94 | 94 | |
| 95 | 95 |  	public function addQuery($data) { | 
| @@ -122,10 +122,10 @@ discard block | ||
| 122 | 122 | } | 
| 123 | 123 | |
| 124 | 124 |  		if (isset($this->options['data'])) { | 
| 125 | - $this->options['data'] = is_array($this->options['data'])?http_build_query($this->options['data']):$this->options['data'];//for better compatibility | |
| 125 | + $this->options['data'] = is_array($this->options['data']) ? http_build_query($this->options['data']) : $this->options['data']; //for better compatibility | |
| 126 | 126 | } | 
| 127 | 127 |  		if (isset($this->withURIQuery)) { | 
| 128 | - $this->options['url'] .= strpos($this->options['url'], '?') === FALSE?'?':'&'; | |
| 128 | + $this->options['url'] .= strpos($this->options['url'], '?') === FALSE ? '?' : '&'; | |
| 129 | 129 | $this->options['url'] .= $this->withURIQuery; | 
| 130 | 130 | } | 
| 131 | 131 |  		if (isset($this->options['callback'])) { | 
| @@ -254,7 +254,7 @@ discard block | ||
| 254 | 254 | return $alias; | 
| 255 | 255 | } | 
| 256 | 256 |  	public function makeResponse($isMultiCurl = false) { | 
| 257 | - $body = $isMultiCurl?curl_multi_getcontent($this->curlHandle):curl_exec($this->curlHandle); | |
| 257 | + $body = $isMultiCurl ? curl_multi_getcontent($this->curlHandle) : curl_exec($this->curlHandle); | |
| 258 | 258 | $info = curl_getinfo($this->curlHandle); | 
| 259 | 259 | $errno = curl_errno($this->curlHandle); | 
| 260 | 260 | $error = curl_error($this->curlHandle); | 
| @@ -35,15 +35,15 @@ discard block | ||
| 35 | 35 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , | 
| 36 | 36 | ); | 
| 37 | 37 | public $curlHandle; | 
| 38 | - protected $options = array( | |
| 39 | - 'CURLOPT_MAXREDIRS' => 10, | |
| 38 | + protected $options = array( | |
| 39 | + 'CURLOPT_MAXREDIRS' => 10, | |
| 40 | 40 | // 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//IPv4 | 
| 41 | - 'header' => true, | |
| 42 | - 'method' => self::GET, | |
| 43 | - 'transfer' => true, | |
| 44 | - 'follow_location' => true, | |
| 45 | - 'timeout' => 0); | |
| 46 | - protected $endCallback; | |
| 41 | + 'header' => true, | |
| 42 | + 'method' => self::GET, | |
| 43 | + 'transfer' => true, | |
| 44 | + 'follow_location' => true, | |
| 45 | + 'timeout' => 0); | |
| 46 | + protected $endCallback; | |
| 47 | 47 | protected $withURIQuery; | 
| 48 | 48 | |
| 49 | 49 |  	protected function __construct() { | 
| @@ -222,7 +222,7 @@ discard block | ||
| 222 | 222 | |
| 223 | 223 | self::filterAndRaw($this->options); | 
| 224 | 224 | |
| 225 | - curl_setopt_array($this->curlHandle, $this->options); | |
| 225 | + curl_setopt_array($this->curlHandle, $this->options); | |
| 226 | 226 | |
| 227 | 227 | return $this; | 
| 228 | 228 | } | 
| @@ -70,9 +70,9 @@ discard block | ||
| 70 | 70 | */ | 
| 71 | 71 |  	public function execute() { | 
| 72 | 72 | $sleepTime = 1000;//microsecond, prevent CPU 100% | 
| 73 | - //prepare conf | |
| 73 | + //prepare conf | |
| 74 | 74 | // while (($multiFlg = curl_multi_exec(self::$multiHandler, $active)) == CURLM_CALL_MULTI_PERFORM); | 
| 75 | - //fetch data | |
| 75 | + //fetch data | |
| 76 | 76 |  //        while ($active && $multiFlg == CURLM_OK) { | 
| 77 | 77 |  //            if (curl_multi_select(self::$multiHandler) != -1) { | 
| 78 | 78 | // while (curl_multi_exec(self::$multiHandler, $active) === CURLM_CALL_MULTI_PERFORM); | 
| @@ -83,25 +83,25 @@ discard block | ||
| 83 | 83 | // } | 
| 84 | 84 | // } | 
| 85 | 85 | |
| 86 | -        do{ | |
| 87 | - curl_multi_exec(self::$multiHandler, $active); | |
| 88 | - // bug in PHP 5.3.18+ where curl_multi_select can return -1 | |
| 86 | +		do{ | |
| 87 | + curl_multi_exec(self::$multiHandler, $active); | |
| 88 | + // bug in PHP 5.3.18+ where curl_multi_select can return -1 | |
| 89 | 89 | // https://bugs.php.net/bug.php?id=63411 | 
| 90 | -            if (curl_multi_select(self::$multiHandler) == -1) { | |
| 91 | - usleep($sleepTime); | |
| 92 | - } | |
| 93 | - usleep($sleepTime); | |
| 94 | - }while($active); | |
| 95 | - $return = array(); | |
| 90 | +			if (curl_multi_select(self::$multiHandler) == -1) { | |
| 91 | + usleep($sleepTime); | |
| 92 | + } | |
| 93 | + usleep($sleepTime); | |
| 94 | + }while($active); | |
| 95 | + $return = array(); | |
| 96 | 96 |  		foreach (self::$requestPool as $request) { | 
| 97 | 97 | $response = $request->makeResponse(true); | 
| 98 | - $func = $response->request->endCallback(); | |
| 98 | + $func = $response->request->endCallback(); | |
| 99 | 99 |  			if (isset($func)) { | 
| 100 | 100 | $func($response); | 
| 101 | 101 | } | 
| 102 | 102 | $return[] = $response; | 
| 103 | - curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); | |
| 104 | - curl_close($request->curlHandle); | |
| 103 | + curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); | |
| 104 | + curl_close($request->curlHandle); | |
| 105 | 105 | } | 
| 106 | 106 | curl_multi_close(self::$multiHandler); | 
| 107 | 107 | return $return; | 
| @@ -69,7 +69,7 @@ discard block | ||
| 69 | 69 | * @return array(Response) | 
| 70 | 70 | */ | 
| 71 | 71 |  	public function execute() { | 
| 72 | - $sleepTime = 1000;//microsecond, prevent CPU 100% | |
| 72 | + $sleepTime = 1000; //microsecond, prevent CPU 100% | |
| 73 | 73 | //prepare conf | 
| 74 | 74 | // while (($multiFlg = curl_multi_exec(self::$multiHandler, $active)) == CURLM_CALL_MULTI_PERFORM); | 
| 75 | 75 | //fetch data | 
| @@ -83,7 +83,7 @@ discard block | ||
| 83 | 83 | // } | 
| 84 | 84 | // } | 
| 85 | 85 | |
| 86 | -        do{ | |
| 86 | +        do { | |
| 87 | 87 | curl_multi_exec(self::$multiHandler, $active); | 
| 88 | 88 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 | 
| 89 | 89 | // https://bugs.php.net/bug.php?id=63411 | 
| @@ -91,7 +91,7 @@ discard block | ||
| 91 | 91 | usleep($sleepTime); | 
| 92 | 92 | } | 
| 93 | 93 | usleep($sleepTime); | 
| 94 | - }while($active); | |
| 94 | + }while ($active); | |
| 95 | 95 | $return = array(); | 
| 96 | 96 |  		foreach (self::$requestPool as $request) { | 
| 97 | 97 | $response = $request->makeResponse(true); |