Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 336237d338 | |||
| d075dd73a4 | |||
| 40c946b58e | |||
| 1d4554dbe7 | |||
| 70b4400f95 | |||
| 6f4f4c2beb |
+66
-24
@@ -48,11 +48,60 @@ abstract class ClientAbstracts implements IClient
|
||||
private int $port = 80;
|
||||
|
||||
|
||||
|
||||
private int $statusCode = 200;
|
||||
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $body;
|
||||
|
||||
|
||||
private ?StreamInterface $_data = null;
|
||||
|
||||
private int $connect_timeout = 1;
|
||||
|
||||
|
||||
protected mixed $client;
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function setStatusCode(int $statusCode): void
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getBody(): string|null
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ?string $body
|
||||
*/
|
||||
public function setBody(?string $body): void
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $host
|
||||
* @param $port
|
||||
@@ -67,22 +116,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 +147,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -505,6 +546,7 @@ abstract class ClientAbstracts implements IClient
|
||||
*/
|
||||
protected function mergeParams($newData): string
|
||||
{
|
||||
if (empty($data)) return '';
|
||||
if (!is_string($newData)) {
|
||||
return $this->toRequest($newData);
|
||||
}
|
||||
|
||||
+30
-30
@@ -10,12 +10,9 @@ declare(strict_types=1);
|
||||
namespace Http\Client;
|
||||
|
||||
use Exception;
|
||||
use Http\Message\Response;
|
||||
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 +26,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 +42,21 @@ 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()));
|
||||
$this->setStatusCode($this->client->getStatusCode());
|
||||
$this->setBody($this->client->getBody());
|
||||
} catch (\Throwable $exception) {
|
||||
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
||||
return (new Response())->withStatus(-1)->withHeaders([])
|
||||
->withBody(new Stream(jTraceEx($exception)));
|
||||
$this->setStatusCode(-1);
|
||||
$this->setBody(jTraceEx($exception));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,50 +66,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
|
||||
*/
|
||||
|
||||
+35
-32
@@ -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,34 +127,40 @@ 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)));
|
||||
$this->setStatusCode(404);
|
||||
$this->setBody(curl_error($this->client));
|
||||
} else {
|
||||
$response = $this->explode($output);
|
||||
$this->explode($output);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
curl_close($this->client);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $output
|
||||
* @return ResponseInterface
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function explode($output): ResponseInterface
|
||||
private function explode($output): void
|
||||
{
|
||||
[$header, $body] = explode("\r\n\r\n", $output, 2);
|
||||
if ($header == 'HTTP/1.1 100 Continue') {
|
||||
@@ -167,8 +170,8 @@ class Curl extends ClientAbstracts
|
||||
$header = explode("\r\n", $header);
|
||||
$status = explode(' ', array_shift($header));
|
||||
|
||||
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
|
||||
->withBody(new Stream($body));
|
||||
$this->setStatusCode(intval($status[1]));
|
||||
$this->setBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+20
-16
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user