|
18 | 18 | use Http\Client\Exception\{NetworkException, RequestException}; |
19 | 19 |
|
20 | 20 | class CurlClient implements HTTPClientInterface{ |
21 | | - use HTTPRequestTrait; |
22 | 21 |
|
23 | 22 | /** |
24 | 23 | * @var \chillerlan\HTTP\HTTPOptions |
@@ -104,4 +103,47 @@ public function sendRequest(RequestInterface $request):ResponseInterface{ |
104 | 103 |
|
105 | 104 | } |
106 | 105 |
|
| 106 | + /** |
| 107 | + * @todo: files, content-type |
| 108 | + * |
| 109 | + * @param string $uri |
| 110 | + * @param string|null $method |
| 111 | + * @param array|null $query |
| 112 | + * @param mixed|null $body |
| 113 | + * @param array|null $headers |
| 114 | + * |
| 115 | + * @return \Psr\Http\Message\ResponseInterface |
| 116 | + */ |
| 117 | + public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{ |
| 118 | + $method = strtoupper($method ?? 'GET'); |
| 119 | + $headers = Psr7\normalize_request_headers($headers); |
| 120 | + $request = $this->requestFactory->createRequest($method, Psr7\merge_query($uri, $query ?? [])); |
| 121 | + |
| 122 | + if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){ |
| 123 | + |
| 124 | + if(is_array($body) || is_object($body)){ |
| 125 | + |
| 126 | + if(!isset($headers['Content-type'])){ |
| 127 | + $headers['Content-type'] = 'application/x-www-form-urlencoded'; |
| 128 | + } |
| 129 | + |
| 130 | + if($headers['Content-type'] === 'application/x-www-form-urlencoded'){ |
| 131 | + $body = http_build_query($body, '', '&', PHP_QUERY_RFC1738); |
| 132 | + } |
| 133 | + elseif($headers['Content-type'] === 'application/json'){ |
| 134 | + $body = json_encode($body); |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + $request = $request->withBody($this->streamFactory->createStream((string)$body)); |
| 140 | + } |
| 141 | + |
| 142 | + foreach($headers as $header => $value){ |
| 143 | + $request = $request->withAddedHeader($header, $value); |
| 144 | + } |
| 145 | + |
| 146 | + return $this->sendRequest($request); |
| 147 | + } |
| 148 | + |
107 | 149 | } |
0 commit comments