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