[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/http/src/ -> Http.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Http Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Http;
  10  
  11  use Joomla\Uri\Uri;
  12  use Joomla\Uri\UriInterface;
  13  use Psr\Http\Client\ClientInterface;
  14  use Psr\Http\Message\RequestInterface;
  15  use Psr\Http\Message\ResponseInterface;
  16  
  17  /**
  18   * HTTP client class.
  19   *
  20   * @since  1.0
  21   */
  22  class Http implements ClientInterface
  23  {
  24      /**
  25       * Options for the HTTP client.
  26       *
  27       * @var    array|\ArrayAccess
  28       * @since  1.0
  29       */
  30      protected $options;
  31  
  32      /**
  33       * The HTTP transport object to use in sending HTTP requests.
  34       *
  35       * @var    TransportInterface
  36       * @since  1.0
  37       */
  38      protected $transport;
  39  
  40      /**
  41       * Constructor.
  42       *
  43       * @param   array|\ArrayAccess  $options    Client options array. If the registry contains any headers.* elements,
  44       *                                          these will be added to the request headers.
  45       * @param   TransportInterface  $transport  The HTTP transport object.
  46       *
  47       * @since   1.0
  48       * @throws  \InvalidArgumentException
  49       */
  50  	public function __construct($options = [], TransportInterface $transport = null)
  51      {
  52          if (!\is_array($options) && !($options instanceof \ArrayAccess))
  53          {
  54              throw new \InvalidArgumentException(
  55                  'The options param must be an array or implement the ArrayAccess interface.'
  56              );
  57          }
  58  
  59          $this->options = $options;
  60  
  61          if (!$transport)
  62          {
  63              $transport = (new HttpFactory)->getAvailableDriver($this->options);
  64  
  65              // Ensure the transport is a TransportInterface instance or bail out
  66              if (!($transport instanceof TransportInterface))
  67              {
  68                  throw new \InvalidArgumentException(sprintf('A valid %s object was not set.', TransportInterface::class));
  69              }
  70          }
  71  
  72          $this->transport = $transport;
  73      }
  74  
  75      /**
  76       * Get an option from the HTTP client.
  77       *
  78       * @param   string  $key      The name of the option to get.
  79       * @param   mixed   $default  The default value if the option is not set.
  80       *
  81       * @return  mixed  The option value.
  82       *
  83       * @since   1.0
  84       */
  85  	public function getOption($key, $default = null)
  86      {
  87          return $this->options[$key] ?? $default;
  88      }
  89  
  90      /**
  91       * Set an option for the HTTP client.
  92       *
  93       * @param   string  $key    The name of the option to set.
  94       * @param   mixed   $value  The option value to set.
  95       *
  96       * @return  $this
  97       *
  98       * @since   1.0
  99       */
 100  	public function setOption($key, $value)
 101      {
 102          $this->options[$key] = $value;
 103  
 104          return $this;
 105      }
 106  
 107      /**
 108       * Method to send the OPTIONS command to the server.
 109       *
 110       * @param   string|UriInterface  $url      The URI to the resource to request.
 111       * @param   array                $headers  An array of request headers to send with the request.
 112       * @param   integer              $timeout  Read timeout in seconds.
 113       *
 114       * @return  Response
 115       *
 116       * @since   1.0
 117       */
 118  	public function options($url, array $headers = [], $timeout = null)
 119      {
 120          return $this->makeTransportRequest('OPTIONS', $url, null, $headers, $timeout);
 121      }
 122  
 123      /**
 124       * Method to send the HEAD command to the server.
 125       *
 126       * @param   string|UriInterface  $url      The URI to the resource to request.
 127       * @param   array                $headers  An array of request headers to send with the request.
 128       * @param   integer              $timeout  Read timeout in seconds.
 129       *
 130       * @return  Response
 131       *
 132       * @since   1.0
 133       */
 134  	public function head($url, array $headers = [], $timeout = null)
 135      {
 136          return $this->makeTransportRequest('HEAD', $url, null, $headers, $timeout);
 137      }
 138  
 139      /**
 140       * Method to send the GET command to the server.
 141       *
 142       * @param   string|UriInterface  $url      The URI to the resource to request.
 143       * @param   array                $headers  An array of request headers to send with the request.
 144       * @param   integer              $timeout  Read timeout in seconds.
 145       *
 146       * @return  Response
 147       *
 148       * @since   1.0
 149       */
 150  	public function get($url, array $headers = [], $timeout = null)
 151      {
 152          return $this->makeTransportRequest('GET', $url, null, $headers, $timeout);
 153      }
 154  
 155      /**
 156       * Method to send the POST command to the server.
 157       *
 158       * @param   string|UriInterface  $url      The URI to the resource to request.
 159       * @param   mixed                $data     Either an associative array or a string to be sent with the request.
 160       * @param   array                $headers  An array of request headers to send with the request.
 161       * @param   integer              $timeout  Read timeout in seconds.
 162       *
 163       * @return  Response
 164       *
 165       * @since   1.0
 166       */
 167  	public function post($url, $data, array $headers = [], $timeout = null)
 168      {
 169          return $this->makeTransportRequest('POST', $url, $data, $headers, $timeout);
 170      }
 171  
 172      /**
 173       * Method to send the PUT command to the server.
 174       *
 175       * @param   string|UriInterface  $url      The URI to the resource to request.
 176       * @param   mixed                $data     Either an associative array or a string to be sent with the request.
 177       * @param   array                $headers  An array of request headers to send with the request.
 178       * @param   integer              $timeout  Read timeout in seconds.
 179       *
 180       * @return  Response
 181       *
 182       * @since   1.0
 183       */
 184  	public function put($url, $data, array $headers = [], $timeout = null)
 185      {
 186          return $this->makeTransportRequest('PUT', $url, $data, $headers, $timeout);
 187      }
 188  
 189      /**
 190       * Method to send the DELETE command to the server.
 191       *
 192       * @param   string|UriInterface  $url      The URI to the resource to request.
 193       * @param   array                $headers  An array of request headers to send with the request.
 194       * @param   integer              $timeout  Read timeout in seconds.
 195       * @param   mixed                $data     Either an associative array or a string to be sent with the request.
 196       *
 197       * @return  Response
 198       *
 199       * @since   1.0
 200       */
 201  	public function delete($url, array $headers = [], $timeout = null, $data = null)
 202      {
 203          return $this->makeTransportRequest('DELETE', $url, $data, $headers, $timeout);
 204      }
 205  
 206      /**
 207       * Method to send the TRACE command to the server.
 208       *
 209       * @param   string|UriInterface  $url      The URI to the resource to request.
 210       * @param   array                $headers  An array of request headers to send with the request.
 211       * @param   integer              $timeout  Read timeout in seconds.
 212       *
 213       * @return  Response
 214       *
 215       * @since   1.0
 216       */
 217  	public function trace($url, array $headers = [], $timeout = null)
 218      {
 219          return $this->makeTransportRequest('TRACE', $url, null, $headers, $timeout);
 220      }
 221  
 222      /**
 223       * Method to send the PATCH command to the server.
 224       *
 225       * @param   string|UriInterface  $url      The URI to the resource to request.
 226       * @param   mixed                $data     Either an associative array or a string to be sent with the request.
 227       * @param   array                $headers  An array of request headers to send with the request.
 228       * @param   integer              $timeout  Read timeout in seconds.
 229       *
 230       * @return  Response
 231       *
 232       * @since   1.0
 233       */
 234  	public function patch($url, $data, array $headers = [], $timeout = null)
 235      {
 236          return $this->makeTransportRequest('PATCH', $url, $data, $headers, $timeout);
 237      }
 238  
 239      /**
 240       * Sends a PSR-7 request and returns a PSR-7 response.
 241       *
 242       * @param   RequestInterface  $request  The PSR-7 request object.
 243       *
 244       * @return  ResponseInterface|Response
 245       *
 246       * @since   2.0.0
 247       */
 248  	public function sendRequest(RequestInterface $request): ResponseInterface
 249      {
 250          $data = $request->getBody()->getContents();
 251  
 252          return $this->makeTransportRequest(
 253              $request->getMethod(),
 254              new Uri((string) $request->getUri()),
 255              empty($data) ? null : $data,
 256              $request->getHeaders()
 257          );
 258      }
 259  
 260      /**
 261       * Send a request to the server and return a Response object with the response.
 262       *
 263       * @param   string               $method   The HTTP method for sending the request.
 264       * @param   string|UriInterface  $url      The URI to the resource to request.
 265       * @param   mixed                $data     Either an associative array or a string to be sent with the request.
 266       * @param   array                $headers  An array of request headers to send with the request.
 267       * @param   integer              $timeout  Read timeout in seconds.
 268       *
 269       * @return  Response
 270       *
 271       * @since   1.0
 272       * @throws  \InvalidArgumentException
 273       */
 274  	protected function makeTransportRequest($method, $url, $data = null, array $headers = [], $timeout = null)
 275      {
 276          // Look for headers set in the options.
 277          if (isset($this->options['headers']))
 278          {
 279              $temp = (array) $this->options['headers'];
 280  
 281              foreach ($temp as $key => $val)
 282              {
 283                  if (!isset($headers[$key]))
 284                  {
 285                      $headers[$key] = $val;
 286                  }
 287              }
 288          }
 289  
 290          // Look for timeout set in the options.
 291          if ($timeout === null && isset($this->options['timeout']))
 292          {
 293              $timeout = $this->options['timeout'];
 294          }
 295  
 296          $userAgent = isset($this->options['userAgent']) ? $this->options['userAgent'] : null;
 297  
 298          // Convert to a Uri object if we were given a string
 299          if (\is_string($url))
 300          {
 301              $url = new Uri($url);
 302          }
 303          elseif (!($url instanceof UriInterface))
 304          {
 305              throw new \InvalidArgumentException(
 306                  sprintf(
 307                      'A string or %s object must be provided, a "%s" was provided.',
 308                      UriInterface::class,
 309                      \gettype($url)
 310                  )
 311              );
 312          }
 313  
 314          return $this->transport->request($method, $url, $data, $headers, $timeout, $userAgent);
 315      }
 316  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer