|
13 | 13 | namespace chillerlan\HTTP; |
14 | 14 |
|
15 | 15 | use chillerlan\HTTP\Psr17\{RequestFactory, ResponseFactory, StreamFactory}; |
16 | | -use chillerlan\HTTP\Psr7; |
17 | 16 | use chillerlan\Settings\SettingsContainerInterface; |
18 | 17 | use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, StreamFactoryInterface}; |
19 | 18 | use Http\Client\Exception\{NetworkException, RequestException}; |
20 | 19 |
|
21 | 20 | class CurlClient implements HTTPClientInterface{ |
| 21 | + use HTTPRequestTrait; |
22 | 22 |
|
23 | 23 | /** |
24 | 24 | * @var \chillerlan\HTTP\HTTPOptions |
@@ -104,45 +104,4 @@ public function sendRequest(RequestInterface $request):ResponseInterface{ |
104 | 104 |
|
105 | 105 | } |
106 | 106 |
|
107 | | - /** |
108 | | - * @param string $uri |
109 | | - * @param string|null $method |
110 | | - * @param array|null $query |
111 | | - * @param mixed|null $body |
112 | | - * @param array|null $headers |
113 | | - * |
114 | | - * @return \Psr\Http\Message\ResponseInterface |
115 | | - */ |
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 | | - } |
137 | | - |
138 | | - $request = $request->withBody($this->streamFactory->createStream((string)$body)); |
139 | | - } |
140 | | - |
141 | | - foreach($headers as $header => $value){ |
142 | | - $request = $request->withAddedHeader($header, $value); |
143 | | - } |
144 | | - |
145 | | - return $this->sendRequest($request); |
146 | | - } |
147 | | - |
148 | 107 | } |
0 commit comments