CurlHandler.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Promise\PromiseInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. /**
  6. * HTTP handler that uses cURL easy handles as a transport layer.
  7. *
  8. * When using the CurlHandler, custom curl options can be specified as an
  9. * associative array of curl option constants mapping to values in the
  10. * **curl** key of the "client" key of the request.
  11. *
  12. * @final
  13. */
  14. class CurlHandler
  15. {
  16. /**
  17. * @var CurlFactoryInterface
  18. */
  19. private $factory;
  20. /**
  21. * Accepts an associative array of options:
  22. *
  23. * - handle_factory: Optional curl factory used to create cURL handles.
  24. *
  25. * @param array{handle_factory?: ?CurlFactoryInterface} $options Array of options to use with the handler
  26. */
  27. public function __construct(array $options = [])
  28. {
  29. $this->factory = $options['handle_factory']
  30. ?? new CurlFactory(3);
  31. }
  32. public function __invoke(RequestInterface $request, array $options): PromiseInterface
  33. {
  34. if (isset($options['delay'])) {
  35. \usleep($options['delay'] * 1000);
  36. }
  37. $easy = $this->factory->create($request, $options);
  38. \curl_exec($easy->handle);
  39. $easy->errno = \curl_errno($easy->handle);
  40. return CurlFactory::finish($this, $easy, $this->factory);
  41. }
  42. }