[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/maxmind/web-service-common/src/WebService/Http/ -> CurlRequest.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace MaxMind\WebService\Http;
   6  
   7  use MaxMind\Exception\HttpException;
   8  
   9  /**
  10   * This class is for internal use only. Semantic versioning does not not apply.
  11   *
  12   * @internal
  13   */
  14  class CurlRequest implements Request
  15  {
  16      /**
  17       * @var \CurlHandle
  18       */
  19      private $ch;
  20  
  21      /**
  22       * @var string
  23       */
  24      private $url;
  25  
  26      /**
  27       * @var array<string, mixed>
  28       */
  29      private $options;
  30  
  31      /**
  32       * @param array<string, mixed> $options
  33       */
  34      public function __construct(string $url, array $options)
  35      {
  36          $this->url = $url;
  37          $this->options = $options;
  38          $this->ch = $options['curlHandle'];
  39      }
  40  
  41      /**
  42       * @throws HttpException
  43       *
  44       * @return array{0:int, 1:string|null, 2:string|null}
  45       */
  46      public function post(string $body): array
  47      {
  48          $curl = $this->createCurl();
  49  
  50          curl_setopt($curl, \CURLOPT_POST, true);
  51          curl_setopt($curl, \CURLOPT_POSTFIELDS, $body);
  52  
  53          return $this->execute($curl);
  54      }
  55  
  56      /**
  57       * @return array{0:int, 1:string|null, 2:string|null}
  58       */
  59      public function get(): array
  60      {
  61          $curl = $this->createCurl();
  62  
  63          curl_setopt($curl, \CURLOPT_HTTPGET, true);
  64  
  65          return $this->execute($curl);
  66      }
  67  
  68      /**
  69       * @return \CurlHandle
  70       */
  71      private function createCurl()
  72      {
  73          curl_reset($this->ch);
  74  
  75          $opts = [];
  76          $opts[\CURLOPT_URL] = $this->url;
  77  
  78          if (!empty($this->options['caBundle'])) {
  79              $opts[\CURLOPT_CAINFO] = $this->options['caBundle'];
  80          }
  81  
  82          $opts[\CURLOPT_ENCODING] = '';
  83          $opts[\CURLOPT_SSL_VERIFYHOST] = 2;
  84          $opts[\CURLOPT_FOLLOWLOCATION] = false;
  85          $opts[\CURLOPT_SSL_VERIFYPEER] = true;
  86          $opts[\CURLOPT_RETURNTRANSFER] = true;
  87  
  88          $opts[\CURLOPT_HTTPHEADER] = $this->options['headers'];
  89          $opts[\CURLOPT_USERAGENT] = $this->options['userAgent'];
  90          $opts[\CURLOPT_PROXY] = $this->options['proxy'];
  91  
  92          // The defined()s are here as the *_MS opts are not available on older
  93          // cURL versions
  94          $connectTimeout = $this->options['connectTimeout'];
  95          if (\defined('CURLOPT_CONNECTTIMEOUT_MS')) {
  96              $opts[\CURLOPT_CONNECTTIMEOUT_MS] = ceil($connectTimeout * 1000);
  97          } else {
  98              $opts[\CURLOPT_CONNECTTIMEOUT] = ceil($connectTimeout);
  99          }
 100  
 101          $timeout = $this->options['timeout'];
 102          if (\defined('CURLOPT_TIMEOUT_MS')) {
 103              $opts[\CURLOPT_TIMEOUT_MS] = ceil($timeout * 1000);
 104          } else {
 105              $opts[\CURLOPT_TIMEOUT] = ceil($timeout);
 106          }
 107  
 108          curl_setopt_array($this->ch, $opts);
 109  
 110          return $this->ch;
 111      }
 112  
 113      /**
 114       * @param \CurlHandle $curl
 115       *
 116       * @throws HttpException
 117       *
 118       * @return array{0:int, 1:string|null, 2:string|null}
 119       */
 120      private function execute($curl): array
 121      {
 122          $body = curl_exec($curl);
 123          if ($errno = curl_errno($curl)) {
 124              $errorMessage = curl_error($curl);
 125  
 126              throw new HttpException(
 127                  "cURL error ({$errno}): {$errorMessage}",
 128                  0,
 129                  $this->url
 130              );
 131          }
 132  
 133          $statusCode = curl_getinfo($curl, \CURLINFO_HTTP_CODE);
 134          $contentType = curl_getinfo($curl, \CURLINFO_CONTENT_TYPE);
 135  
 136          return [
 137              $statusCode,
 138              // The PHP docs say "Content-Type: of the requested document. NULL
 139              // indicates server did not send valid Content-Type: header" for
 140              // CURLINFO_CONTENT_TYPE. However, it will return FALSE if no header
 141              // is set. To keep our types simple, we return null in this case.
 142              $contentType === false ? null : $contentType,
 143              $body,
 144          ];
 145      }
 146  }


Generated: Mon Mar 31 05:10:02 2025 Cross-referenced by PHPXref 0.7.1