diff --git a/ClientAbstracts.php b/ClientAbstracts.php index 1bad621..40b467d 100644 --- a/ClientAbstracts.php +++ b/ClientAbstracts.php @@ -17,741 +17,655 @@ defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+( abstract class ClientAbstracts implements IClient { - const string POST = 'post'; - const string UPLOAD = 'upload'; - const string GET = 'get'; - const string DELETE = 'delete'; - const string OPTIONS = 'options'; - const string HEAD = 'head'; - const string PUT = 'put'; - - private string $host = ''; - private array $header = []; - private int $timeout = 0; - private string $method = 'get'; - private bool $isSSL = FALSE; - private string $agent = ''; - private string $ssl_cert_file = ''; - private string $ssl_key_file = ''; - private string $ca = ''; - private int $port = 80; - protected int $num = 0; - private ?array $_responseHeader = []; - private int $statusCode = 200; - protected int $retryNum = 0; - protected int $retryTimeout = 0; - private bool $verifyPeer = TRUE; - private string $proxyHost = ''; - private int $proxyPort = 0; - - - /** - * @var string|null - */ - protected ?string $body; - - - private string|array|null $_data = NULL; - - private int $connect_timeout = 1; - - - /** - * @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|CurlHandle - */ - protected mixed $client; - - - /** - * @param int $retryNum - * @return $this - */ - public function withRetryNum(int $retryNum): static - { - $this->retryNum = $retryNum; - return $this; - } - - - /** - * @return string - */ - public function getProxyHost(): string - { - return $this->proxyHost; - } - - - /** - * @return string - */ - public function getProxyPort(): string - { - return $this->proxyPort; - } - - - /** - * @param string $proxy - * @return ClientAbstracts - */ - public function withProxyHost(string $proxy): static - { - $this->proxyHost = $proxy; - return $this; - } - - - /** - * @param int $proxy - * @return ClientAbstracts - */ - public function withProxyPort(int $proxy): static - { - $this->proxyPort = $proxy; - return $this; - } - - - /** - * @param int $retryTimeout - * @return $this - */ - public function withRetryTimeout(int $retryTimeout): static - { - $this->retryTimeout = $retryTimeout; - return $this; - } - - - /** - * @return int - */ - public function getRetryNum(): int - { - return $this->retryNum; - } - - - /** - * @return int - */ - public function getRetryTimeout(): int - { - return $this->retryTimeout; - } - - - /** - * @param bool $bool - * @return $this - */ - public function withVerifyPeer(bool $bool): static - { - $this->verifyPeer = $bool; - return $this; - } - - - /** - * @return bool - */ - public function getVerifyPeer(): bool - { - return $this->verifyPeer; - } - - - /** - * @return int - */ - public function getStatusCode(): int - { - return $this->statusCode; - } - - - /** - * @return array - */ - public function getResponseHeaders(): array - { - return $this->_responseHeader; - } - - - /** - * @param string $key - * @return string|int|null - */ - public function getResponseHeader(string $key): null|string|int - { - return $this->_responseHeader[$key] ?? NULL; - } - - - /** - * @param null|array $responseHeader - */ - public function setResponseHeader(?array $responseHeader): void - { - $this->_responseHeader = $responseHeader; - } - - - /** - * @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 string $host - * @param int $port - * @param false $isSSL - */ - public function __construct(string $host, int $port, bool $isSSL = FALSE) - { - $this->withHost($host)->withPort($port)->withIsSSL($isSSL); - } - - - /** - * @param string $path - * @param array|string $params - */ - public function post(string $path, array|string $params = []): void - { - $this->request(self::POST, $path, $params); - } - - - /** - * @param string $path - * @param array|string $params - */ - public function put(string $path, array|string $params = []): void - { - $this->request(self::PUT, $path, $params); - } - - - /** - * @param string $contentType - * @return ClientAbstracts - */ - public function withContentType(string $contentType): static - { - $this->header['Content-Type'] = $contentType; - return $this; - } - - - /** - * @param string $path - * @param array|string $params - */ - public function head(string $path, array|string $params = []): void - { - $this->request(self::HEAD, $path, $params); - } - - - /** - * @param string $path - * @param array|string $params - */ - public function get(string $path, array|string $params = []): void - { - if (is_array($params)) { - $params = http_build_query($params); - } - $this->request(self::GET, $path, $params); - } - - /** - * @param string $path - * @param array|string $params - */ - public function option(string $path, array|string $params = []): void - { - $this->request(self::OPTIONS, $path, $params); - } - - /** - * @param string $path - * @param array|string $params - */ - public function delete(string $path, array|string $params = []): void - { - $this->request(self::DELETE, $path, $params); - } - - /** - * @param string $path - * @param array|string $params - */ - public function options(string $path, array|string $params = []): void - { - $this->request(self::OPTIONS, $path, $params); - - } - - /** - * @param string $path - * @param array|string $params - */ - public function upload(string $path, array|string $params = []): void - { - $this->request(self::UPLOAD, $path, $params); - } - - - /** - * @return string - */ - public function getHost(): string - { - return $this->host; - } - - /** - * @return int - */ - protected function getHostPort(): int - { - if (!empty($this->getPort())) { - return $this->getPort(); - } - $port = 80; - if ($this->isSSL()) $port = 443; - return $port; - } - - - /** - * @param string $host - * @return ClientAbstracts - */ - protected function withHost(string $host): static - { - $this->host = $host; - return $this; - } - - /** - * @return array - */ - public function getHeader(): array - { - return $this->header; - } - - - /** - * @return mixed|null - */ - public function getContentType(): ?string - { - return $this->header['Content-Type'] ?? $this->header['content-type'] ?? NULL; - } - - - /** - * @param array $header - * @return ClientAbstracts - */ - public function withHeader(array $header): static - { - $this->header = $header; - return $this; - } - - - /** - * @param array $header - * @return ClientAbstracts - */ - public function withHeaders(array $header): static - { - if (empty($header)) { - return $this; - } - foreach ($header as $key => $val) { - $this->header[$key] = $val; - } - return $this; - } - - /** - * @param string $key - * @param string|array $value - * @return ClientAbstracts - */ - public function withAddedHeader(string $key, string|array $value): static - { - $this->header[$key] = $value; - return $this; - } - - /** - * @return int - */ - public function getTimeout(): int - { - return $this->timeout; - } - - /** - * @param int $value - * @return ClientAbstracts - */ - public function withTimeout(int $value): static - { - $this->timeout = $value; - return $this; - } - - - /** - * @param Closure|null $value - * @return ClientAbstracts - */ - public function withCallback(?Closure $value): static - { - return $this; - } - - /** - * @return string - */ - public function getMethod(): string - { - return $this->method; - } - - /** - * @param string $value - * @return static - */ - public function withMethod(string $value): static - { - $this->method = $value; - return $this; - } - - /** - * @return bool - */ - public function isSSL(): bool - { - return $this->isSSL; - } - - /** - * @param bool $isSSL - * @return ClientAbstracts - */ - public function withIsSSL(bool $isSSL): static - { - $this->isSSL = $isSSL; - return $this; - } - - /** - * @return string - */ - public function getAgent(): string - { - return $this->agent; - } - - /** - * @param string $agent - * @return ClientAbstracts - */ - public function withAgent(string $agent): static - { - $this->agent = $agent; - return $this; - } - - - /** - * @return string - */ - public function getSslCertFile(): string - { - return $this->ssl_cert_file; - } - - /** - * @param string $ssl_cert_file - * @return ClientAbstracts - */ - public function withSslCertFile(string $ssl_cert_file): static - { - $this->ssl_cert_file = $ssl_cert_file; - return $this; - } - - /** - * @return string - */ - public function getSslKeyFile(): string - { - return $this->ssl_key_file; - } - - /** - * @param string $ssl_key_file - * @return ClientAbstracts - */ - public function withSslKeyFile(string $ssl_key_file): static - { - $this->ssl_key_file = $ssl_key_file; - return $this; - } - - /** - * @return string - */ - public function getCa(): string - { - return $this->ca; - } - - /** - * @param string $ssl_key_file - * @return static - */ - public function withCa(string $ssl_key_file): static - { - $this->ca = $ssl_key_file; - return $this; - } - - /** - * @return int - */ - public function getPort(): int - { - if ($this->isSSL()) { - return 443; - } - if (empty($this->port)) { - return 80; - } - return $this->port; - } - - /** - * @param int $port - * @return ClientAbstracts - */ - private function withPort(int $port): static - { - $this->port = $port; - return $this; - } - - - /** - * @return string|null - */ - public function getData(): ?string - { - return $this->_data; - } - - /** - * @param string|null $data - * @return ClientAbstracts - */ - public function withBody(?string $data): static - { - $this->_data = $data; - return $this; - } - - /** - * @return int - */ - public function getConnectTimeout(): int - { - return $this->connect_timeout; - } - - /** - * @param int $connect_timeout - * @return ClientAbstracts - */ - public function withConnectTimeout(int $connect_timeout): static - { - $this->connect_timeout = $connect_timeout; - return $this; - } - - - /** - * @param string $host - * @return string|string[] - */ - protected function replaceHost(string $host): array|string - { - if ($this->isHttp($host)) { - return str_replace('http://', '', $host); - } - if ($this->isHttps($host)) { - return str_replace('https://', '', $host); - } - return $host; - } - - - /** - * @param string $url - * @return false|int - */ - protected function checkIsIp(string $url): bool|int - { - return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url); - } - - /** - * @param string $url - * @return bool - */ - protected function isHttp(string $url): bool - { - return str_starts_with($url, 'http://'); - } - - /** - * @param string $url - * @return bool - */ - protected function isHttps(string $url): bool - { - return str_starts_with($url, 'https://'); - } - - - /** - * @param array|string $newData - * @return string|null - */ - protected function mergeParams(array|string $newData): ?string - { - if (is_array($newData)) { - return json_encode($newData, JSON_UNESCAPED_UNICODE); - } - return $newData; - } - - - /** - * @return bool - * check isPost Request - */ - protected function isPost(): bool - { - return strtolower($this->method) === self::POST; - } - - /** - * @return bool - * check isPost Request - */ - protected function isUpload(): bool - { - return strtolower($this->method) === self::UPLOAD; - } - - - /** - * @return bool - * - * check isGet Request - */ - protected function isGet(): bool - { - return strtolower($this->method) === self::GET; - } - - /** - * @param array|string $arr - * - * @return array|string - * 将请求参数进行编码 - */ - protected function paramEncode(array|string $arr): array|string - { - if (!is_array($arr)) { - return $arr; - } - $_tmp = []; - foreach ($arr as $Key => $val) { - $_tmp[$Key] = $val; - } - if ($this->isGet()) { - return http_build_query($_tmp); - } - return $_tmp; - } - - - /** - * @param string $string - * @return array - */ - protected function matchHost(string $string): array - { - return [$this->host, $this->isSSL(), $string]; - } - - - /** - * @param string $path - * @param array|string $params - * @return string - */ - protected function joinGetParams(string $path, array|string $params): string - { - if (empty($params)) { - return $path; - } - if (!is_string($params)) { - $params = http_build_query($params); - } - if (str_contains($path, '?')) { - [$path, $getParams] = explode('?', $path); - } - if (empty($getParams)) { - return $path . '?' . $params; - } - return $path . '?' . $params . '&' . $getParams; - } + const string POST = 'post'; + const string UPLOAD = 'upload'; + const string GET = 'get'; + const string DELETE = 'delete'; + const string OPTIONS = 'options'; + const string HEAD = 'head'; + const string PUT = 'put'; + + public string $host = '' { + get { + return $this->host; + } + } + protected array $header = [] { + &get { + return $this->header; + } + } + protected int $timeout = 0 { + get { + return $this->timeout; + } + } + protected string $method = 'get' { + get { + return $this->method; + } + } + protected bool $isSSL = FALSE { + get { + return $this->isSSL; + } + } + protected string $agent = '' { + get { + return $this->agent; + } + } + public string $ssl_cert_file = '' { + get { + return $this->ssl_cert_file; + } + } + public string $ssl_key_file = '' { + get { + return $this->ssl_key_file; + } + } + public string $ca = '' { + get { + return $this->ca; + } + } + private int $port = 80; + protected int $num = 0; + private ?array $_responseHeader = [] { + &get { + return $this->_responseHeader; + } + } + public int $statusCode = 200 { + get { + return $this->statusCode; + } + set { + $this->statusCode = $value; + } + } + protected int $retryNum = 0 { + get { + return $this->retryNum; + } + } + protected int $retryTimeout = 0 { + get { + return $this->retryTimeout; + } + } + private bool $verifyPeer = TRUE { + get { + return $this->verifyPeer; + } + } + public string $proxyHost = '' { + get { + return $this->proxyHost; + } + } + public int $proxyPort = 0 { + get { + return $this->proxyPort; + } + } + + + /** + * @var string|null + */ + public ?string $body { + get { + return $this->body; + } + set { + $this->body = $value; + } + } + + + protected string|array|null $_data = NULL { + get { + return $this->_data; + } + } + + public int $connect_timeout = 1 { + get { + return $this->connect_timeout; + } + } + + + /** + * @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|CurlHandle + */ + protected mixed $client; + + + /** + * @param int $retryNum + * @return $this + */ + public function withRetryNum(int $retryNum): static + { + $this->retryNum = $retryNum; + return $this; + } + + + /** + * @param string $proxy + * @return ClientAbstracts + */ + public function withProxyHost(string $proxy): static + { + $this->proxyHost = $proxy; + return $this; + } + + + /** + * @param int $proxy + * @return ClientAbstracts + */ + public function withProxyPort(int $proxy): static + { + $this->proxyPort = $proxy; + return $this; + } + + + /** + * @param int $retryTimeout + * @return $this + */ + public function withRetryTimeout(int $retryTimeout): static + { + $this->retryTimeout = $retryTimeout; + return $this; + } + + + /** + * @param bool $bool + * @return $this + */ + public function withVerifyPeer(bool $bool): static + { + $this->verifyPeer = $bool; + return $this; + } + + + /** + * @return array + */ + public function getResponseHeaders(): array + { + return $this->_responseHeader; + } + + + /** + * @param string $key + * @return string|int|null + */ + public function getResponseHeader(string $key): null|string|int + { + return $this->_responseHeader[$key] ?? NULL; + } + + + /** + * @param null|array $responseHeader + */ + public function setResponseHeader(?array $responseHeader): void + { + $this->_responseHeader = $responseHeader; + } + + + /** + * @param string $host + * @param int $port + * @param false $isSSL + */ + public function __construct(string $host, int $port, bool $isSSL = FALSE) + { + $this->withHost($host)->withPort($port)->withIsSSL($isSSL); + } + + + /** + * @param string $path + * @param array|string $params + */ + public function post(string $path, array|string $params = []): void + { + $this->request(self::POST, $path, $params); + } + + + /** + * @param string $path + * @param array|string $params + */ + public function put(string $path, array|string $params = []): void + { + $this->request(self::PUT, $path, $params); + } + + + /** + * @param string $contentType + * @return ClientAbstracts + */ + public function withContentType(string $contentType): static + { + $this->header['Content-Type'] = $contentType; + return $this; + } + + + /** + * @param string $path + * @param array|string $params + */ + public function head(string $path, array|string $params = []): void + { + $this->request(self::HEAD, $path, $params); + } + + + /** + * @param string $path + * @param array|string $params + */ + public function get(string $path, array|string $params = []): void + { + if (is_array($params)) { + $params = http_build_query($params); + } + $this->request(self::GET, $path, $params); + } + + /** + * @param string $path + * @param array|string $params + */ + public function option(string $path, array|string $params = []): void + { + $this->request(self::OPTIONS, $path, $params); + } + + /** + * @param string $path + * @param array|string $params + */ + public function delete(string $path, array|string $params = []): void + { + $this->request(self::DELETE, $path, $params); + } + + /** + * @param string $path + * @param array|string $params + */ + public function options(string $path, array|string $params = []): void + { + $this->request(self::OPTIONS, $path, $params); + + } + + /** + * @param string $path + * @param array|string $params + */ + public function upload(string $path, array|string $params = []): void + { + $this->request(self::UPLOAD, $path, $params); + } + + + /** + * @return int + */ + protected function getHostPort(): int + { + if (!empty($this->getPort())) { + return $this->getPort(); + } + $port = 80; + if ($this->isSSL) + $port = 443; + return $port; + } + + + /** + * @param string $host + * @return ClientAbstracts + */ + protected function withHost(string $host): static + { + $this->host = $host; + return $this; + } + + + /** + * @return mixed|null + */ + public function getContentType(): ?string + { + return $this->header['Content-Type'] ?? $this->header['content-type'] ?? NULL; + } + + + /** + * @param array $header + * @return ClientAbstracts + */ + public function withHeader(array $header): static + { + $this->header = $header; + return $this; + } + + + /** + * @param array $header + * @return ClientAbstracts + */ + public function withHeaders(array $header): static + { + if (empty($header)) { + return $this; + } + foreach ($header as $key => $val) { + $this->header[$key] = $val; + } + return $this; + } + + /** + * @param string $key + * @param string|array $value + * @return ClientAbstracts + */ + public function withAddedHeader(string $key, string|array $value): static + { + $this->header[$key] = $value; + return $this; + } + + /** + * @param int $value + * @return ClientAbstracts + */ + public function withTimeout(int $value): static + { + $this->timeout = $value; + return $this; + } + + + /** + * @param Closure|null $value + * @return ClientAbstracts + */ + public function withCallback(?Closure $value): static + { + return $this; + } + + /** + * @param string $value + * @return static + */ + public function withMethod(string $value): static + { + $this->method = $value; + return $this; + } + + /** + * @param bool $isSSL + * @return ClientAbstracts + */ + public function withIsSSL(bool $isSSL): static + { + $this->isSSL = $isSSL; + return $this; + } + + /** + * @param string $agent + * @return ClientAbstracts + */ + public function withAgent(string $agent): static + { + $this->agent = $agent; + return $this; + } + + + /** + * @param string $ssl_cert_file + * @return ClientAbstracts + */ + public function withSslCertFile(string $ssl_cert_file): static + { + $this->ssl_cert_file = $ssl_cert_file; + return $this; + } + + /** + * @param string $ssl_key_file + * @return ClientAbstracts + */ + public function withSslKeyFile(string $ssl_key_file): static + { + $this->ssl_key_file = $ssl_key_file; + return $this; + } + + /** + * @param string $ssl_key_file + * @return static + */ + public function withCa(string $ssl_key_file): static + { + $this->ca = $ssl_key_file; + return $this; + } + + /** + * @return int + */ + public function getPort(): int + { + if ($this->isSSL) { + return 443; + } + if (empty($this->port)) { + return 80; + } + return $this->port; + } + + /** + * @param int $port + * @return ClientAbstracts + */ + private function withPort(int $port): static + { + $this->port = $port; + return $this; + } + + + /** + * @param string|null $data + * @return ClientAbstracts + */ + public function withBody(?string $data): static + { + $this->_data = $data; + return $this; + } + + /** + * @param int $connect_timeout + * @return ClientAbstracts + */ + public function withConnectTimeout(int $connect_timeout): static + { + $this->connect_timeout = $connect_timeout; + return $this; + } + + + /** + * @param string $host + * @return string|string[] + */ + protected function replaceHost(string $host): array|string + { + if ($this->isHttp($host)) { + return str_replace('http://', '', $host); + } + if ($this->isHttps($host)) { + return str_replace('https://', '', $host); + } + return $host; + } + + + /** + * @param string $url + * @return false|int + */ + protected function checkIsIp(string $url): bool|int + { + return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url); + } + + /** + * @param string $url + * @return bool + */ + protected function isHttp(string $url): bool + { + return str_starts_with($url, 'http://'); + } + + /** + * @param string $url + * @return bool + */ + protected function isHttps(string $url): bool + { + return str_starts_with($url, 'https://'); + } + + + /** + * @param array|string $newData + * @return string|null + */ + protected function mergeParams(array|string $newData): ?string + { + if (is_array($newData)) { + return json_encode($newData, JSON_UNESCAPED_UNICODE); + } + return $newData; + } + + + /** + * @return bool + * check isPost Request + */ + protected function isPost(): bool + { + return strtolower($this->method) === self::POST; + } + + /** + * @return bool + * check isPost Request + */ + protected function isUpload(): bool + { + return strtolower($this->method) === self::UPLOAD; + } + + + /** + * @return bool + * + * check isGet Request + */ + protected function isGet(): bool + { + return strtolower($this->method) === self::GET; + } + + /** + * @param array|string $arr + * + * @return array|string + * 将请求参数进行编码 + */ + protected function paramEncode(array|string $arr): array|string + { + if (!is_array($arr)) { + return $arr; + } + $_tmp = []; + foreach ($arr as $Key => $val) { + $_tmp[$Key] = $val; + } + if ($this->isGet()) { + return http_build_query($_tmp); + } + return $_tmp; + } + + + /** + * @param string $string + * @return array + */ + protected function matchHost(string $string): array + { + return [$this->host, $this->isSSL, $string]; + } + + + /** + * @param string $path + * @param array|string $params + * @return string + */ + protected function joinGetParams(string $path, array|string $params): string + { + if (empty($params)) { + return $path; + } + if (!is_string($params)) { + $params = http_build_query($params); + } + if (str_contains($path, '?')) { + [$path, $getParams] = explode('?', $path); + } + if (empty($getParams)) { + return $path . '?' . $params; + } + return $path . '?' . $params . '&' . $getParams; + } } diff --git a/CoroutineClient.php b/CoroutineClient.php index 71b8e82..21c9659 100644 --- a/CoroutineClient.php +++ b/CoroutineClient.php @@ -34,7 +34,7 @@ class CoroutineClient extends ClientAbstracts $path = '/' . $path; } - $host = $this->getHost(); + $host = $this->host; if (!preg_match('/(\d{1,3}\.){3}\d{1,3}/', $host)) { $this->withAddedHeader('Host', $host); } @@ -62,15 +62,15 @@ class CoroutineClient extends ClientAbstracts private function coroutine(string $url, array|string $data = []): void { try { - $this->generate_client($this->getHost(), $this->isSSL()); + $this->generate_client($this->host, $this->isSSL); if ($this->client->statusCode < 0) { throw new Exception($this->client->errMsg); } $this->execute($url, $data); } catch (\Throwable $exception) { - $this->setStatusCode(-1); - $this->setBody(json_encode(['code' => 500, 'message' => $exception->getMessage()])); + $this->statusCode = -1; + $this->body = json_encode(['code' => 500, 'message' => $exception->getMessage()]); } } @@ -86,8 +86,8 @@ class CoroutineClient extends ClientAbstracts if (in_array($this->client->getStatusCode(), [502, 404])) { $this->retry($path, $data); } else { - $this->setStatusCode($this->client->getStatusCode()); - $this->setBody($this->client->getBody()); + $this->statusCode = $this->client->getStatusCode(); + $this->body = $this->client->getBody(); $this->setResponseHeader($this->client->headers); } } @@ -105,8 +105,8 @@ class CoroutineClient extends ClientAbstracts $this->execute($path, $data); } else { - $this->setStatusCode($this->client->statusCode); - $this->setBody($this->client->errMsg); + $this->statusCode = $this->client->statusCode; + $this->body = $this->client->errMsg; } } @@ -116,17 +116,17 @@ class CoroutineClient extends ClientAbstracts */ private function generate_client(string $host, bool $isHttps): void { - if ($isHttps || $this->isSSL()) { + if ($isHttps || $this->isSSL) { $this->client = new SwowClient($host, 443, true); } else { $this->client = new SwowClient($host, $this->getPort(), false); } $this->client->set($this->settings()); - if (!empty($this->getAgent())) { - $this->withAddedHeader('User-Agent', $this->getAgent()); + if (!empty($this->agent)) { + $this->withAddedHeader('User-Agent', $this->agent); } - $this->client->setHeaders($this->getHeader()); - $this->client->setMethod(strtoupper($this->getMethod())); + $this->client->setHeaders($this->header); + $this->client->setMethod(strtoupper($this->method)); } @@ -137,7 +137,7 @@ class CoroutineClient extends ClientAbstracts */ private function setParams(string $path, mixed $data): string { - $content = $this->getData(); + $content = $this->_data; if (!empty($content)) { $this->client->setData($content); } diff --git a/CurlClient.php b/CurlClient.php index 85c5eb8..313654b 100644 --- a/CurlClient.php +++ b/CurlClient.php @@ -44,15 +44,15 @@ class CurlClient extends ClientAbstracts */ private function getCurlHandler(string $path, string $method, $params): void { - $host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost(); + $host = $this->isSSL ? 'https://' . $this->host : 'http://' . $this->host; if ($this->getPort() != 443 && $this->getPort() != 80) { $host .= ':' . $this->getPort(); } $this->do(curl_init($host . $path), $host . $path, $method); - if ($this->isSSL()) { + if ($this->isSSL) { $this->curlHandlerSslSet(); } - $contents = $this->getData(); + $contents = $this->_data; if (empty($params) && empty($contents)) { return; } @@ -75,14 +75,14 @@ class CurlClient extends ClientAbstracts */ private function curlHandlerSslSet(): void { - if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) { - curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile()); + if (!empty($this->ssl_key_file) && file_exists($this->ssl_key_file)) { + curl_setopt($this->client, CURLOPT_SSLKEY, $this->ssl_key_file); } - if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) { - curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile()); + if (!empty($this->ssl_cert_file) && file_exists($this->ssl_cert_file)) { + curl_setopt($this->client, CURLOPT_SSLCERT, $this->ssl_cert_file); } - if (!empty($this->getCa()) && file_exists($this->getCa())) { - curl_setopt($this->client, CURLOPT_CAINFO, $this->getCa()); + if (!empty($this->ca) && file_exists($this->ca)) { + curl_setopt($this->client, CURLOPT_CAINFO, $this->ca); } } @@ -97,8 +97,8 @@ class CurlClient extends ClientAbstracts private function do(mixed $resource, string $path, string $method): void { curl_setopt($resource, CURLOPT_URL, $path); - curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 - curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置 + curl_setopt($resource, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 + curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout); // 超时设置 curl_setopt($resource, CURLOPT_HEADER, TRUE); curl_setopt($resource, CURLOPT_FAILONERROR, TRUE); curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); @@ -107,8 +107,8 @@ class CurlClient extends ClientAbstracts } curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE); curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE); - if (!empty($this->getAgent())) { - curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); + if (!empty($this->agent)) { + curl_setopt($resource, CURLOPT_USERAGENT, $this->agent); } curl_setopt($resource, CURLOPT_NOBODY, FALSE); curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 @@ -117,7 +117,7 @@ class CurlClient extends ClientAbstracts if ($method === self::POST || $method == self::UPLOAD) { curl_setopt($resource, CURLOPT_POST, 1); } - [$proxy, $port] = [$this->getProxyHost(), $this->getProxyPort()]; + [$proxy, $port] = [$this->proxyHost, $this->proxyPort]; if (!empty($proxy) && $port > 0) { curl_setopt($resource, CURLOPT_PROXYPORT, $port); curl_setopt($resource, CURLOPT_PROXY, $proxy); @@ -157,8 +157,8 @@ class CurlClient extends ClientAbstracts if ($output !== FALSE) { $this->explode($output); } else { - $this->setStatusCode(curl_errno($this->client)); - $this->setBody(curl_error($this->client)); + $this->statusCode = curl_errno($this->client); + $this->body = curl_error($this->client); } $this->close(); } @@ -175,8 +175,8 @@ class CurlClient extends ClientAbstracts $this->execute(); } else { - $this->setStatusCode(curl_errno($this->client)); - $this->setBody(curl_error($this->client)); + $this->statusCode = curl_errno($this->client); + $this->body = curl_error($this->client); } } @@ -207,8 +207,8 @@ class CurlClient extends ClientAbstracts if (in_array($statusCode, [502, 404])) { $this->retry(); } else { - $this->setStatusCode($statusCode); - $this->setBody(substr($output, $headerSize)); + $this->statusCode = $statusCode; + $this->body = substr($output, $headerSize); $this->setResponseHeader(explode("\r\n", $header)); } } @@ -220,7 +220,7 @@ class CurlClient extends ClientAbstracts private function parseHeaderMat(): array { $headers = []; - foreach ($this->getHeader() as $key => $val) { + foreach ($this->header as $key => $val) { $headers[$key] = $key . ': ' . $val; } return array_values($headers); diff --git a/IClient.php b/IClient.php index 96daea5..7e6dbba 100644 --- a/IClient.php +++ b/IClient.php @@ -172,8 +172,7 @@ interface IClient public function withContentType(string $contentType): static; - /** - * @return mixed - */ - public function getBody(): mixed; + public null|string $body { + get; + } } diff --git a/TSwooleClient.php b/TSwooleClient.php index fbfb625..31abb3a 100644 --- a/TSwooleClient.php +++ b/TSwooleClient.php @@ -11,16 +11,16 @@ trait TSwooleClient */ private function settings(): array { - $sslCert = $this->getSslCertFile(); - $sslKey = $this->getSslKeyFile(); - $sslCa = $this->getCa(); + $sslCert = $this->ssl_cert_file; + $sslKey = $this->ssl_key_file; + $sslCa = $this->ca; $params = []; - if ($this->getConnectTimeout() > 0) { - $params['timeout'] = $this->getConnectTimeout(); + if ($this->connect_timeout > 0) { + $params['timeout'] = $this->connect_timeout; } - [$proxy, $port] = [$this->getProxyHost(), $this->getProxyPort()]; + [$proxy, $port] = [$this->proxyHost, $this->proxyPort]; if (!empty($proxy) && $port > 0) { $params['http_proxy_host'] = $proxy; $params['http_proxy_port'] = $port; @@ -29,9 +29,9 @@ trait TSwooleClient return $params; } - $params['ssl_host_name'] = $this->getHost(); - $params['ssl_cert_file'] = $this->getSslCertFile(); - $params['ssl_key_file'] = $this->getSslKeyFile(); + $params['ssl_host_name'] = $this->host; + $params['ssl_cert_file'] = $this->ssl_cert_file; + $params['ssl_key_file'] = $this->ssl_key_file; $params['ssl_verify_peer'] = TRUE; $params['ssl_cafile'] = $sslCa; diff --git a/composer.json b/composer.json index 7399c95..077bd5c 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ ], "license": "MIT", "require": { - "php": ">=8.4", + "php": ">=8.5", "ext-json": "*", "ext-swoole": "*" },