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