vendor/mpdf/mpdf/src/Http/CurlHttpClient.php line 25

Open in your IDE?
  1. <?php
  2. namespace Mpdf\Http;
  3. use Mpdf\Log\Context as LogContext;
  4. use Mpdf\Mpdf;
  5. use Mpdf\PsrHttpMessageShim\Response;
  6. use Mpdf\PsrHttpMessageShim\Stream;
  7. use Mpdf\PsrLogAwareTrait\PsrLogAwareTrait;
  8. use Psr\Http\Message\RequestInterface;
  9. use Psr\Log\LoggerInterface;
  10. class CurlHttpClient implements \Mpdf\Http\ClientInterface\Psr\Log\LoggerAwareInterface
  11. {
  12.     use PsrLogAwareTrait;
  13.     private $mpdf;
  14.     public function __construct(Mpdf $mpdfLoggerInterface $logger)
  15.     {
  16.         $this->mpdf $mpdf;
  17.         $this->logger $logger;
  18.     }
  19.     public function sendRequest(RequestInterface $request)
  20.     {
  21.         if (null === $request->getUri()) {
  22.             return (new Response());
  23.         }
  24.         $url $request->getUri();
  25.         $this->logger->debug(sprintf('Fetching (cURL) content of remote URL "%s"'$url), ['context' => LogContext::REMOTE_CONTENT]);
  26.         $response = new Response();
  27.         $ch curl_init($url);
  28.         curl_setopt($chCURLOPT_USERAGENT$this->mpdf->curlUserAgent);
  29.         curl_setopt($chCURLOPT_HEADER0);
  30.         curl_setopt($chCURLOPT_NOBODY0);
  31.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  32.         curl_setopt($chCURLOPT_CONNECTTIMEOUT$this->mpdf->curlTimeout);
  33.         if ($this->mpdf->curlExecutionTimeout) {
  34.             curl_setopt($chCURLOPT_TIMEOUT$this->mpdf->curlExecutionTimeout);
  35.         }
  36.         if ($this->mpdf->curlFollowLocation) {
  37.             curl_setopt($chCURLOPT_FOLLOWLOCATION1);
  38.         }
  39.         if ($this->mpdf->curlAllowUnsafeSslRequests) {
  40.             curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
  41.             curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  42.         }
  43.         if ($this->mpdf->curlCaCertificate && is_file($this->mpdf->curlCaCertificate)) {
  44.             curl_setopt($chCURLOPT_CAINFO$this->mpdf->curlCaCertificate);
  45.         }
  46.         if ($this->mpdf->curlProxy) {
  47.             curl_setopt($chCURLOPT_PROXY$this->mpdf->curlProxy);
  48.             if ($this->mpdf->curlProxyAuth) {
  49.                 curl_setopt($chCURLOPT_PROXYUSERPWD$this->mpdf->curlProxyAuth);
  50.             }
  51.         }
  52.         curl_setopt(
  53.             $ch,
  54.             CURLOPT_HEADERFUNCTION,
  55.             static function ($curl$header) use (&$response) {
  56.                 $len strlen($header);
  57.                 $header explode(':'$header2);
  58.                 if (count($header) < 2) { // ignore invalid headers
  59.                     return $len;
  60.                 }
  61.                 $response $response->withHeader(trim($header[0]), trim($header[1]));
  62.                 return $len;
  63.             }
  64.         );
  65.         $data curl_exec($ch);
  66.         if (curl_error($ch)) {
  67.             $message sprintf('cURL error: "%s"'curl_error($ch));
  68.             $this->logger->error($message, ['context' => LogContext::REMOTE_CONTENT]);
  69.             if ($this->mpdf->debug) {
  70.                 throw new \Mpdf\MpdfException($message);
  71.             }
  72.             curl_close($ch);
  73.             return $response;
  74.         }
  75.         $info curl_getinfo($ch);
  76.         if (isset($info['http_code']) && !str_starts_with((string) $info['http_code'], '2')) {
  77.             $message sprintf('HTTP error: %d'$info['http_code']);
  78.             $this->logger->error($message, ['context' => LogContext::REMOTE_CONTENT]);
  79.             if ($this->mpdf->debug) {
  80.                 throw new \Mpdf\MpdfException($message);
  81.             }
  82.             curl_close($ch);
  83.             return $response->withStatus($info['http_code']);
  84.         }
  85.         curl_close($ch);
  86.         return $response
  87.             ->withStatus($info['http_code'])
  88.             ->withBody(Stream::create($data));
  89.     }
  90. }