This commit is contained in:
2023-12-18 18:21:09 +08:00
parent 6da3575619
commit c33adace5c
4 changed files with 71 additions and 89 deletions
+31 -31
View File
@@ -17,13 +17,13 @@ defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(
abstract class ClientAbstracts implements IClient
{
const POST = 'post';
const UPLOAD = 'upload';
const GET = 'get';
const DELETE = 'delete';
const OPTIONS = 'options';
const HEAD = 'head';
const PUT = 'put';
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 = [];
@@ -143,10 +143,10 @@ abstract class ClientAbstracts implements IClient
/**
* @param $bool
* @param bool $bool
* @return $this
*/
public function withVerifyPeer($bool): static
public function withVerifyPeer(bool $bool): static
{
$this->verifyPeer = $bool;
return $this;
@@ -227,11 +227,11 @@ abstract class ClientAbstracts implements IClient
/**
* @param $host
* @param $port
* @param string $host
* @param int $port
* @param false $isSSL
*/
public function __construct($host, $port, bool $isSSL = FALSE)
public function __construct(string $host, int $port, bool $isSSL = FALSE)
{
$this->withHost($host)->withPort($port)->withIsSSL($isSSL);
}
@@ -405,11 +405,11 @@ abstract class ClientAbstracts implements IClient
}
/**
* @param $key
* @param $value
* @param string $key
* @param string|array $value
* @return ClientAbstracts
*/
public function withAddedHeader($key, $value): static
public function withAddedHeader(string $key, string|array $value): static
{
$this->header[$key] = $value;
return $this;
@@ -615,10 +615,10 @@ abstract class ClientAbstracts implements IClient
/**
* @param $host
* @param string $host
* @return string|string[]
*/
protected function replaceHost($host): array|string
protected function replaceHost(string $host): array|string
{
if ($this->isHttp($host)) {
return str_replace('http://', '', $host);
@@ -631,43 +631,43 @@ abstract class ClientAbstracts implements IClient
/**
* @param $url
* @param string $url
* @return false|int
*/
protected function checkIsIp($url): bool|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 $url
* @param string $url
* @return bool
*/
protected function isHttp($url): bool
protected function isHttp(string $url): bool
{
return str_starts_with($url, 'http://');
}
/**
* @param $url
* @param string $url
* @return bool
*/
protected function isHttps($url): bool
protected function isHttps(string $url): bool
{
return str_starts_with($url, 'https://');
}
/**
* @param $newData
* @param array|string $newData
* @return string|null
*/
protected function mergeParams($newData): ?string
protected function mergeParams(array|string $newData): ?string
{
if (is_array($newData)) {
return json_encode($newData, JSON_UNESCAPED_UNICODE);
}
return (string)$newData;
return $newData;
}
@@ -701,12 +701,12 @@ abstract class ClientAbstracts implements IClient
}
/**
* @param $arr
* @param array|string $arr
*
* @return array|string
* 将请求参数进行编码
*/
protected function paramEncode($arr): array|string
protected function paramEncode(array|string $arr): array|string
{
if (!is_array($arr)) {
return $arr;
@@ -733,11 +733,11 @@ abstract class ClientAbstracts implements IClient
/**
* @param $path
* @param $params
* @param string $path
* @param array|string $params
* @return string
*/
protected function joinGetParams($path, $params): string
protected function joinGetParams(string $path, array|string $params): string
{
if (empty($params)) {
return $path;
+16 -19
View File
@@ -47,20 +47,19 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $path
* @param string $path
* @return $this
*/
public function withCAInfo($path): static
public function withCAInfo(string $path): static
{
return $this;
}
/**
* @param $url
* @param string $url
* @param array|string $data
* @throws
*/
private function coroutine($url, array|string $data = []): void
private function coroutine(string $url, array|string $data = []): void
{
try {
$this->generate_client($this->getHost(), $this->isSSL());
@@ -78,12 +77,11 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $path
* @param $data
* @param string $path
* @param array|string $data
* @return void
* @throws
*/
private function execute($path, $data): void
private function execute(string $path, array|string $data): void
{
$this->client->execute($this->setParams($path, $data));
if (in_array($this->client->getStatusCode(), [502, 404])) {
@@ -97,12 +95,11 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $path
* @param $data
* @param string $path
* @param array|string $data
* @return void
* @throws
*/
private function retry($path, $data): void
private function retry(string $path, array|string $data): void
{
if (($this->num += 1) <= $this->retryNum) {
sleep($this->retryTimeout);
@@ -115,10 +112,10 @@ class CoroutineClient extends ClientAbstracts
}
/**
* @param $host
* @param $isHttps
* @param string $host
* @param bool $isHttps
*/
private function generate_client($host, $isHttps): void
private function generate_client(string $host, bool $isHttps): void
{
if ($isHttps || $this->isSSL()) {
$this->client = new SwowClient($host, 443, true);
@@ -135,11 +132,11 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $path
* @param $data
* @param string $path
* @param mixed $data
* @return string
*/
private function setParams($path, $data): string
private function setParams(string $path, mixed $data): string
{
$content = $this->getData();
if (!empty($content)) {
+21 -35
View File
@@ -4,9 +4,6 @@ declare(strict_types=1);
namespace Kiri;
use Exception;
/**
* Class CurlClient
* @package Http\Handler\Client
@@ -14,13 +11,14 @@ use Exception;
class CurlClient extends ClientAbstracts
{
/**
* @param $method
* @param $path
* @param string $method
* @param string $path
* @param array|string $params
* @throws
* @return void
*/
public function request($method, $path, array|string $params = []): void
public function request(string $method, string $path, array|string $params = []): void
{
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
@@ -36,12 +34,12 @@ class CurlClient extends ClientAbstracts
/**
* @param $path
* @param $method
* @param string $path
* @param string $method
* @param $params
* @throws
* @return void
*/
private function getCurlHandler($path, $method, $params): void
private function getCurlHandler(string $path, string $method, $params): void
{
$host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost();
if ($this->getPort() != 443 && $this->getPort() != 80) {
@@ -87,12 +85,12 @@ class CurlClient extends ClientAbstracts
/**
* @param $resource
* @param $path
* @param $method
* @throws
* @param mixed $resource
* @param string $path
* @param string $method
* @return void
*/
private function do($resource, $path, $method): void
private function do(mixed $resource, string $path, string $method): void
{
curl_setopt($resource, CURLOPT_URL, $path);
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
@@ -128,14 +126,17 @@ class CurlClient extends ClientAbstracts
}
/**
* @var string
*/
private string $caPath = '';
/**
* @param $path
* @param string $path
* @return $this
*/
public function withCAInfo($path): static
public function withCAInfo(string $path): static
{
$this->caPath = $path;
return $this;
@@ -184,11 +185,11 @@ class CurlClient extends ClientAbstracts
/**
* @param $output
* @param string $output
* @return void
* @throws
*/
private function explode($output): void
private function explode(string $output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
@@ -208,21 +209,6 @@ class CurlClient extends ClientAbstracts
}
}
/**
* @param $headers
* @return array
*/
private function headerFormat($headers): array
{
$_tmp = [];
foreach ($headers as $val) {
$trim = explode(': ', trim($val));
$_tmp[strtolower($trim[0])] = [$trim[1] ?? ''];
}
return $_tmp;
}
/**
* @return array
+3 -4
View File
@@ -40,11 +40,10 @@ class HttpParse
}
/**
* @param $data
* @param array|string $data
* @return string
* @throws
*/
public static function parse($data): string
public static function parse(array|string $data): string
{
$tmp = [];
if (is_string($data)) {
@@ -65,7 +64,7 @@ class HttpParse
* @return string
* @throws
*/
private static function ifElse($t, $qt): string
private static function ifElse(string $t, mixed $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;