-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCurlClient.php
More file actions
55 lines (41 loc) · 1.04 KB
/
CurlClient.php
File metadata and controls
55 lines (41 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Class HTTPClient
*
* @created 27.08.2018
* @author smiley <[email protected]>
* @copyright 2018 smiley
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\HTTP;
use Psr\Http\Message\{RequestInterface, ResponseInterface};
use function in_array, sprintf;
use const CURLE_OK;
/**
* A "simple" cURL http client
*/
class CurlClient extends HTTPClientAbstract{
/**
* @inheritDoc
*/
public function sendRequest(RequestInterface $request):ResponseInterface{
$handle = new CurlHandle(
$request,
$this->responseFactory->createResponse(),
$this->options,
$this->streamFactory?->createStream(),
);
$errno = $handle->exec();
if($errno !== CURLE_OK){
$error = $handle->getError();
$this->logger->error(sprintf('cURL error #%s: %s', $errno, $error));
if(in_array($errno, $handle::CURL_NETWORK_ERRORS, true)){
throw new NetworkException($error, $request);
}
throw new RequestException($error, $request);
}
$handle->close();
return $handle->getResponse();
}
}