Няма описание

CurlHandler.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Psr7;
  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. class CurlHandler
  13. {
  14. /** @var CurlFactoryInterface */
  15. private $factory;
  16. /**
  17. * Accepts an associative array of options:
  18. *
  19. * - factory: Optional curl factory used to create cURL handles.
  20. *
  21. * @param array $options Array of options to use with the handler
  22. */
  23. public function __construct(array $options = [])
  24. {
  25. $this->factory = isset($options['handle_factory'])
  26. ? $options['handle_factory']
  27. : new CurlFactory(3);
  28. }
  29. public function __invoke(RequestInterface $request, array $options)
  30. {
  31. if (isset($options['delay'])) {
  32. usleep($options['delay'] * 1000);
  33. }
  34. $easy = $this->factory->create($request, $options);
  35. curl_exec($easy->handle);
  36. $easy->errno = curl_errno($easy->handle);
  37. return CurlFactory::finish($this, $easy, $this->factory);
  38. }
  39. }