This commit is contained in:
2023-12-12 15:35:36 +08:00
parent 9814e06e8d
commit 6da3575619
5 changed files with 104 additions and 103 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ namespace Kiri;
use Closure; use Closure;
use CurlHandle;
defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/'); defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/');
@@ -56,7 +57,7 @@ abstract class ClientAbstracts implements IClient
/** /**
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle * @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|CurlHandle
*/ */
protected mixed $client; protected mixed $client;
+4 -4
View File
@@ -26,7 +26,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path * @param $path
* @param array|string $params * @param array|string $params
* @return void * @return void
* @throws Exception * @throws
*/ */
public function request(string $method, $path, array|string $params = []): void public function request(string $method, $path, array|string $params = []): void
{ {
@@ -58,7 +58,7 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
* @throws Exception 使用swoole协程方式请求 * @throws
*/ */
private function coroutine($url, array|string $data = []): void private function coroutine($url, array|string $data = []): void
{ {
@@ -81,7 +81,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path * @param $path
* @param $data * @param $data
* @return void * @return void
* @throws Exception * @throws
*/ */
private function execute($path, $data): void private function execute($path, $data): void
{ {
@@ -100,7 +100,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path * @param $path
* @param $data * @param $data
* @return void * @return void
* @throws Exception * @throws
*/ */
private function retry($path, $data): void private function retry($path, $data): void
{ {
+7 -7
View File
@@ -18,7 +18,7 @@ class CurlClient extends ClientAbstracts
* @param $method * @param $method
* @param $path * @param $path
* @param array|string $params * @param array|string $params
* @throws Exception * @throws
*/ */
public function request($method, $path, array|string $params = []): void public function request($method, $path, array|string $params = []): void
{ {
@@ -39,7 +39,7 @@ class CurlClient extends ClientAbstracts
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
* @throws Exception * @throws
*/ */
private function getCurlHandler($path, $method, $params): void private function getCurlHandler($path, $method, $params): void
{ {
@@ -70,7 +70,7 @@ class CurlClient extends ClientAbstracts
/** /**
* @return void * @return void
* @throws Exception * @throws
*/ */
private function curlHandlerSslSet(): void private function curlHandlerSslSet(): void
{ {
@@ -90,7 +90,7 @@ class CurlClient extends ClientAbstracts
* @param $resource * @param $resource
* @param $path * @param $path
* @param $method * @param $method
* @throws Exception * @throws
*/ */
private function do($resource, $path, $method): void private function do($resource, $path, $method): void
{ {
@@ -143,7 +143,7 @@ class CurlClient extends ClientAbstracts
/** /**
* @throws Exception * @throws
*/ */
private function execute(): void private function execute(): void
{ {
@@ -159,7 +159,7 @@ class CurlClient extends ClientAbstracts
/** /**
* @return void * @return void
* @throws Exception * @throws
*/ */
private function retry(): void private function retry(): void
{ {
@@ -186,7 +186,7 @@ class CurlClient extends ClientAbstracts
/** /**
* @param $output * @param $output
* @return void * @return void
* @throws Exception * @throws
*/ */
private function explode($output): void private function explode($output): void
{ {
+89 -89
View File
@@ -11,99 +11,99 @@ use Exception;
*/ */
class HttpParse class HttpParse
{ {
/** /**
* @param mixed ...$object * @param mixed ...$object
* @return string * @return string
*/ */
private static function getKey(...$object): string private static function getKey(...$object): string
{ {
$first = ''; $first = '';
$tp = []; $tp = [];
foreach ($object as $value) { foreach ($object as $value) {
if ($value === null) { if ($value === null) {
continue; continue;
} }
if (is_array($value)) { if (is_array($value)) {
$value = key($value); $value = key($value);
} }
if ($first === '') { if ($first === '') {
$first = $value; $first = $value;
} else { } else {
$tp[] = $value; $tp[] = $value;
} }
} }
$key = $first . '[' . implode('][', $tp) . ']'; $key = $first . '[' . implode('][', $tp) . ']';
if (count($tp) < 1) { if (count($tp) < 1) {
$key = $first; $key = $first;
} }
return $key; return $key;
} }
/** /**
* @param $data * @param $data
* @return string * @return string
* @throws Exception * @throws
*/ */
public static function parse($data): string public static function parse($data): string
{ {
$tmp = []; $tmp = [];
if (is_string($data)) { if (is_string($data)) {
return $data; return $data;
} }
foreach ($data as $key => $datum) { foreach ($data as $key => $datum) {
if ($datum === null) { if ($datum === null) {
continue; continue;
} }
$tmp[] = static::ifElse($key, $datum); $tmp[] = static::ifElse($key, $datum);
} }
return implode('&', $tmp); return implode('&', $tmp);
} }
/** /**
* @param $t * @param $t
* @param $qt * @param $qt
* @return string * @return string
* @throws Exception * @throws
*/ */
private static function ifElse($t, $qt): string private static function ifElse($t, $qt): string
{ {
if (is_numeric($qt)) { if (is_numeric($qt)) {
return $t . '=' . $qt; return $t . '=' . $qt;
} }
if (is_string($qt)) { if (is_string($qt)) {
$string = $t . '=' . urlencode($qt); $string = $t . '=' . urlencode($qt);
} else { } else {
$string = static::encode($t, $qt); $string = static::encode($t, $qt);
} }
return $string; return $string;
} }
/** /**
* @param mixed ...$object * @param mixed ...$object
* @return string * @return string
* @throws Exception * @throws
*/ */
private static function encode(...$object): string private static function encode(...$object): string
{ {
$ret = []; $ret = [];
$data = $object[count($object) - 1]; $data = $object[count($object) - 1];
$key = static::getKey(...$object); $key = static::getKey(...$object);
foreach ($data as $s => $datum) { foreach ($data as $s => $datum) {
if (is_array($datum)) { if (is_array($datum)) {
$object[count($object) - 1] = $s; $object[count($object) - 1] = $s;
$object[] = $datum; $object[] = $datum;
$string = static::encode(...$object); $string = static::encode(...$object);
} else { } else {
if (is_object($datum)) { if (is_object($datum)) {
throw new Exception('Http body con\'t object.'); throw new Exception('Http body con\'t object.');
} }
$string = $key . '=' . urlencode($datum); $string = $key . '=' . urlencode($datum);
} }
$ret[] = $string; $ret[] = $string;
} }
return implode('&', $ret); return implode('&', $ret);
} }
} }
+2 -2
View File
@@ -22,7 +22,7 @@ class TcpClient
* @param string $host * @param string $host
* @param int $port * @param int $port
* @param int $socket * @param int $socket
* @throws Exception * @throws
*/ */
public function __construct(readonly public string $host, readonly public int $port, readonly public int $socket = SWOOLE_SOCK_TCP) public function __construct(readonly public string $host, readonly public int $port, readonly public int $socket = SWOOLE_SOCK_TCP)
{ {
@@ -32,7 +32,7 @@ class TcpClient
/** /**
* @return void * @return void
* @throws Exception * @throws
*/ */
public function reconnect(): void public function reconnect(): void
{ {