1 | <?php |
||
28 | final class CurlHttpClient implements HttpClientInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var null|Uri |
||
32 | */ |
||
33 | private $url; |
||
34 | |||
35 | /** |
||
36 | * @var null|HttpClientAuthentication |
||
37 | */ |
||
38 | private $auth; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $options = [ |
||
44 | CURLOPT_RETURNTRANSFER => true, |
||
45 | CURLOPT_HEADER => true |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var resource |
||
50 | */ |
||
51 | private $handler; |
||
52 | |||
53 | /** |
||
54 | * Creates a CURL HTTP Client |
||
55 | * |
||
56 | * @param Uri|null $url |
||
57 | * @param HttpClientAuthentication|null $auth |
||
58 | * @param array $options |
||
59 | */ |
||
60 | public function __construct(Uri $url = null, HttpClientAuthentication $auth = null, array $options = []) |
||
70 | |||
71 | /** |
||
72 | * Send out an HTTP requests returning a promise |
||
73 | * |
||
74 | * @param RequestInterface $request |
||
75 | * |
||
76 | * @return PromiseInterface |
||
77 | */ |
||
78 | public function send(RequestInterface $request) |
||
90 | |||
91 | /** |
||
92 | * Call the HTTP server returning the response |
||
93 | * |
||
94 | * @param RequestInterface $request |
||
95 | * |
||
96 | * @return Response |
||
97 | * |
||
98 | * @throws RuntimeException If any error occur while preparing and connecting to the server |
||
99 | * @throws ClientErrorException for responses with status codes between 400 and 499 |
||
100 | * @throws ServerErrorException for responses with status codes grater or equal to 500 |
||
101 | */ |
||
102 | private function call(RequestInterface $request) |
||
121 | |||
122 | /** |
||
123 | * Prepares the cURL handler options |
||
124 | * |
||
125 | * @param RequestInterface $request |
||
126 | */ |
||
127 | private function prepare(RequestInterface $request) |
||
142 | |||
143 | /** |
||
144 | * Sets the URL for cURL to use |
||
145 | * |
||
146 | * @param RequestInterface $request |
||
147 | */ |
||
148 | private function setUrl(RequestInterface $request) |
||
155 | |||
156 | /** |
||
157 | * Sets the headers from the request |
||
158 | * |
||
159 | * @param RequestInterface $request |
||
160 | */ |
||
161 | private function setHeaders(RequestInterface $request) |
||
169 | |||
170 | /** |
||
171 | * Resets the cURL handler |
||
172 | * |
||
173 | * @param resource $ch |
||
174 | */ |
||
175 | private function reset(&$ch){ |
||
178 | |||
179 | /** |
||
180 | * Creates a response from cURL execution result |
||
181 | * |
||
182 | * @param string $result |
||
183 | * |
||
184 | * @return Response |
||
185 | */ |
||
186 | private function createResponse($result) |
||
192 | |||
193 | /** |
||
194 | * Splits the cURL execution result into header and body |
||
195 | * |
||
196 | * @param $result |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | private function splitHeaderFromBody($result) |
||
209 | |||
210 | /** |
||
211 | * Parses the HTTP message headers from header part |
||
212 | * |
||
213 | * @param string $header |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | private function parseHeaders($header) |
||
231 | |||
232 | /** |
||
233 | * Checks provided response is an HTTP client error (4xx) |
||
234 | * |
||
235 | * @param ResponseInterface $response |
||
236 | * @param RequestInterface $request |
||
237 | * |
||
238 | * @throws ClientErrorException for responses with status codes between 400 and 499 |
||
239 | */ |
||
240 | private function checkClientError(ResponseInterface $response, RequestInterface $request) |
||
246 | |||
247 | /** |
||
248 | * Checks provided response is an HTTP server error (5xx) |
||
249 | * |
||
250 | * @param ResponseInterface $response |
||
251 | * @param RequestInterface $request |
||
252 | * |
||
253 | * @throws ServerErrorException for responses with status codes grater or equal to 500 |
||
254 | */ |
||
255 | private function checkServerError(ResponseInterface $response, RequestInterface $request) |
||
261 | |||
262 | /** |
||
263 | * Close the cURL handler on destruct |
||
264 | */ |
||
265 | public function __destruct() |
||
270 | } |