Compare commits

...

103 Commits

Author SHA1 Message Date
as2252258 eb42c8e0a2 eee 2026-07-03 16:15:52 +08:00
as2252258 10de0d5e08 eee 2026-07-03 16:10:28 +08:00
as2252258 d1adc532e1 eee 2026-07-03 16:08:07 +08:00
as2252258 ec22ac39a3 eee 2026-07-03 16:07:27 +08:00
as2252258 b96bd159d3 eee 2026-07-03 15:59:49 +08:00
as2252258 c31211a6fa eee 2026-07-03 15:53:55 +08:00
as2252258 45d8931267 eee 2026-07-03 15:06:12 +08:00
as2252258 7bcb789ed2 eee 2026-06-12 23:57:21 +08:00
as2252258 1d9d761798 eee 2025-12-31 00:19:30 +08:00
as2252258 22f425ceb7 ea 2025-11-28 11:43:24 +08:00
as2252258 a52610cf2a ea 2025-11-28 11:39:14 +08:00
as2252258 5697102a85 ea 2025-11-28 11:38:29 +08:00
as2252258 0c0a2fadd5 ea 2025-11-28 11:37:09 +08:00
as2252258 5061a17342 ea 2025-11-28 11:30:07 +08:00
as2252258 9c1bc63f78 ea 2025-11-26 07:52:46 +08:00
as2252258 345af9e727 eee 2025-07-14 15:35:37 +08:00
as2252258 196db497c1 eee 2023-12-18 18:21:09 +08:00
as2252258 6da3575619 eee 2023-12-12 15:35:36 +08:00
as2252258 9814e06e8d eee 2023-12-12 14:08:01 +08:00
as2252258 df32502723 eee 2023-11-09 14:46:15 +08:00
as2252258 7f702e5784 eee 2023-11-09 14:45:59 +08:00
as2252258 fd61b6f1d9 eee 2023-11-07 16:16:34 +08:00
as2252258 9e062a7a33 eee 2023-11-07 16:15:48 +08:00
as2252258 43c8f9b8e6 eee 2023-10-27 22:36:05 +08:00
as2252258 7b14fa075e eee 2023-10-27 22:23:11 +08:00
as2252258 165f62113c eee 2023-10-24 17:22:30 +08:00
as2252258 3c3b6f2e65 qqq 2023-08-24 11:25:42 +08:00
as2252258 2ca20d3a96 qqq 2023-08-18 19:53:00 +08:00
as2252258 b2800b806c qqq 2023-08-18 19:50:43 +08:00
as2252258 092daa022a qqq 2023-08-18 17:33:06 +08:00
as2252258 57337cbe16 qqq 2023-08-18 16:37:47 +08:00
as2252258 ad8b20723b qqq 2023-08-18 16:37:18 +08:00
as2252258 f2421b71e7 qqq 2023-08-18 16:36:05 +08:00
as2252258 02685a9e61 qqq 2023-08-18 15:45:30 +08:00
as2252258 1b5232f5e7 qqq 2023-08-18 15:43:52 +08:00
as2252258 c76fb40689 qqq 2023-08-17 16:32:02 +08:00
as2252258 18b44b32f2 qqq 2023-07-12 21:38:27 +08:00
as2252258 14574e7a6c qqq 2023-06-12 17:51:10 +08:00
as2252258 13fc812ae4 变更 2022-12-12 17:31:12 +08:00
as2252258 edd1751f7a 变更 2022-09-08 14:07:22 +08:00
as2252258 7dbce8b1e8 modify plugin name 2022-07-08 17:40:25 +08:00
as2252258 eb7ffecdb9 modify plugin name 2022-06-22 16:29:42 +08:00
as2252258 dd85596907 变更 2022-05-31 11:48:20 +08:00
as2252258 e6b10bbd70 变更 2022-05-31 08:24:44 +08:00
as2252258 c149afa3f8 变更 2022-05-31 08:23:26 +08:00
as2252258 f195d001c6 变更 2022-05-31 07:46:45 +08:00
as2252258 b45666d9db 变更 2022-05-31 07:46:01 +08:00
as2252258 650aaee974 modify plugin name 2022-03-01 18:47:45 +08:00
as2252258 d589e096af modify plugin name 2022-03-01 18:46:08 +08:00
as2252258 f17a55f8d0 modify plugin name 2022-02-21 15:39:55 +08:00
as2252258 3cc98214c7 modify plugin name 2022-02-21 15:23:41 +08:00
as2252258 aaeb538849 modify plugin name 2022-02-21 15:21:34 +08:00
as2252258 0c7b8eb57f modify plugin name 2022-02-21 15:17:42 +08:00
as2252258 48808ded98 modify plugin name 2022-02-18 13:53:51 +08:00
as2252258 eb25fe06ed modify plugin name 2022-02-18 13:45:30 +08:00
as2252258 4d51f6e5d6 Revert "改名"
This reverts commit fdf58326
2022-01-12 14:10:33 +08:00
as2252258 21fbbe52e5 Revert "改名"
This reverts commit fdf58326
2022-01-11 17:56:16 +08:00
as2252258 a0a753d8b4 Revert "改名"
This reverts commit fdf58326
2022-01-11 16:25:17 +08:00
as2252258 dcc5e67f0a Revert "改名"
This reverts commit fdf58326
2022-01-11 16:21:29 +08:00
as2252258 b8e7a5f086 Revert "改名"
This reverts commit fdf58326
2022-01-11 16:20:49 +08:00
as2252258 88a414f0bb Revert "改名"
This reverts commit fdf58326
2022-01-11 16:19:05 +08:00
as2252258 cba27222fa Revert "改名"
This reverts commit fdf58326
2022-01-11 16:16:04 +08:00
as2252258 593f05754d Revert "改名"
This reverts commit fdf58326
2022-01-11 15:44:56 +08:00
as2252258 044c5c4a28 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:44:07 +08:00
as2252258 121642e825 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:42:55 +08:00
as2252258 5eecf66b43 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:41:53 +08:00
as2252258 a88787a530 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:40:55 +08:00
as2252258 aed0e61531 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:38:56 +08:00
as2252258 c94883375a Revert "改名"
This reverts commit fdf58326
2022-01-11 15:38:06 +08:00
as2252258 6565f1d7a3 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:35:59 +08:00
as2252258 cefe7ae010 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:35:14 +08:00
as2252258 56f42ac97b Revert "改名"
This reverts commit fdf58326
2022-01-11 15:34:27 +08:00
as2252258 9c7355d7f6 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:33:27 +08:00
as2252258 2ff2e3cdb8 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:32:19 +08:00
as2252258 18c6f9594e Revert "改名"
This reverts commit fdf58326
2022-01-11 15:31:37 +08:00
as2252258 a5c2467cdf Revert "改名"
This reverts commit fdf58326
2022-01-11 15:30:10 +08:00
as2252258 698868b6ee Revert "改名"
This reverts commit fdf58326
2022-01-11 15:28:30 +08:00
as2252258 cbee38ae54 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:27:10 +08:00
as2252258 e19536ce99 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:25:22 +08:00
as2252258 b92ab0bf1d Revert "改名"
This reverts commit fdf58326
2022-01-11 15:24:22 +08:00
as2252258 bc049a3c77 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:20:00 +08:00
as2252258 a026a610a1 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:12:59 +08:00
as2252258 e71a17cbce Revert "改名"
This reverts commit fdf58326
2022-01-11 15:11:18 +08:00
as2252258 86c09f3b25 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:09:14 +08:00
as2252258 788ecee65d Revert "改名"
This reverts commit fdf58326
2022-01-11 15:08:18 +08:00
as2252258 98cbb07845 Revert "改名"
This reverts commit fdf58326
2022-01-11 15:04:16 +08:00
as2252258 93bab6d5eb Revert "改名"
This reverts commit fdf58326
2022-01-11 15:01:42 +08:00
as2252258 99f79b7616 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:59:59 +08:00
as2252258 47dc988e49 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:59:24 +08:00
as2252258 072de12cc1 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:58:21 +08:00
as2252258 77f755d07c Revert "改名"
This reverts commit fdf58326
2022-01-11 14:54:21 +08:00
as2252258 bc5f9b1085 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:51:50 +08:00
as2252258 4660025775 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:46:42 +08:00
as2252258 336cc875a1 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:46:08 +08:00
as2252258 d943b2ebc2 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:44:37 +08:00
as2252258 f982b51ff6 Revert "改名"
This reverts commit fdf58326
2022-01-10 18:51:56 +08:00
as2252258 5b7a33f48b Revert "改名"
This reverts commit fdf58326
2022-01-10 18:48:17 +08:00
as2252258 753e521a41 Revert "改名"
This reverts commit fdf58326
2022-01-10 18:47:31 +08:00
as2252258 d479f36662 Revert "改名"
This reverts commit fdf58326
2022-01-10 18:45:21 +08:00
as2252258 6984f78746 Revert "改名"
This reverts commit fdf58326
2022-01-10 18:44:20 +08:00
as2252258 030f337e74 Revert "改名"
This reverts commit fdf58326
2022-01-10 18:36:35 +08:00
as2252258 ae3b0c411b Revert "改名"
This reverts commit fdf58326
2022-01-10 11:39:56 +08:00
as2252258 f838795983 e 2022-01-09 13:54:34 +08:00
12 changed files with 1543 additions and 1711 deletions
+37
View File
@@ -0,0 +1,37 @@
# Created by .ignore support plugin (hsz.mobi)
### Yii template
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
db/
async-queue/
composer.lock
*.log
commands/result
config/setting.php
tests/
vendor/*
runtime/
*.xml
*.lock
oot
d
composer.lock
-178
View File
@@ -1,178 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Http\Client;
use Exception;
use Http\Message\Stream;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
use Swoole\Client as SwowClient;
/**
* Class Client
* @package Kiri\Kiri\Http
*/
class AsyncClient extends ClientAbstracts
{
use TSwooleClient;
/**
* @param string $method
* @param $path
* @param array $params
* @return void
* @throws Exception
*/
public function request(string $method, $path, array $params = []): void
{
$this->withMethod($method)
->coroutine(
$this->matchHost($path),
$this->paramEncode($params)
);
}
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
return $this;
}
/**
* @param $url
* @param array|string $data
* @throws Exception 使用swoole协程方式请求
*/
private function coroutine($url, array|string $data = []): void
{
try {
$this->generate_client($data, ...$url);
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
* @throws Exception
*/
private function generate_client($data, $host, $isHttps, $path): void
{
$this->client = new SwowClient(SWOOLE_TCP, FALSE);
if (!$this->client->connect($host, $this->getPort())) {
throw new Exception('链接失败');
}
if ($isHttps || $this->isSSL()) {
$this->client->enableSSL();
}
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
$path = $this->setParams($path, $data);
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9');
// $this->withAddedHeader('Accept-Encoding', 'gzip');
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
$this->execute($path, $this->getData()->getContents());
}
/**
* @param string $path
* @param string $content
* @return void
*/
private function execute(string $path, string $content)
{
$array = [];
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
if (!empty($this->getHeader())) {
foreach ($this->getHeader() as $key => $value) {
$array[] = sprintf('%s: %s', $key, $value);
}
}
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content);
$receive = $this->client->recv();
[$header, $body] = explode("\r\n\r\n", $receive);
$header = explode("\r\n", $header);
$status = array_shift($header);
$this->setStatusCode(intval(explode(' ', $status)[1]));
$this->parseResponseHeaders($header);
$this->setBody($body);
}
private function chunked()
{
}
/**
* @param array $headers
* @return void
*/
private function parseResponseHeaders(array $headers)
{
$array = [];
foreach ($headers as $header) {
[$key, $value] = explode(': ', $header);
$array[$key] = trim($value);
}
$this->setResponseHeader($array);
}
/**
* @param $path
* @param $data
* @return string
*/
private function setParams($path, $data): string
{
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$this->withBody(new Stream($data));
}
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
}
+70 -43
View File
@@ -1,43 +1,70 @@
<?php <?php
namespace Http\Client; namespace Kiri;
use Kiri\Context; use Exception;
use Swoole\Coroutine;
/**
* @mixin CoroutineClient|CurlClient /**
*/ * @mixin CoroutineClient|CurlClient
class Client */
{ class Client
{
private CoroutineClient|CurlClient|AsyncClient $abstracts;
private CoroutineClient|CurlClient $abstracts;
/**
* @param string $host /**
* @param int $port * @param string $host
* @param bool $isSsl * @param int $port
*/ * @param bool $isSsl
public function __construct(string $host, int $port, bool $isSsl = false) */
{ public function __construct(string $host, int $port, bool $isSsl = false)
if (Context::inCoroutine()) { {
$this->abstracts = new CoroutineClient($host, $port, $isSsl); if (class_exists(Coroutine::class) && Coroutine::getCid() > -1) {
} else { $this->abstracts = new CoroutineClient($host, $port, $isSsl);
$this->abstracts = new CurlClient($host, $port, $isSsl); } else {
} $this->abstracts = new CurlClient($host, $port, $isSsl);
} }
}
/**
* @param string $name /**
* @param array $arguments * @param string $name
* @return mixed * @param array $arguments
*/ * @return mixed
public function __call(string $name, array $arguments) */
{ public function __call(string $name, array $arguments)
return $this->abstracts->{$name}(...$arguments); {
} return $this->abstracts->{$name}(...$arguments);
}
}
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public function __get(string $name)
{
// TODO: Implement __get() method.
$getter = 'get' . ucfirst($name);
if (method_exists($this->abstracts, $getter)) {
return $this->abstracts->$getter();
}
if (method_exists($this->abstracts, $name)) {
return $this->abstracts->$name();
}
if (property_exists($this->abstracts, $name)) {
return $this->abstracts->$name;
}
throw new Exception('Property|Method "' . $name . '" does not exist.');
}
}
+589 -779
View File
File diff suppressed because it is too large Load Diff
+159 -129
View File
@@ -1,129 +1,159 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: whwyy * User: whwyy
* Date: 2018/5/24 0024 * Date: 2018/5/24 0024
* Time: 11:34 * Time: 11:34
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Kiri;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use Swoole\Coroutine\Http\Client as SwowClient;
use Kiri\Abstracts\Logger;
use Kiri\Kiri; /**
use Swoole\Coroutine\Http\Client as SwowClient; * Class Client
* @package Kiri\Http
/** */
* Class Client class CoroutineClient extends ClientAbstracts
* @package Kiri\Kiri\Http {
*/
class CoroutineClient extends ClientAbstracts use TSwooleClient;
{
/**
use TSwooleClient; * @param string $method
* @param $path
/** * @param array|string $params
* @param string $method * @return void
* @param $path * @throws
* @param array $params */
* @return void public function request(string $method, $path, array|string $params = []): void
* @throws Exception {
*/ if (!str_starts_with($path, '/')) {
public function request(string $method, $path, array $params = []): void $path = '/' . $path;
{ }
$this->withMethod($method)
->coroutine( $host = $this->host;
$this->matchHost($path), if (!preg_match('/(\d{1,3}\.){3}\d{1,3}/', $host)) {
$this->paramEncode($params) $this->withAddedHeader('Host', $host);
); }
} $this->withMethod($method)
->coroutine(
$path,
$this->paramEncode($params)
/** );
* @param $path }
* @return $this
*/
public function withCAInfo($path): static /**
{ * @param string $path
return $this; * @return $this
} */
public function withCAInfo(string $path): static
/** {
* @param $url return $this;
* @param array|string $data }
* @throws Exception 使用swoole协程方式请求
*/ /**
private function coroutine($url, array|string $data = []): void * @param string $url
{ * @param array|string $data
try { */
$this->generate_client($data, ...$url); private function coroutine(string $url, array|string $data = []): void
if ($this->client->statusCode < 0) { {
throw new Exception($this->client->errMsg); try {
} $this->generate_client($this->host, $this->isSSL);
$this->setStatusCode($this->client->getStatusCode()); if ($this->client->statusCode < 0) {
$this->setBody($this->client->getBody()); throw new Exception($this->client->errMsg);
$this->setResponseHeader($this->client->headers); }
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); $this->execute($url, $data);
$this->setStatusCode(-1); } catch (\Throwable $exception) {
$this->setBody(jTraceEx($exception)); $this->statusCode = -1;
} $this->body = json_encode(['code' => 500, 'message' => $exception->getMessage()]);
} }
}
/**
* @param $data /**
* @param $host * @param string $path
* @param $isHttps * @param array|string $data
* @param $path * @return void
*/ */
private function generate_client($data, $host, $isHttps, $path): void private function execute(string $path, array|string $data): void
{ {
if ($isHttps || $this->isSSL()) { $this->client->execute($this->setParams($path, $data));
$this->client = new SwowClient($host, 443, true); if (in_array($this->client->getStatusCode(), [502, 404])) {
} else { $this->retry($path, $data);
$this->client = new SwowClient($host, $this->getPort(), false); } else {
} $this->statusCode = $this->client->getStatusCode();
$this->client->set($this->settings()); $this->body = $this->client->getBody();
if (!empty($this->getAgent())) { $this->setResponseHeader($this->client->headers);
$this->withAddedHeader('User-Agent', $this->getAgent()); }
} }
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data)); /**
} * @param string $path
* @param array|string $data
* @return void
/** */
* @param $path private function retry(string $path, array|string $data): void
* @param $data {
* @return string if (($this->num += 1) <= $this->retryNum) {
*/ sleep($this->retryTimeout);
private function setParams($path, $data): string
{ $this->execute($path, $data);
$content = $this->getData()->getContents(); } else {
if (!empty($content)) { $this->statusCode = $this->client->statusCode;
$this->client->setData($content); $this->body = $this->client->errMsg;
} }
if ($this->isGet()) { }
if (!empty($data)) $path .= '?' . $data;
} else { /**
$data = $this->mergeParams($data); * @param string $host
if (!empty($data)) { * @param bool $isHttps
$this->client->setData($data); */
} private function generate_client(string $host, bool $isHttps): void
} {
return $path; 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());
public function close(): void if (!empty($this->agent)) {
{ $this->withAddedHeader('User-Agent', $this->agent);
$this->client->close(); }
} $this->client->setHeaders($this->header);
} $this->client->setMethod(strtoupper($this->method));
}
/**
* @param string $path
* @param mixed $data
* @return string
*/
private function setParams(string $path, mixed $data): string
{
$content = $this->_data;
if (!empty($content)) {
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else if (!empty($data)) {
$this->client->setData($data);
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
}
+230 -208
View File
@@ -1,208 +1,230 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Kiri;
use Exception; /**
use Http\Message\Response; * Class CurlClient
use Http\Message\Stream; *
use JetBrains\PhpStorm\Pure; * @package Http\Handler\Client
use Psr\Http\Message\ResponseInterface; */
class CurlClient extends ClientAbstracts
{
/**
* Class CurlClient
* @package Http\Handler\Client /**
*/ * @param string $method
class CurlClient extends ClientAbstracts * @param string $path
{ * @param array|string $params
*
/** * @return void
* @param $method */
* @param $path public function request(string $method, string $path, array|string $params = []): void
* @param array $params {
* @throws Exception if (!str_starts_with($path, '/')) {
*/ $path = '/' . $path;
public function request($method, $path, array $params = []): void }
{ if ($method == self::GET) {
if ($method == self::GET) { $path = $this->joinGetParams($path, $params);
$path = $this->joinGetParams($path, $params); }
}
$this->getCurlHandler($path, $method, $params);
$this->getCurlHandler($path, $method, $params);
$this->execute();
$this->execute(); }
}
/**
/** * @param string $path
* @param $path * @param string $method
* @param $method * @param $params
* @param $params *
* @throws Exception * @return void
*/ */
private function getCurlHandler($path, $method, $params): void private function getCurlHandler(string $path, string $method, $params): void
{ {
[$host, $isHttps, $path] = $this->matchHost($path); $host = $this->isSSL ? 'https://' . $this->host : 'http://' . $this->host;
if ($this->getPort() != 443 && $this->getPort() != 80) {
$host = $isHttps ? 'https://' . $host : 'http://' . $host; $host .= ':' . $this->getPort();
if ($this->getPort() != 443 && $this->getPort() != 80) { }
$host .= ':' . $this->getPort(); $this->do(curl_init($host . $path), $host . $path, $method);
} if ($this->isSSL) {
$this->do(curl_init($host . $path), $host . $path, $method); $this->curlHandlerSslSet();
if ($isHttps !== FALSE) { }
$this->curlHandlerSslSet(); $contents = $this->_data;
} if (empty($params) && empty($contents)) {
$contents = $this->getData()->getContents(); return;
if (empty($params) && empty($contents)) { }
return; if (!empty($contents)) {
} curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
if (!empty($contents)) { } else if ($method === self::UPLOAD) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents); curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} else if ($method === self::POST) { } else if ($method === self::POST) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params)); if (is_array($params)) {
} else if ($method === self::UPLOAD) { $params = http_build_query($params);
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params); }
} curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} }
}
/**
* @return void /**
* @throws Exception * @return void
*/ * @throws
private function curlHandlerSslSet(): void */
{ 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->ca) && file_exists($this->ca)) {
curl_setopt($this->client, CURLOPT_CAINFO, $this->ca);
/** }
* @param $resource }
* @param $path
* @param $method
* @throws Exception /**
*/ * @param mixed $resource
private function do($resource, $path, $method): void * @param string $path
{ * @param string $method
curl_setopt($resource, CURLOPT_URL, $path); *
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 * @return void
curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置 */
curl_setopt($resource, CURLOPT_HEADER, TRUE); private function do(mixed $resource, string $path, string $method): void
curl_setopt($resource, CURLOPT_FAILONERROR, TRUE); {
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); curl_setopt($resource, CURLOPT_URL, $path);
if (defined('CURLOPT_SSL_FALSESTART')) { curl_setopt($resource, CURLOPT_TIMEOUT, $this->timeout); // 超时设置
curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE); curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout); // 超时设置
} curl_setopt($resource, CURLOPT_HEADER, TRUE);
curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE); curl_setopt($resource, CURLOPT_FAILONERROR, TRUE);
curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE); curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
if (!empty($this->getAgent())) { if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
} }
curl_setopt($resource, CURLOPT_NOBODY, FALSE); curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE);
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 if (!empty($this->agent)) {
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($resource, CURLOPT_USERAGENT, $this->agent);
if ($method === self::POST || $method == self::UPLOAD) { }
curl_setopt($resource, CURLOPT_POST, 1); curl_setopt($resource, CURLOPT_NOBODY, FALSE);
} curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
$this->client = $resource; curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
if (!empty($this->caPath)) { if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath); curl_setopt($resource, CURLOPT_POST, 1);
} }
} [$proxy, $port] = [$this->proxyHost, $this->proxyPort];
if (!empty($proxy) && $port > 0) {
curl_setopt($resource, CURLOPT_PROXYPORT, $port);
private string $caPath = ''; curl_setopt($resource, CURLOPT_PROXY, $proxy);
}
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
/** $this->client = $resource;
* @param $path if (!empty($this->caPath)) {
* @return $this curl_setopt($this->client, CURLOPT_CAINFO, $this->caPath);
*/ }
public function withCAInfo($path): static }
{
$this->caPath = $path;
return $this; /**
} * @var string
*/
private string $caPath = '';
/**
* @throws Exception
*/ /**
private function execute(): void * @param string $path
{ *
$output = curl_exec($this->client); * @return $this
if ($output === FALSE) { */
$this->setStatusCode(curl_errno($this->client)); public function withCAInfo(string $path): static
$this->setBody(curl_error($this->client)); {
} else { $this->caPath = $path;
$this->explode($output); return $this;
} }
}
/**
/** * @throws
* */
*/ private function execute(): void
public function close(): void {
{ $output = curl_exec($this->client);
curl_close($this->client); if ($output !== FALSE) {
} $this->explode($output);
} else {
$this->statusCode = curl_errno($this->client);
/** $this->body = curl_error($this->client);
* @param $output }
* @return void $this->close();
* @throws Exception }
*/
private function explode($output): void
{ /**
[$header, $body] = explode("\r\n\r\n", $output, 2); * @return void
if ($header == 'HTTP/1.1 100 Continue') { * @throws
[$header, $body] = explode("\r\n\r\n", $body, 2); */
} private function retry(): void
{
$header = explode("\r\n", $header); if (($this->num += 1) <= $this->retryNum) {
$status = explode(' ', array_shift($header)); sleep($this->retryTimeout);
$this->setStatusCode(intval($status[1])); $this->execute();
$this->setBody($body); } else {
$this->setResponseHeader($header); $this->statusCode = curl_errno($this->client);
} $this->body = curl_error($this->client);
}
/** }
* @param $headers
* @return array
*/ /**
private function headerFormat($headers): array *
{ */
$_tmp = []; public function close(): void
foreach ($headers as $val) { {
$trim = explode(': ', trim($val)); if (PHP_VERSION < '8.0') {
curl_close($this->client);
$_tmp[strtolower($trim[0])] = [$trim[1] ?? '']; }
} }
return $_tmp;
}
/**
* @param string $output
/** *
* @return array * @return void
*/ * @throws
#[Pure] private function parseHeaderMat(): array */
{ private function explode(string $output): void
$headers = []; {
foreach ($this->getHeader() as $key => $val) { // 获取 HTTP 状态码
$headers[$key] = $key . ': ' . $val; $statusCode = +curl_getinfo($this->client, CURLINFO_HTTP_CODE);
}
return array_values($headers); // 获取 header 的大小(不包括最后的 \r\n\r\n 分隔符)
} $headerSize = curl_getinfo($this->client, CURLINFO_HEADER_SIZE);
} $header = substr($output, 0, $headerSize);
if (in_array($statusCode, [502, 404])) {
$this->retry();
} else {
$this->statusCode = $statusCode;
$this->body = substr($output, $headerSize);
$this->setResponseHeader(explode("\r\n", $header));
}
}
/**
* @return array
*/
private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->header as $key => $val) {
$headers[$key] = $key . ': ' . $val;
}
return array_values($headers);
}
}
+108 -109
View File
@@ -1,109 +1,108 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Kiri;
use Exception; use Exception;
/** /**
* Class HttpParse * Class HttpParse
* @package BeReborn\Http * @package BeReborn\Http
*/ */
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 $key => $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 array|string $data
* @return string * @return string
* @throws Exception */
*/ public static function parse(array|string $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
* @throws Exception */
*/ private static function ifElse(string $t, mixed $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
* @throws Exception */
*/ 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); }
}
}
}
+178 -181
View File
@@ -1,181 +1,178 @@
<?php <?php
namespace Http\Client; namespace Kiri;
use Closure; use Closure;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface; interface IClient
{
interface IClient
{
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function get(string $path, array|string $params = []): void;
*/
public function get(string $path, array $params = []): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function post(string $path, array|string $params = []): void;
*/
public function post(string $path, array $params = []): void;
/**
*
/** */
* public function close(): void;
*/
public function close(): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function delete(string $path, array|string $params = []): void;
*/
public function delete(string $path, array $params = []): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function options(string $path, array|string $params = []): void;
*/
public function options(string $path, array $params = []): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function upload(string $path, array|string $params = []): void;
*/
public function upload(string $path, array $params = []): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function put(string $path, array|string $params = []): void;
*/
public function put(string $path, array $params = []): void;
/**
* @param string $path
/** * @param array|string $params
* @param string $path */
* @param array $params public function head(string $path, array|string $params = []): void;
*/
public function head(string $path, array $params = []): void;
/**
* @param string $method
/** * @param string $path
* @param string $method * @param array|string $params
* @param string $path */
* @param array $params public function request(string $method, string $path, array|string $params = []): void;
*/
public function request(string $method, string $path, array $params = []): void;
/**
* @param array $header
/** * @return static
* @param array $header */
* @return static public function withHeader(array $header): static;
*/
public function withHeader(array $header): static;
/**
* @param array $header
/** * @return static
* @param array $header */
* @return static public function withHeaders(array $header): static;
*/
public function withHeaders(array $header): static;
/**
* @param string $key
/** * @param string $value
* @param string $key * @return static
* @param string $value */
* @return static public function withAddedHeader(string $key, string $value): static;
*/
public function withAddedHeader(string $key, string $value): static;
/**
* @param int $value
/** * @return static
* @param int $value */
* @return static public function withTimeout(int $value): static;
*/
public function withTimeout(int $value): static;
/**
* @param Closure|null $value
/** * @return static
* @param Closure|null $value */
* @return static public function withCallback(?Closure $value): static;
*/
public function withCallback(?Closure $value): static;
/**
* @param string $value
/** * @return static
* @param string $value */
* @return static public function withMethod(string $value): static;
*/
public function withMethod(string $value): static;
/**
* @param bool $isSSL
/** * @return static
* @param bool $isSSL */
* @return static public function withIsSSL(bool $isSSL): static;
*/
public function withIsSSL(bool $isSSL): static;
/**
* @param string $agent
/** * @return static
* @param string $agent */
* @return static public function withAgent(string $agent): static;
*/
public function withAgent(string $agent): static;
/**
* @param string $ssl_cert_file
/** * @return static
* @param string $ssl_cert_file */
* @return static public function withSslCertFile(string $ssl_cert_file): static;
*/
public function withSslCertFile(string $ssl_cert_file): static;
/**
* @param string $ssl_key_file
/** * @return static
* @param string $ssl_key_file */
* @return static public function withSslKeyFile(string $ssl_key_file): static;
*/
public function withSslKeyFile(string $ssl_key_file): static;
/**
* @param string $ssl_key_file
/** * @return static
* @param string $ssl_key_file */
* @return static public function withCa(string $ssl_key_file): static;
*/
public function withCa(string $ssl_key_file): static;
/**
* @param string $data
/** * @return static
* @param string|StreamInterface $data */
* @return static public function withBody(string $data): static;
*/
public function withBody(string|StreamInterface $data): static;
/**
* @param int $connect_timeout
/** * @return static
* @param int $connect_timeout */
* @return static public function withConnectTimeout(int $connect_timeout): static;
*/
public function withConnectTimeout(int $connect_timeout): static;
/**
* @param string $contentType
/** * @return static
* @param string $contentType */
* @return static public function withContentType(string $contentType): static;
*/
public function withContentType(string $contentType): static;
public null|string $body {
get;
/** }
* @return mixed }
*/
public function getBody(): mixed;
}
+21 -21
View File
@@ -1,21 +1,21 @@
MIT License MIT License
Copyright (c) 2021 向林 Copyright (c) 2021 向林
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
+41 -38
View File
@@ -1,38 +1,41 @@
<?php <?php
namespace Http\Client; namespace Kiri;
use JetBrains\PhpStorm\Pure; trait TSwooleClient
{
trait TSwooleClient
{
/**
* @return array
*/
/** private function settings(): array
* @return array {
*/ $sslCert = $this->ssl_cert_file;
#[Pure] private function settings(): array $sslKey = $this->ssl_key_file;
{ $sslCa = $this->ca;
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile(); $params = [];
$sslCa = $this->getCa(); if ($this->connect_timeout > 0) {
$params['timeout'] = $this->connect_timeout;
$params = []; }
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout(); [$proxy, $port] = [$this->proxyHost, $this->proxyPort];
} if (!empty($proxy) && $port > 0) {
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { $params['http_proxy_host'] = $proxy;
return $params; $params['http_proxy_port'] = $port;
} }
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
$params['ssl_host_name'] = $this->getHost(); return $params;
$params['ssl_cert_file'] = $this->getSslCertFile(); }
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = TRUE; $params['ssl_host_name'] = $this->host;
$params['ssl_cafile'] = $sslCa; $params['ssl_cert_file'] = $this->ssl_cert_file;
$params['ssl_key_file'] = $this->ssl_key_file;
return $params; $params['ssl_verify_peer'] = TRUE;
} $params['ssl_cafile'] = $sslCa;
} return $params;
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
namespace Kiri;
use Exception;
use Kiri\Di\Context;
use Swoole\Coroutine\Client as CoroutineClient;
use Swoole\Client as AsyncClient;
class TcpClient
{
/**
* @var AsyncClient|CoroutineClient
*/
protected AsyncClient|CoroutineClient $client;
/**
* @param string $host
* @param int $port
* @param int $socket
* @throws
*/
public function __construct(readonly public string $host, readonly public int $port, readonly public int $socket = SWOOLE_SOCK_TCP)
{
$this->reconnect();
}
/**
* @return void
* @throws
*/
public function reconnect(): void
{
$this->client?->close();
if (Context::inCoroutine()) {
$this->client = new CoroutineClient($this->socket);
} else {
$this->client = new AsyncClient($this->socket);
}
if (!$this->client->connect($this->host, $this->port, 1)) {
throw new Exception('Connect ' . $this->host . '::' . $this->port . ' fail');
}
}
/**
* @param string $data
* @return int|bool
*/
public function send(string $data): int|bool
{
return $this->client->send($data);
}
/**
* @return bool|string
*/
public function read(): bool|string
{
return $this->client->recv();
}
/**
* @return bool
*/
public function isConnected(): bool
{
return $this->client->isConnected();
}
/**
* @return bool
*/
public function close(): bool
{
return $this->client->close();
}
}
+23 -25
View File
@@ -1,25 +1,23 @@
{ {
"name": "game-worker/kiri-client", "name": "game-worker/kiri-client",
"description": "db", "description": "db",
"authors": [ "authors": [
{ {
"name": "XiangLin", "name": "XiangLin",
"email": "as2252258@163.com" "email": "as2252258@163.com"
} }
], ],
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": ">=8.0", "php": ">=8.5",
"ext-json": "*", "ext-json": "*",
"ext-swoole": "*", "ext-swoole": "*"
"psr/http-client": "^1.0", },
"psr/http-message": "^1.0" "autoload": {
}, "psr-4": {
"autoload": { "Kiri\\": "./"
"psr-4": { }
"Http\\Client\\": "./" },
} "require-dev": {
}, }
"require-dev": { }
}
}