| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Cookie\CookieJar;
|
|---|
| 5 | use GuzzleHttp\Promise;
|
|---|
| 6 | use GuzzleHttp\Psr7;
|
|---|
| 7 | use Psr\Http\Message\UriInterface;
|
|---|
| 8 | use Psr\Http\Message\RequestInterface;
|
|---|
| 9 | use Psr\Http\Message\ResponseInterface;
|
|---|
| 10 | use \InvalidArgumentException as Iae;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * @method ResponseInterface get($uri, array $options = [])
|
|---|
| 14 | * @method ResponseInterface head($uri, array $options = [])
|
|---|
| 15 | * @method ResponseInterface put($uri, array $options = [])
|
|---|
| 16 | * @method ResponseInterface post($uri, array $options = [])
|
|---|
| 17 | * @method ResponseInterface patch($uri, array $options = [])
|
|---|
| 18 | * @method ResponseInterface delete($uri, array $options = [])
|
|---|
| 19 | * @method Promise\PromiseInterface getAsync($uri, array $options = [])
|
|---|
| 20 | * @method Promise\PromiseInterface headAsync($uri, array $options = [])
|
|---|
| 21 | * @method Promise\PromiseInterface putAsync($uri, array $options = [])
|
|---|
| 22 | * @method Promise\PromiseInterface postAsync($uri, array $options = [])
|
|---|
| 23 | * @method Promise\PromiseInterface patchAsync($uri, array $options = [])
|
|---|
| 24 | * @method Promise\PromiseInterface deleteAsync($uri, array $options = [])
|
|---|
| 25 | */
|
|---|
| 26 | class Client implements ClientInterface
|
|---|
| 27 | {
|
|---|
| 28 | /** @var array Default request options */
|
|---|
| 29 | private $config;
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Clients accept an array of constructor parameters.
|
|---|
| 33 | *
|
|---|
| 34 | * Here's an example of creating a client using a base_uri and an array of
|
|---|
| 35 | * default request options to apply to each request:
|
|---|
| 36 | *
|
|---|
| 37 | * $client = new Client([
|
|---|
| 38 | * 'base_uri' => 'http://www.foo.com/1.0/',
|
|---|
| 39 | * 'timeout' => 0,
|
|---|
| 40 | * 'allow_redirects' => false,
|
|---|
| 41 | * 'proxy' => '192.168.16.1:10'
|
|---|
| 42 | * ]);
|
|---|
| 43 | *
|
|---|
| 44 | * Client configuration settings include the following options:
|
|---|
| 45 | *
|
|---|
| 46 | * - handler: (callable) Function that transfers HTTP requests over the
|
|---|
| 47 | * wire. The function is called with a Psr7\Http\Message\RequestInterface
|
|---|
| 48 | * and array of transfer options, and must return a
|
|---|
| 49 | * GuzzleHttp\Promise\PromiseInterface that is fulfilled with a
|
|---|
| 50 | * Psr7\Http\Message\ResponseInterface on success. "handler" is a
|
|---|
| 51 | * constructor only option that cannot be overridden in per/request
|
|---|
| 52 | * options. If no handler is provided, a default handler will be created
|
|---|
| 53 | * that enables all of the request options below by attaching all of the
|
|---|
| 54 | * default middleware to the handler.
|
|---|
| 55 | * - base_uri: (string|UriInterface) Base URI of the client that is merged
|
|---|
| 56 | * into relative URIs. Can be a string or instance of UriInterface.
|
|---|
| 57 | * - **: any request option
|
|---|
| 58 | *
|
|---|
| 59 | * @param array $config Client configuration settings.
|
|---|
| 60 | *
|
|---|
| 61 | * @see \GuzzleHttp\RequestOptions for a list of available request options.
|
|---|
| 62 | */
|
|---|
| 63 | public function __construct(array $config = [])
|
|---|
| 64 | {
|
|---|
| 65 | if (!isset($config['handler'])) {
|
|---|
| 66 | $config['handler'] = HandlerStack::create();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // Convert the base_uri to a UriInterface
|
|---|
| 70 | if (isset($config['base_uri'])) {
|
|---|
| 71 | $config['base_uri'] = Psr7\uri_for($config['base_uri']);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | $this->configureDefaults($config);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public function __call($method, $args)
|
|---|
| 78 | {
|
|---|
| 79 | if (count($args) < 1) {
|
|---|
| 80 | throw new \InvalidArgumentException('Magic request methods require a URI and optional options array');
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | $uri = $args[0];
|
|---|
| 84 | $opts = isset($args[1]) ? $args[1] : [];
|
|---|
| 85 |
|
|---|
| 86 | return substr($method, -5) === 'Async'
|
|---|
| 87 | ? $this->requestAsync(substr($method, 0, -5), $uri, $opts)
|
|---|
| 88 | : $this->request($method, $uri, $opts);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public function sendAsync(RequestInterface $request, array $options = [])
|
|---|
| 92 | {
|
|---|
| 93 | // Merge the base URI into the request URI if needed.
|
|---|
| 94 | $options = $this->prepareDefaults($options);
|
|---|
| 95 |
|
|---|
| 96 | return $this->transfer(
|
|---|
| 97 | $request->withUri($this->buildUri($request->getUri(), $options)),
|
|---|
| 98 | $options
|
|---|
| 99 | );
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public function send(RequestInterface $request, array $options = [])
|
|---|
| 103 | {
|
|---|
| 104 | $options[RequestOptions::SYNCHRONOUS] = true;
|
|---|
| 105 | return $this->sendAsync($request, $options)->wait();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | public function requestAsync($method, $uri = null, array $options = [])
|
|---|
| 109 | {
|
|---|
| 110 | $options = $this->prepareDefaults($options);
|
|---|
| 111 | // Remove request modifying parameter because it can be done up-front.
|
|---|
| 112 | $headers = isset($options['headers']) ? $options['headers'] : [];
|
|---|
| 113 | $body = isset($options['body']) ? $options['body'] : null;
|
|---|
| 114 | $version = isset($options['version']) ? $options['version'] : '1.1';
|
|---|
| 115 | // Merge the URI into the base URI.
|
|---|
| 116 | $uri = $this->buildUri($uri, $options);
|
|---|
| 117 | if (is_array($body)) {
|
|---|
| 118 | $this->invalidBody();
|
|---|
| 119 | }
|
|---|
| 120 | $request = new Psr7\Request($method, $uri, $headers, $body, $version);
|
|---|
| 121 | // Remove the option so that they are not doubly-applied.
|
|---|
| 122 | unset($options['headers'], $options['body'], $options['version']);
|
|---|
| 123 |
|
|---|
| 124 | return $this->transfer($request, $options);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public function request($method, $uri = null, array $options = [])
|
|---|
| 128 | {
|
|---|
| 129 | $options[RequestOptions::SYNCHRONOUS] = true;
|
|---|
| 130 | return $this->requestAsync($method, $uri, $options)->wait();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | public function getConfig($option = null)
|
|---|
| 134 | {
|
|---|
| 135 | return $option === null
|
|---|
| 136 | ? $this->config
|
|---|
| 137 | : (isset($this->config[$option]) ? $this->config[$option] : null);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | private function buildUri($uri, array $config)
|
|---|
| 141 | {
|
|---|
| 142 | if (!isset($config['base_uri'])) {
|
|---|
| 143 | return $uri instanceof UriInterface ? $uri : new Psr7\Uri($uri);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | return Psr7\Uri::resolve(Psr7\uri_for($config['base_uri']), $uri);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Configures the default options for a client.
|
|---|
| 151 | *
|
|---|
| 152 | * @param array $config
|
|---|
| 153 | *
|
|---|
| 154 | * @return array
|
|---|
| 155 | */
|
|---|
| 156 | private function configureDefaults(array $config)
|
|---|
| 157 | {
|
|---|
| 158 | $defaults = [
|
|---|
| 159 | 'allow_redirects' => RedirectMiddleware::$defaultSettings,
|
|---|
| 160 | 'http_errors' => true,
|
|---|
| 161 | 'decode_content' => true,
|
|---|
| 162 | 'verify' => true,
|
|---|
| 163 | 'cookies' => false
|
|---|
| 164 | ];
|
|---|
| 165 |
|
|---|
| 166 | // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set
|
|---|
| 167 | if ($proxy = getenv('HTTP_PROXY')) {
|
|---|
| 168 | $defaults['proxy']['http'] = $proxy;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | if ($proxy = getenv('HTTPS_PROXY')) {
|
|---|
| 172 | $defaults['proxy']['https'] = $proxy;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | $this->config = $config + $defaults;
|
|---|
| 176 |
|
|---|
| 177 | if (!empty($config['cookies']) && $config['cookies'] === true) {
|
|---|
| 178 | $this->config['cookies'] = new CookieJar();
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | // Add the default user-agent header.
|
|---|
| 182 | if (!isset($this->config['headers'])) {
|
|---|
| 183 | $this->config['headers'] = ['User-Agent' => default_user_agent()];
|
|---|
| 184 | } else {
|
|---|
| 185 | // Add the User-Agent header if one was not already set.
|
|---|
| 186 | foreach (array_keys($this->config['headers']) as $name) {
|
|---|
| 187 | if (strtolower($name) === 'user-agent') {
|
|---|
| 188 | return;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | $this->config['headers']['User-Agent'] = default_user_agent();
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Merges default options into the array.
|
|---|
| 197 | *
|
|---|
| 198 | * @param array $options Options to modify by reference
|
|---|
| 199 | *
|
|---|
| 200 | * @return array
|
|---|
| 201 | */
|
|---|
| 202 | private function prepareDefaults($options)
|
|---|
| 203 | {
|
|---|
| 204 | $defaults = $this->config;
|
|---|
| 205 |
|
|---|
| 206 | if (!empty($defaults['headers'])) {
|
|---|
| 207 | // Default headers are only added if they are not present.
|
|---|
| 208 | $defaults['_conditional'] = $defaults['headers'];
|
|---|
| 209 | unset($defaults['headers']);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | // Special handling for headers is required as they are added as
|
|---|
| 213 | // conditional headers and as headers passed to a request ctor.
|
|---|
| 214 | if (array_key_exists('headers', $options)) {
|
|---|
| 215 | // Allows default headers to be unset.
|
|---|
| 216 | if ($options['headers'] === null) {
|
|---|
| 217 | $defaults['_conditional'] = null;
|
|---|
| 218 | unset($options['headers']);
|
|---|
| 219 | } elseif (!is_array($options['headers'])) {
|
|---|
| 220 | throw new \InvalidArgumentException('headers must be an array');
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // Shallow merge defaults underneath options.
|
|---|
| 225 | $result = $options + $defaults;
|
|---|
| 226 |
|
|---|
| 227 | // Remove null values.
|
|---|
| 228 | foreach ($result as $k => $v) {
|
|---|
| 229 | if ($v === null) {
|
|---|
| 230 | unset($result[$k]);
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | return $result;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Transfers the given request and applies request options.
|
|---|
| 239 | *
|
|---|
| 240 | * The URI of the request is not modified and the request options are used
|
|---|
| 241 | * as-is without merging in default options.
|
|---|
| 242 | *
|
|---|
| 243 | * @param RequestInterface $request
|
|---|
| 244 | * @param array $options
|
|---|
| 245 | *
|
|---|
| 246 | * @return Promise\PromiseInterface
|
|---|
| 247 | */
|
|---|
| 248 | private function transfer(RequestInterface $request, array $options)
|
|---|
| 249 | {
|
|---|
| 250 | // save_to -> sink
|
|---|
| 251 | if (isset($options['save_to'])) {
|
|---|
| 252 | $options['sink'] = $options['save_to'];
|
|---|
| 253 | unset($options['save_to']);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | // exceptions -> http_error
|
|---|
| 257 | if (isset($options['exceptions'])) {
|
|---|
| 258 | $options['http_errors'] = $options['exceptions'];
|
|---|
| 259 | unset($options['exceptions']);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | $request = $this->applyOptions($request, $options);
|
|---|
| 263 | $handler = $options['handler'];
|
|---|
| 264 |
|
|---|
| 265 | try {
|
|---|
| 266 | return Promise\promise_for($handler($request, $options));
|
|---|
| 267 | } catch (\Exception $e) {
|
|---|
| 268 | return Promise\rejection_for($e);
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /**
|
|---|
| 273 | * Applies the array of request options to a request.
|
|---|
| 274 | *
|
|---|
| 275 | * @param RequestInterface $request
|
|---|
| 276 | * @param array $options
|
|---|
| 277 | *
|
|---|
| 278 | * @return RequestInterface
|
|---|
| 279 | */
|
|---|
| 280 | private function applyOptions(RequestInterface $request, array &$options)
|
|---|
| 281 | {
|
|---|
| 282 | $modify = [];
|
|---|
| 283 |
|
|---|
| 284 | if (isset($options['form_params'])) {
|
|---|
| 285 | if (isset($options['multipart'])) {
|
|---|
| 286 | throw new \InvalidArgumentException('You cannot use '
|
|---|
| 287 | . 'form_params and multipart at the same time. Use the '
|
|---|
| 288 | . 'form_params option if you want to send application/'
|
|---|
| 289 | . 'x-www-form-urlencoded requests, and the multipart '
|
|---|
| 290 | . 'option to send multipart/form-data requests.');
|
|---|
| 291 | }
|
|---|
| 292 | $options['body'] = http_build_query($options['form_params']);
|
|---|
| 293 | unset($options['form_params']);
|
|---|
| 294 | $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | if (isset($options['multipart'])) {
|
|---|
| 298 | $elements = $options['multipart'];
|
|---|
| 299 | unset($options['multipart']);
|
|---|
| 300 | $options['body'] = new Psr7\MultipartStream($elements);
|
|---|
| 301 | // Use a multipart/form-data POST if a Content-Type is not set.
|
|---|
| 302 | $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary='
|
|---|
| 303 | . $options['body']->getBoundary();
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | if (!empty($options['decode_content'])
|
|---|
| 307 | && $options['decode_content'] !== true
|
|---|
| 308 | ) {
|
|---|
| 309 | $modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | if (isset($options['headers'])) {
|
|---|
| 313 | if (isset($modify['set_headers'])) {
|
|---|
| 314 | $modify['set_headers'] = $options['headers'] + $modify['set_headers'];
|
|---|
| 315 | } else {
|
|---|
| 316 | $modify['set_headers'] = $options['headers'];
|
|---|
| 317 | }
|
|---|
| 318 | unset($options['headers']);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | if (isset($options['body'])) {
|
|---|
| 322 | if (is_array($options['body'])) {
|
|---|
| 323 | $this->invalidBody();
|
|---|
| 324 | }
|
|---|
| 325 | $modify['body'] = Psr7\stream_for($options['body']);
|
|---|
| 326 | unset($options['body']);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | if (!empty($options['auth'])) {
|
|---|
| 330 | $value = $options['auth'];
|
|---|
| 331 | $type = is_array($value)
|
|---|
| 332 | ? (isset($value[2]) ? strtolower($value[2]) : 'basic')
|
|---|
| 333 | : $value;
|
|---|
| 334 | $config['auth'] = $value;
|
|---|
| 335 | switch (strtolower($type)) {
|
|---|
| 336 | case 'basic':
|
|---|
| 337 | $modify['set_headers']['Authorization'] = 'Basic '
|
|---|
| 338 | . base64_encode("$value[0]:$value[1]");
|
|---|
| 339 | break;
|
|---|
| 340 | case 'digest':
|
|---|
| 341 | // @todo: Do not rely on curl
|
|---|
| 342 | $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
|
|---|
| 343 | $options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]";
|
|---|
| 344 | break;
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | if (isset($options['query'])) {
|
|---|
| 349 | $value = $options['query'];
|
|---|
| 350 | if (is_array($value)) {
|
|---|
| 351 | $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
|
|---|
| 352 | }
|
|---|
| 353 | if (!is_string($value)) {
|
|---|
| 354 | throw new Iae('query must be a string or array');
|
|---|
| 355 | }
|
|---|
| 356 | $modify['query'] = $value;
|
|---|
| 357 | unset($options['query']);
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | if (isset($options['json'])) {
|
|---|
| 361 | $modify['body'] = Psr7\stream_for(json_encode($options['json']));
|
|---|
| 362 | $options['_conditional']['Content-Type'] = 'application/json';
|
|---|
| 363 | unset($options['json']);
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | $request = Psr7\modify_request($request, $modify);
|
|---|
| 367 |
|
|---|
| 368 | // Merge in conditional headers if they are not present.
|
|---|
| 369 | if (isset($options['_conditional'])) {
|
|---|
| 370 | // Build up the changes so it's in a single clone of the message.
|
|---|
| 371 | $modify = [];
|
|---|
| 372 | foreach ($options['_conditional'] as $k => $v) {
|
|---|
| 373 | if (!$request->hasHeader($k)) {
|
|---|
| 374 | $modify['set_headers'][$k] = $v;
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 | $request = Psr7\modify_request($request, $modify);
|
|---|
| 378 | // Don't pass this internal value along to middleware/handlers.
|
|---|
| 379 | unset($options['_conditional']);
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | return $request;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | private function invalidBody()
|
|---|
| 386 | {
|
|---|
| 387 | throw new \InvalidArgumentException('Passing in the "body" request '
|
|---|
| 388 | . 'option as an array to send a POST request has been deprecated. '
|
|---|
| 389 | . 'Please use the "form_params" request option to send a '
|
|---|
| 390 | . 'application/x-www-form-urlencoded request, or a the "multipart" '
|
|---|
| 391 | . 'request option to send a multipart/form-data request.');
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|