[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/SimplePie/src/HTTP/ -> FileClient.php (source)

   1  <?php
   2  
   3  // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
   4  // SPDX-License-Identifier: BSD-3-Clause
   5  
   6  declare(strict_types=1);
   7  
   8  namespace SimplePie\HTTP;
   9  
  10  use InvalidArgumentException;
  11  use SimplePie\File;
  12  use SimplePie\Misc;
  13  use SimplePie\Registry;
  14  use Throwable;
  15  
  16  /**
  17   * HTTP Client based on \SimplePie\File
  18   *
  19   * @internal
  20   */
  21  final class FileClient implements Client
  22  {
  23      /** @var Registry */
  24      private $registry;
  25  
  26      /** @var array{timeout?: int, redirects?: int, useragent?: string, force_fsockopen?: bool, curl_options?: array<mixed>} */
  27      private $options;
  28  
  29      /**
  30       * @param array{timeout?: int, redirects?: int, useragent?: string, force_fsockopen?: bool, curl_options?: array<mixed>} $options
  31       */
  32      public function __construct(Registry $registry, array $options = [])
  33      {
  34          $this->registry = $registry;
  35          $this->options = $options;
  36      }
  37  
  38      /**
  39       * send a request and return the response
  40       *
  41       * @param Client::METHOD_* $method
  42       * @param array<string, string> $headers
  43       *
  44       * @throws ClientException if anything goes wrong requesting the data
  45       */
  46      public function request(string $method, string $url, array $headers = []): Response
  47      {
  48          // @phpstan-ignore-next-line Enforce PHPDoc type.
  49          if ($method !== self::METHOD_GET) {
  50              throw new InvalidArgumentException(sprintf(
  51                  '%s(): Argument #1 ($method) only supports method "%s".',
  52                  __METHOD__,
  53                  self::METHOD_GET
  54              ), 1);
  55          }
  56  
  57          try {
  58              $file = $this->registry->create(File::class, [
  59                  $url,
  60                  $this->options['timeout'] ?? 10,
  61                  $this->options['redirects'] ?? 5,
  62                  $headers,
  63                  $this->options['useragent'] ?? Misc::get_default_useragent(),
  64                  $this->options['force_fsockopen'] ?? false,
  65                  $this->options['curl_options'] ?? []
  66              ]);
  67          } catch (Throwable $th) {
  68              throw new ClientException($th->getMessage(), $th->getCode(), $th);
  69          }
  70  
  71          if ($file->error !== null && $file->get_status_code() === 0) {
  72              throw new ClientException($file->error);
  73          }
  74  
  75          return $file;
  76      }
  77  }


Generated : Wed Sep 17 08:20:04 2025 Cross-referenced by PHPXref