|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Class CurlClient |
| 3 | + * Class HTTPClient |
4 | 4 | * |
5 | | - * @filesource CurlClient.php |
6 | | - * @created 21.10.2017 |
| 5 | + * @filesource HTTPClient.php |
| 6 | + * @created 27.08.2018 |
7 | 7 | * @package chillerlan\HTTP |
8 | | - * @author Smiley <smiley@chillerlan.net> |
9 | | - * @copyright 2017 Smiley |
| 8 | + * @author smiley <smiley@chillerlan.net> |
| 9 | + * @copyright 2018 smiley |
10 | 10 | * @license MIT |
11 | 11 | */ |
12 | 12 |
|
13 | 13 | namespace chillerlan\HTTP; |
14 | 14 |
|
15 | | -use chillerlan\Traits\ImmutableSettingsInterface; |
| 15 | +use chillerlan\HTTP\Psr17\{RequestFactory, ResponseFactory, StreamFactory}; |
| 16 | +use chillerlan\HTTP\Psr7; |
| 17 | +use chillerlan\Settings\SettingsContainerInterface; |
| 18 | +use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, StreamFactoryInterface}; |
| 19 | +use Http\Client\Exception\{NetworkException, RequestException}; |
16 | 20 |
|
17 | | -/** |
18 | | - * @property resource $http |
19 | | - */ |
20 | | -class CurlClient extends HTTPClientAbstract{ |
| 21 | +class CurlClient implements HTTPClientInterface{ |
21 | 22 |
|
22 | 23 | /** |
23 | | - * @var \stdClass |
| 24 | + * @var \chillerlan\HTTP\HTTPOptions |
24 | 25 | */ |
25 | | - protected $responseHeaders; |
| 26 | + protected $options; |
26 | 27 |
|
27 | 28 | /** |
28 | | - * CurlClient constructor. |
29 | | - * |
30 | | - * @param \chillerlan\Traits\ImmutableSettingsInterface $options |
31 | | - * |
32 | | - * @throws \chillerlan\HTTP\HTTPClientException |
| 29 | + * @var \Psr\Http\Message\RequestFactoryInterface |
33 | 30 | */ |
34 | | - public function __construct(ImmutableSettingsInterface $options){ |
35 | | - parent::__construct($options); |
| 31 | + protected $requestFactory; |
36 | 32 |
|
37 | | - if(!isset($this->options->ca_info) || !is_file($this->options->ca_info)){ |
38 | | - throw new HTTPClientException('invalid CA file'); |
39 | | - } |
| 33 | + /** |
| 34 | + * @var \Psr\Http\Message\ResponseFactoryInterface |
| 35 | + */ |
| 36 | + protected $responseFactory; |
40 | 37 |
|
41 | | - } |
| 38 | + /** |
| 39 | + * @var \Psr\Http\Message\StreamFactoryInterface |
| 40 | + */ |
| 41 | + protected $streamFactory; |
42 | 42 |
|
43 | 43 | /** |
44 | | - * @return void |
| 44 | + * CurlClient constructor. |
| 45 | + * |
| 46 | + * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
| 47 | + * @param \Psr\Http\Message\RequestFactoryInterface|null $requestFactory |
| 48 | + * @param \Psr\Http\Message\ResponseFactoryInterface|null $responseFactory |
| 49 | + * @param \Psr\Http\Message\StreamFactoryInterface|null $streamFactory |
45 | 50 | */ |
46 | | - protected function initCurl(){ |
47 | | - $this->http = curl_init(); |
48 | | - |
49 | | - curl_setopt_array($this->http, [ |
50 | | - CURLOPT_HEADER => false, |
51 | | - CURLOPT_RETURNTRANSFER => true, |
52 | | - CURLOPT_PROTOCOLS => CURLPROTO_HTTP|CURLPROTO_HTTPS, |
53 | | - CURLOPT_CAINFO => $this->options->ca_info, |
54 | | - CURLOPT_SSL_VERIFYPEER => true, |
55 | | - CURLOPT_SSL_VERIFYHOST => 2, |
56 | | - CURLOPT_TIMEOUT => 5, |
57 | | - CURLOPT_USERAGENT => $this->options->user_agent, |
58 | | - ]); |
59 | | - |
60 | | - curl_setopt_array($this->http, $this->options->curl_options); |
| 51 | + public function __construct( |
| 52 | + SettingsContainerInterface $options = null, |
| 53 | + RequestFactoryInterface $requestFactory = null, |
| 54 | + ResponseFactoryInterface $responseFactory = null, |
| 55 | + StreamFactoryInterface $streamFactory = null |
| 56 | + ){ |
| 57 | + $this->options = $options ?? new HTTPOptions; |
| 58 | + $this->requestFactory = $requestFactory ?? new RequestFactory; |
| 59 | + $this->responseFactory = $responseFactory ?? new ResponseFactory; |
| 60 | + $this->streamFactory = $streamFactory ?? new StreamFactory; |
61 | 61 | } |
62 | 62 |
|
63 | | - /** @inheritdoc */ |
64 | | - protected function getResponse():HTTPResponseInterface{ |
65 | | - $this->responseHeaders = new \stdClass; |
| 63 | + /** |
| 64 | + * Sends a PSR-7 request. |
| 65 | + * |
| 66 | + * @param \Psr\Http\Message\RequestInterface $request |
| 67 | + * |
| 68 | + * @return \Psr\Http\Message\ResponseInterface |
| 69 | + * |
| 70 | + * @throws \Http\Client\Exception If an error happens during processing the request. |
| 71 | + * @throws \Exception If processing the request is impossible (eg. bad configuration). |
| 72 | + */ |
| 73 | + public function sendRequest(RequestInterface $request):ResponseInterface{ |
| 74 | + $handle = new CurlHandle($request, $this->responseFactory->createResponse(), $this->options); |
| 75 | + $handle->init(); |
66 | 76 |
|
67 | | - $headers = $this->normalizeRequestHeaders($this->requestHeaders); |
| 77 | + curl_exec($handle->ch); |
68 | 78 |
|
69 | | - if(in_array($this->requestMethod, ['PATCH', 'POST', 'PUT', 'DELETE'])){ |
| 79 | + $errno = curl_errno($handle->ch); |
70 | 80 |
|
71 | | - $options = in_array($this->requestMethod, ['PATCH', 'PUT', 'DELETE']) |
72 | | - ? [CURLOPT_CUSTOMREQUEST => $this->requestMethod] |
73 | | - : [CURLOPT_POST => true]; |
| 81 | + if($errno !== CURLE_OK){ |
| 82 | + $error = curl_error($handle->ch); |
74 | 83 |
|
| 84 | + $network_errors = [ |
| 85 | + CURLE_COULDNT_RESOLVE_PROXY, |
| 86 | + CURLE_COULDNT_RESOLVE_HOST, |
| 87 | + CURLE_COULDNT_CONNECT, |
| 88 | + CURLE_OPERATION_TIMEOUTED, |
| 89 | + CURLE_SSL_CONNECT_ERROR, |
| 90 | + CURLE_GOT_NOTHING, |
| 91 | + ]; |
75 | 92 |
|
76 | | - if(!isset($headers['Content-type']) && $this->requestMethod === 'POST' && is_array($this->requestBody)){ |
77 | | - $headers += ['Content-type: application/x-www-form-urlencoded']; |
78 | | - $this->requestBody = http_build_query($this->requestBody, '', '&', PHP_QUERY_RFC1738); |
| 93 | + if(in_array($errno, $network_errors, true)){ |
| 94 | + throw new NetworkException($error, $request); |
79 | 95 | } |
80 | 96 |
|
81 | | - $options += [CURLOPT_POSTFIELDS => $this->requestBody]; |
82 | | - } |
83 | | - else{ |
84 | | - $options = [CURLOPT_CUSTOMREQUEST => $this->requestMethod]; |
| 97 | + throw new RequestException($error, $request); |
85 | 98 | } |
86 | 99 |
|
87 | | - $headers += [ |
88 | | - 'Host: '.$this->parsedURL['host'], |
89 | | - 'Connection: close', |
90 | | - ]; |
91 | | - |
92 | | - parse_str($this->parsedURL['query'] ?? '', $parsedquery); |
93 | | - $params = array_merge($parsedquery, $this->requestParams); |
94 | | - |
95 | | - $url = $this->requestURL.(!empty($params) ? '?'.$this->buildQuery($params) : ''); |
96 | | - |
97 | | - $options += [ |
98 | | - CURLOPT_URL => $url, |
99 | | - CURLOPT_HTTPHEADER => $headers, |
100 | | - CURLOPT_HEADERFUNCTION => [$this, 'headerLine'], |
101 | | - ]; |
102 | | - |
103 | | - $this->initCurl(); |
104 | | - curl_setopt_array($this->http, $options); |
| 100 | + $handle->close(); |
| 101 | + $handle->response->getBody()->rewind(); |
105 | 102 |
|
106 | | - $response = curl_exec($this->http); |
107 | | - $curl_info = curl_getinfo($this->http); |
| 103 | + return $handle->response; |
108 | 104 |
|
109 | | - return new HTTPResponse([ |
110 | | - 'url' => $url, |
111 | | - 'curl_info' => $curl_info, |
112 | | - 'headers' => $this->responseHeaders, |
113 | | - 'body' => $response, |
114 | | - ]); |
115 | 105 | } |
116 | 106 |
|
117 | 107 | /** |
118 | | - * @param resource $curl |
119 | | - * @param string $header_line |
| 108 | + * @param string $uri |
| 109 | + * @param string|null $method |
| 110 | + * @param array|null $query |
| 111 | + * @param mixed|null $body |
| 112 | + * @param array|null $headers |
120 | 113 | * |
121 | | - * @return int |
122 | | - * |
123 | | - * @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
| 114 | + * @return \Psr\Http\Message\ResponseInterface |
124 | 115 | */ |
125 | | - protected function headerLine($curl, $header_line){ |
126 | | - $header = explode(':', $header_line, 2); |
| 116 | + public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{ |
| 117 | + $method = strtoupper($method ?? 'GET'); |
| 118 | + $headers = Psr7\normalize_request_headers($headers); |
| 119 | + $request = $this->requestFactory->createRequest($method, Psr7\merge_query($uri, $query ?? [])); |
| 120 | + |
| 121 | + if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){ |
| 122 | + |
| 123 | + if(is_array($body) || is_object($body)){ |
| 124 | + |
| 125 | + if(!isset($headers['Content-type'])){ |
| 126 | + $headers['Content-type'] = 'application/x-www-form-urlencoded'; |
| 127 | + } |
| 128 | + |
| 129 | + if($headers['Content-type'] === 'application/x-www-form-urlencoded'){ |
| 130 | + $body = http_build_query($body, '', '&', PHP_QUERY_RFC1738); |
| 131 | + } |
| 132 | + elseif($headers['Content-type'] === 'application/json'){ |
| 133 | + $body = json_encode($body); |
| 134 | + } |
| 135 | + |
| 136 | + } |
127 | 137 |
|
128 | | - if(count($header) === 2){ |
129 | | - $this->responseHeaders->{trim(strtolower($header[0]))} = trim($header[1]); |
| 138 | + $request = $request->withBody($this->streamFactory->createStream((string)$body)); |
130 | 139 | } |
131 | | - elseif(substr($header_line, 0, 4) === 'HTTP'){ |
132 | | - $status = explode(' ', $header_line, 3); |
133 | 140 |
|
134 | | - $this->responseHeaders->httpversion = explode('/', $status[0], 2)[1]; |
135 | | - $this->responseHeaders->statuscode = intval($status[1]); |
136 | | - $this->responseHeaders->statustext = trim($status[2]); |
| 141 | + foreach($headers as $header => $value){ |
| 142 | + $request = $request->withAddedHeader($header, $value); |
137 | 143 | } |
138 | 144 |
|
139 | | - return strlen($header_line); |
| 145 | + return $this->sendRequest($request); |
140 | 146 | } |
141 | 147 |
|
142 | 148 | } |
0 commit comments