1 | <?php |
||
25 | final class CurlHttpClient implements ClientInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var null|Uri |
||
29 | */ |
||
30 | private $url; |
||
31 | |||
32 | /** |
||
33 | * @var null|HttpClientAuthentication |
||
34 | */ |
||
35 | private $auth; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options = [ |
||
41 | CURLOPT_RETURNTRANSFER => true, |
||
42 | CURLOPT_HEADER => true |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @var resource |
||
47 | */ |
||
48 | private $handler; |
||
49 | |||
50 | /** |
||
51 | * Creates a CURL HTTP Client |
||
52 | * |
||
53 | * @param Uri|null $url |
||
54 | * @param HttpClientAuthentication|null $auth |
||
55 | * @param array $options |
||
56 | */ |
||
57 | public function __construct(Uri $url = null, HttpClientAuthentication $auth = null, array $options = []) |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | public function sendRequest(RequestInterface $request): ResponseInterface |
||
93 | |||
94 | /** |
||
95 | * Prepares the cURL handler options |
||
96 | * |
||
97 | * @param RequestInterface $request |
||
98 | */ |
||
99 | private function prepare(RequestInterface $request) |
||
114 | |||
115 | /** |
||
116 | * Sets the URL for cURL to use |
||
117 | * |
||
118 | * @param RequestInterface $request |
||
119 | */ |
||
120 | private function setUrl(RequestInterface $request) |
||
136 | |||
137 | /** |
||
138 | * Sets the headers from the request |
||
139 | * |
||
140 | * @param RequestInterface $request |
||
141 | */ |
||
142 | private function setHeaders(RequestInterface $request) |
||
150 | |||
151 | /** |
||
152 | * Resets the cURL handler |
||
153 | * |
||
154 | * @param resource $ch |
||
155 | */ |
||
156 | private function reset(&$ch) |
||
160 | |||
161 | /** |
||
162 | * Creates a response from cURL execution result |
||
163 | * |
||
164 | * @param string $result |
||
165 | * |
||
166 | * @return Response |
||
167 | */ |
||
168 | private function createResponse($result) |
||
174 | |||
175 | /** |
||
176 | * Splits the cURL execution result into header and body |
||
177 | * |
||
178 | * @param $result |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | private function splitHeaderFromBody($result) |
||
191 | |||
192 | /** |
||
193 | * Parses the HTTP message headers from header part |
||
194 | * |
||
195 | * @param string $header |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | private function parseHeaders($header) |
||
213 | |||
214 | /** |
||
215 | * Close the cURL handler on destruct |
||
216 | */ |
||
217 | public function __destruct() |
||
223 | } |
||
224 |