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 CurlHandle;
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;
+4 -4
View File
@@ -26,7 +26,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path
* @param array|string $params
* @return void
* @throws Exception
* @throws
*/
public function request(string $method, $path, array|string $params = []): void
{
@@ -58,7 +58,7 @@ class CoroutineClient extends ClientAbstracts
/**
* @param $url
* @param array|string $data
* @throws Exception 使用swoole协程方式请求
* @throws
*/
private function coroutine($url, array|string $data = []): void
{
@@ -81,7 +81,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path
* @param $data
* @return void
* @throws Exception
* @throws
*/
private function execute($path, $data): void
{
@@ -100,7 +100,7 @@ class CoroutineClient extends ClientAbstracts
* @param $path
* @param $data
* @return void
* @throws Exception
* @throws
*/
private function retry($path, $data): void
{
+7 -7
View File
@@ -18,7 +18,7 @@ class CurlClient extends ClientAbstracts
* @param $method
* @param $path
* @param array|string $params
* @throws Exception
* @throws
*/
public function request($method, $path, array|string $params = []): void
{
@@ -39,7 +39,7 @@ class CurlClient extends ClientAbstracts
* @param $path
* @param $method
* @param $params
* @throws Exception
* @throws
*/
private function getCurlHandler($path, $method, $params): void
{
@@ -70,7 +70,7 @@ class CurlClient extends ClientAbstracts
/**
* @return void
* @throws Exception
* @throws
*/
private function curlHandlerSslSet(): void
{
@@ -90,7 +90,7 @@ class CurlClient extends ClientAbstracts
* @param $resource
* @param $path
* @param $method
* @throws Exception
* @throws
*/
private function do($resource, $path, $method): void
{
@@ -143,7 +143,7 @@ class CurlClient extends ClientAbstracts
/**
* @throws Exception
* @throws
*/
private function execute(): void
{
@@ -159,7 +159,7 @@ class CurlClient extends ClientAbstracts
/**
* @return void
* @throws Exception
* @throws
*/
private function retry(): void
{
@@ -186,7 +186,7 @@ class CurlClient extends ClientAbstracts
/**
* @param $output
* @return void
* @throws Exception
* @throws
*/
private function explode($output): void
{
+89 -89
View File
@@ -11,99 +11,99 @@ use Exception;
*/
class HttpParse
{
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
$value = key($value);
}
if ($first === '') {
$first = $value;
} else {
$tp[] = $value;
}
}
$key = $first . '[' . implode('][', $tp) . ']';
if (count($tp) < 1) {
$key = $first;
}
return $key;
}
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
$value = key($value);
}
if ($first === '') {
$first = $value;
} else {
$tp[] = $value;
}
}
$key = $first . '[' . implode('][', $tp) . ']';
if (count($tp) < 1) {
$key = $first;
}
return $key;
}
/**
* @param $data
* @return string
* @throws Exception
*/
public static function parse($data): string
{
$tmp = [];
if (is_string($data)) {
return $data;
}
foreach ($data as $key => $datum) {
if ($datum === null) {
continue;
}
$tmp[] = static::ifElse($key, $datum);
}
return implode('&', $tmp);
}
/**
* @param $data
* @return string
* @throws
*/
public static function parse($data): string
{
$tmp = [];
if (is_string($data)) {
return $data;
}
foreach ($data as $key => $datum) {
if ($datum === null) {
continue;
}
$tmp[] = static::ifElse($key, $datum);
}
return implode('&', $tmp);
}
/**
* @param $t
* @param $qt
* @return string
* @throws Exception
*/
private static function ifElse($t, $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;
}
if (is_string($qt)) {
$string = $t . '=' . urlencode($qt);
} else {
$string = static::encode($t, $qt);
}
return $string;
}
/**
* @param $t
* @param $qt
* @return string
* @throws
*/
private static function ifElse($t, $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;
}
if (is_string($qt)) {
$string = $t . '=' . urlencode($qt);
} else {
$string = static::encode($t, $qt);
}
return $string;
}
/**
* @param mixed ...$object
* @return string
* @throws Exception
*/
private static function encode(...$object): string
{
$ret = [];
/**
* @param mixed ...$object
* @return string
* @throws
*/
private static function encode(...$object): string
{
$ret = [];
$data = $object[count($object) - 1];
$key = static::getKey(...$object);
foreach ($data as $s => $datum) {
if (is_array($datum)) {
$object[count($object) - 1] = $s;
$object[] = $datum;
$string = static::encode(...$object);
} else {
if (is_object($datum)) {
throw new Exception('Http body con\'t object.');
}
$string = $key . '=' . urlencode($datum);
}
$ret[] = $string;
}
return implode('&', $ret);
}
$data = $object[count($object) - 1];
$key = static::getKey(...$object);
foreach ($data as $s => $datum) {
if (is_array($datum)) {
$object[count($object) - 1] = $s;
$object[] = $datum;
$string = static::encode(...$object);
} else {
if (is_object($datum)) {
throw new Exception('Http body con\'t object.');
}
$string = $key . '=' . urlencode($datum);
}
$ret[] = $string;
}
return implode('&', $ret);
}
}
+2 -2
View File
@@ -22,7 +22,7 @@ class TcpClient
* @param string $host
* @param int $port
* @param int $socket
* @throws Exception
* @throws
*/
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
* @throws Exception
* @throws
*/
public function reconnect(): void
{