diff --git a/src/ClientAbstracts.php b/src/ClientAbstracts.php index 47cfc73..2b04498 100644 --- a/src/ClientAbstracts.php +++ b/src/ClientAbstracts.php @@ -48,11 +48,41 @@ abstract class ClientAbstracts implements IClient private int $port = 80; + /** + * @var ResponseInterface|null + */ + protected ?ResponseInterface $body; + + private ?StreamInterface $_data = null; private int $connect_timeout = 1; + protected mixed $client; + + + /** + * @return string|ResponseInterface|null + */ + public function getBody(): string|ResponseInterface|null + { + if ($this->body instanceof ResponseInterface) { + return $this->body->getBody()->getContents(); + } + return $this->body; + } + + + /** + * @param ResponseInterface|null $body + */ + public function setBody(?ResponseInterface $body): void + { + $this->body = $body; + } + + /** * @param $host * @param $port @@ -67,22 +97,20 @@ abstract class ClientAbstracts implements IClient /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function post(string $path, array $params = []): ResponseInterface + public function post(string $path, array $params = []): void { - return $this->request(self::POST, $path, $params); + $this->request(self::POST, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function put(string $path, array $params = []): ResponseInterface + public function put(string $path, array $params = []): void { - return $this->request(self::PUT, $path, $params); + $this->request(self::PUT, $path, $params); } @@ -100,63 +128,57 @@ abstract class ClientAbstracts implements IClient /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function head(string $path, array $params = []): ResponseInterface + public function head(string $path, array $params = []): void { - return $this->request(self::HEAD, $path, $params); + $this->request(self::HEAD, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function get(string $path, array $params = []): ResponseInterface + public function get(string $path, array $params = []): void { - return $this->request(self::GET, $path, $params); + $this->request(self::GET, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function option(string $path, array $params = []): ResponseInterface + public function option(string $path, array $params = []): void { - return $this->request(self::OPTIONS, $path, $params); + $this->request(self::OPTIONS, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function delete(string $path, array $params = []): ResponseInterface + public function delete(string $path, array $params = []): void { - return $this->request(self::DELETE, $path, $params); + $this->request(self::DELETE, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function options(string $path, array $params = []): ResponseInterface + public function options(string $path, array $params = []): void { - return $this->request(self::OPTIONS, $path, $params); + $this->request(self::OPTIONS, $path, $params); } /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function upload(string $path, array $params = []): ResponseInterface + public function upload(string $path, array $params = []): void { - return $this->request(self::UPLOAD, $path, $params); + $this->request(self::UPLOAD, $path, $params); } diff --git a/src/CoroutineClient.php b/src/CoroutineClient.php index 83b566c..32b825b 100644 --- a/src/CoroutineClient.php +++ b/src/CoroutineClient.php @@ -15,7 +15,6 @@ use Http\Message\Stream; use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Logger; use Kiri\Kiri; -use Psr\Http\Message\ResponseInterface; use Swoole\Coroutine\Http\Client as SwowClient; /** @@ -29,12 +28,12 @@ class CoroutineClient extends ClientAbstracts * @param string $method * @param $path * @param array $params - * @return ResponseInterface + * @return void * @throws Exception */ - public function request(string $method, $path, array $params = []): ResponseInterface + public function request(string $method, $path, array $params = []): void { - return $this->withMethod($method) + $this->withMethod($method) ->coroutine( $this->matchHost($path), $this->paramEncode($params) @@ -45,23 +44,24 @@ class CoroutineClient extends ClientAbstracts /** * @param $url * @param array|string $data - * @return ResponseInterface * @throws Exception 使用swoole协程方式请求 */ - private function coroutine($url, array|string $data = []): ResponseInterface + private function coroutine($url, array|string $data = []): void { try { - $client = $this->generate_client($data, ...$url); - if ($client->statusCode < 0) { - throw new Exception($client->errMsg); + $this->generate_client($data, ...$url); + if ($this->client->statusCode < 0) { + throw new Exception($this->client->errMsg); } - return (new Response())->withStatus($client->getStatusCode()) - ->withHeaders($client->getHeaders()) - ->withBody(new Stream($client->getBody())); + $body = (new Response())->withStatus($this->client->getStatusCode()) + ->withHeaders($this->client->getHeaders()) + ->withBody(new Stream($this->client->getBody())); } catch (\Throwable $exception) { Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); - return (new Response())->withStatus(-1)->withHeaders([]) + $body = (new Response())->withStatus(-1)->withHeaders([]) ->withBody(new Stream(jTraceEx($exception))); + } finally { + $this->setBody($body); } } @@ -71,50 +71,55 @@ class CoroutineClient extends ClientAbstracts * @param $host * @param $isHttps * @param $path - * @return SwowClient */ - private function generate_client($data, $host, $isHttps, $path): SwowClient + private function generate_client($data, $host, $isHttps, $path): void { if ($isHttps || $this->isSSL()) { - $client = new SwowClient($host, 443, true); + $this->client = new SwowClient($host, 443, true); } else { - $client = new SwowClient($host, $this->getPort(), false); + $this->client = new SwowClient($host, $this->getPort(), false); } - $client->set($this->settings()); + $this->client->set($this->settings()); if (!empty($this->getAgent())) { $this->withAddedHeader('User-Agent', $this->getAgent()); } - $client->setHeaders($this->getHeader()); - $client->setMethod(strtoupper($this->getMethod())); - $client->execute($this->setParams($client, $path, $data)); - $client->close(); - return $client; + $this->client->setHeaders($this->getHeader()); + $this->client->setMethod(strtoupper($this->getMethod())); + $this->client->execute($this->setParams($path, $data)); } /** - * @param SwowClient $client * @param $path * @param $data * @return string */ - private function setParams(SwowClient $client, $path, $data): string + private function setParams($path, $data): string { $content = $this->getData()->getContents(); if (!empty($content)) { - $client->setData($content); + $this->client->setData($content); } if ($this->isGet()) { if (!empty($data)) $path .= '?' . $data; } else { $data = $this->mergeParams($data); if (!empty($data)) { - $client->setData($data); + $this->client->setData($data); } } return $path; } + + /** + * + */ + public function close(): void + { + $this->client->close(); + } + /** * @return array */ diff --git a/src/Curl.php b/src/Curl.php index 4ba4730..67c9993 100644 --- a/src/Curl.php +++ b/src/Curl.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Http\Client; -use CurlHandle; use Exception; use Http\Message\Response; use Http\Message\Stream; @@ -23,15 +22,17 @@ class Curl extends ClientAbstracts * @param $method * @param $path * @param array $params - * @return ResponseInterface * @throws Exception */ - public function request($method, $path, array $params = []): ResponseInterface + public function request($method, $path, array $params = []): void { if ($method == self::GET) { $path = $this->joinGetParams($path, $params); } - return $this->execute($this->getCurlHandler($path, $method, $params)); + + $this->getCurlHandler($path, $method, $params); + + $this->execute(); } @@ -39,10 +40,9 @@ class Curl extends ClientAbstracts * @param $path * @param $method * @param $params - * @return CurlHandle * @throws Exception */ - private function getCurlHandler($path, $method, $params): CurlHandle + private function getCurlHandler($path, $method, $params): void { [$host, $isHttps, $path] = $this->matchHost($path); @@ -51,45 +51,43 @@ class Curl extends ClientAbstracts $host .= ':' . $this->getPort(); } - $resource = $this->do(curl_init($host . $path), $host . $path, $method); + $this->do(curl_init($host . $path), $host . $path, $method); if ($isHttps !== false) { - $this->curlHandlerSslSet($resource); + $this->curlHandlerSslSet(); } $contents = $this->getData()->getContents(); if (empty($params) && empty($contents)) { - return $resource; + return; } if (!empty($contents)) { - curl_setopt($resource, CURLOPT_POSTFIELDS, $contents); + curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents); } else if ($method === self::POST) { - curl_setopt($resource, CURLOPT_POSTFIELDS, $this->mergeParams($params)); + curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params)); } else if ($method === self::UPLOAD) { - curl_setopt($resource, CURLOPT_POSTFIELDS, $params); + curl_setopt($this->client, CURLOPT_POSTFIELDS, $params); } - return $resource; } /** - * @param $resource * @return void * @throws Exception */ - private function curlHandlerSslSet($resource): void + private function curlHandlerSslSet(): void { if (!empty($this->ssl_key)) { if (!file_exists($this->ssl_key)) { throw new Exception('SSL protocol certificate not found.'); } - curl_setopt($resource, CURLOPT_SSLKEY, $this->getSslKeyFile()); + curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile()); } if (!empty($this->ssl_cert)) { if (!!file_exists($this->ssl_cert)) { throw new Exception('SSL protocol certificate not found.'); } - curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile()); + curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile()); } } @@ -98,10 +96,9 @@ class Curl extends ClientAbstracts * @param $resource * @param $path * @param $method - * @return CurlHandle * @throws Exception */ - private function do($resource, $path, $method): CurlHandle + private function do($resource, $path, $method): void { curl_setopt($resource, CURLOPT_URL, $path); curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 @@ -130,25 +127,31 @@ class Curl extends ClientAbstracts } curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); - return $resource; + $this->client = $resource; } /** - * @param $curl - * @return ResponseInterface * @throws Exception */ - private function execute($curl): ResponseInterface + private function execute(): void { - $output = curl_exec($curl); - curl_close($curl); + $output = curl_exec($this->client); if ($output === false) { - $response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($curl))); + $response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client))); } else { $response = $this->explode($output); } - return $response; + $this->setBody($response); + } + + + /** + * + */ + public function close(): void + { + curl_close($this->client); } diff --git a/src/IClient.php b/src/IClient.php index ff11ce1..43478f9 100644 --- a/src/IClient.php +++ b/src/IClient.php @@ -15,66 +15,64 @@ interface IClient /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function get(string $path, array $params = []): ResponseInterface; + public function get(string $path, array $params = []): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function post(string $path, array $params = []): ResponseInterface; + public function post(string $path, array $params = []): void; + + + /** + * + */ + public function close(): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function delete(string $path, array $params = []): ResponseInterface; + public function delete(string $path, array $params = []): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function options(string $path, array $params = []): ResponseInterface; + public function options(string $path, array $params = []): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function upload(string $path, array $params = []): ResponseInterface; + public function upload(string $path, array $params = []): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function put(string $path, array $params = []): ResponseInterface; + public function put(string $path, array $params = []): void; /** * @param string $path * @param array $params - * @return ResponseInterface */ - public function head(string $path, array $params = []): ResponseInterface; + public function head(string $path, array $params = []): void; /** * @param string $method * @param string $path * @param array $params - * @return ResponseInterface */ - public function request(string $method, string $path, array $params = []): ResponseInterface; + public function request(string $method, string $path, array $params = []): void; /** @@ -174,4 +172,10 @@ interface IClient * @return static */ public function withContentType(string $contentType): static; + + + /** + * @return mixed + */ + public function getBody(): mixed; }