Compare commits

..

48 Commits

Author SHA1 Message Date
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
10 changed files with 1752 additions and 1711 deletions
+220 -178
View File
@@ -1,178 +1,220 @@
<?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();
}
}
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException;
use Kiri\Message\Stream;
use Swoole\Client as SwowClient;
/**
* Class Client
* @package 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', [error_trigger_format($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);
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
if (!$this->client->connect($host, $this->getPort())) {
throw new Exception('链接失败');
}
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
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
* @throws ConfigException
*/
private function execute(string $path, string $content)
{
$array = $this->_parseHeaders($path);
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content . "\r\n\r\n");
$receive = '';
while ($this->client->isConnected()) {
$_tmp = $this->client->recv();
if (empty($_tmp)) {
break;
}
$receive .= $_tmp;
}
Kiri::getDi()->get(Logger::class)->debug(implode("\r\n", $array) . "\r\n\r\n" . $content);
Kiri::getDi()->get(Logger::class)->debug($receive);
[$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);
}
/**
* @param string $path
* @return array
*/
#[Pure] private function _parseHeaders(string $path): array
{
$array = [];
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
if (!empty($this->getHeader())) {
$headers = $this->getHeader();
foreach ($headers as $key => $value) {
$array[] = sprintf('%s: %s', $key, $value);
}
}
return $array;
}
/**
* @param $client
* @param $string
* @return mixed
*/
private function waite(&$client, $string): mixed
{
$tmp = $client->recv();
if (!empty($tmp)) {
return $this->waite($client, $string . $tmp);
}
return $string;
}
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
{
/** @var SwowClient $client */
$client = $this->client;
if (!$client || !$client->isConnected()) {
return;
}
$client->close();
}
}
+43 -43
View File
@@ -1,43 +1,43 @@
<?php
namespace Http\Client;
use Kiri\Context;
/**
* @mixin CoroutineClient|CurlClient
*/
class Client
{
private CoroutineClient|CurlClient|AsyncClient $abstracts;
/**
* @param string $host
* @param int $port
* @param bool $isSsl
*/
public function __construct(string $host, int $port, bool $isSsl = false)
{
if (Context::inCoroutine()) {
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
} else {
$this->abstracts = new CurlClient($host, $port, $isSsl);
}
}
/**
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return $this->abstracts->{$name}(...$arguments);
}
}
<?php
namespace Kiri;
use Kiri\Context;
/**
* @mixin CoroutineClient|CurlClient
*/
class Client
{
private CoroutineClient|CurlClient|AsyncClient $abstracts;
/**
* @param string $host
* @param int $port
* @param bool $isSsl
*/
public function __construct(string $host, int $port, bool $isSsl = false)
{
if (Context::inCoroutine()) {
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
} else {
$this->abstracts = new CurlClient($host, $port, $isSsl);
}
}
/**
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return $this->abstracts->{$name}(...$arguments);
}
}
+779 -779
View File
File diff suppressed because it is too large Load Diff
+129 -129
View File
@@ -1,129 +1,129 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Http\Client;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
use Swoole\Coroutine\Http\Client as SwowClient;
/**
* Class Client
* @package Kiri\Kiri\Http
*/
class CoroutineClient 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);
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
} 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
*/
private function generate_client($data, $host, $isHttps, $path): void
{
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());
}
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
}
/**
* @param $path
* @param $data
* @return string
*/
private function setParams($path, $data): string
{
$content = $this->getData()->getContents();
if (!empty($content)) {
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$this->client->setData($data);
}
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
}
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri;
use Swoole\Coroutine\Http\Client as SwowClient;
/**
* Class Client
* @package Kiri\Http
*/
class CoroutineClient 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);
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
*/
private function generate_client($data, $host, $isHttps, $path): void
{
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());
}
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
}
/**
* @param $path
* @param $data
* @return string
*/
private function setParams($path, $data): string
{
$content = $this->getData()->getContents();
if (!empty($content)) {
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$this->client->setData($data);
}
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
}
+208 -208
View File
@@ -1,208 +1,208 @@
<?php
declare(strict_types=1);
namespace Http\Client;
use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/**
* Class CurlClient
* @package Http\Handler\Client
*/
class CurlClient extends ClientAbstracts
{
/**
* @param $method
* @param $path
* @param array $params
* @throws Exception
*/
public function request($method, $path, array $params = []): void
{
if ($method == self::GET) {
$path = $this->joinGetParams($path, $params);
}
$this->getCurlHandler($path, $method, $params);
$this->execute();
}
/**
* @param $path
* @param $method
* @param $params
* @throws Exception
*/
private function getCurlHandler($path, $method, $params): void
{
[$host, $isHttps, $path] = $this->matchHost($path);
$host = $isHttps ? 'https://' . $host : 'http://' . $host;
if ($this->getPort() != 443 && $this->getPort() != 80) {
$host .= ':' . $this->getPort();
}
$this->do(curl_init($host . $path), $host . $path, $method);
if ($isHttps !== FALSE) {
$this->curlHandlerSslSet();
}
$contents = $this->getData()->getContents();
if (empty($params) && empty($contents)) {
return;
}
if (!empty($contents)) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
} else if ($method === self::POST) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::UPLOAD) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
}
}
/**
* @return void
* @throws Exception
*/
private function curlHandlerSslSet(): void
{
if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
}
if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
}
}
/**
* @param $resource
* @param $path
* @param $method
* @throws Exception
*/
private function do($resource, $path, $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_HEADER, TRUE);
curl_setopt($resource, CURLOPT_FAILONERROR, TRUE);
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
}
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());
}
curl_setopt($resource, CURLOPT_NOBODY, FALSE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_POST, 1);
}
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$this->client = $resource;
if (!empty($this->caPath)) {
curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath);
}
}
private string $caPath = '';
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
$this->caPath = $path;
return $this;
}
/**
* @throws Exception
*/
private function execute(): void
{
$output = curl_exec($this->client);
if ($output === FALSE) {
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
} else {
$this->explode($output);
}
}
/**
*
*/
public function close(): void
{
curl_close($this->client);
}
/**
* @param $output
* @return void
* @throws Exception
*/
private function explode($output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
[$header, $body] = explode("\r\n\r\n", $body, 2);
}
$header = explode("\r\n", $header);
$status = explode(' ', array_shift($header));
$this->setStatusCode(intval($status[1]));
$this->setBody($body);
$this->setResponseHeader($header);
}
/**
* @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
*/
#[Pure] private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->getHeader() as $key => $val) {
$headers[$key] = $key . ': ' . $val;
}
return array_values($headers);
}
}
<?php
declare(strict_types=1);
namespace Kiri;
use Exception;
use Kiri\Message\Response;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/**
* Class CurlClient
* @package Http\Handler\Client
*/
class CurlClient extends ClientAbstracts
{
/**
* @param $method
* @param $path
* @param array $params
* @throws Exception
*/
public function request($method, $path, array $params = []): void
{
if ($method == self::GET) {
$path = $this->joinGetParams($path, $params);
}
$this->getCurlHandler($path, $method, $params);
$this->execute();
}
/**
* @param $path
* @param $method
* @param $params
* @throws Exception
*/
private function getCurlHandler($path, $method, $params): void
{
[$host, $isHttps, $path] = $this->matchHost($path);
$host = $isHttps ? 'https://' . $host : 'http://' . $host;
if ($this->getPort() != 443 && $this->getPort() != 80) {
$host .= ':' . $this->getPort();
}
$this->do(curl_init($host . $path), $host . $path, $method);
if ($isHttps !== FALSE) {
$this->curlHandlerSslSet();
}
$contents = $this->getData()->getContents();
if (empty($params) && empty($contents)) {
return;
}
if (!empty($contents)) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
} else if ($method === self::POST) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::UPLOAD) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
}
}
/**
* @return void
* @throws Exception
*/
private function curlHandlerSslSet(): void
{
if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
}
if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
}
}
/**
* @param $resource
* @param $path
* @param $method
* @throws Exception
*/
private function do($resource, $path, $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_HEADER, TRUE);
curl_setopt($resource, CURLOPT_FAILONERROR, TRUE);
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
}
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());
}
curl_setopt($resource, CURLOPT_NOBODY, FALSE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_POST, 1);
}
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$this->client = $resource;
if (!empty($this->caPath)) {
curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath);
}
}
private string $caPath = '';
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
$this->caPath = $path;
return $this;
}
/**
* @throws Exception
*/
private function execute(): void
{
$output = curl_exec($this->client);
if ($output === FALSE) {
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
} else {
$this->explode($output);
}
}
/**
*
*/
public function close(): void
{
curl_close($this->client);
}
/**
* @param $output
* @return void
* @throws Exception
*/
private function explode($output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
[$header, $body] = explode("\r\n\r\n", $body, 2);
}
$header = explode("\r\n", $header);
$status = explode(' ', array_shift($header));
$this->setStatusCode(intval($status[1]));
$this->setBody($body);
$this->setResponseHeader($header);
}
/**
* @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
*/
#[Pure] private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->getHeader() as $key => $val) {
$headers[$key] = $key . ': ' . $val;
}
return array_values($headers);
}
}
+109 -109
View File
@@ -1,109 +1,109 @@
<?php
declare(strict_types=1);
namespace Http\Client;
use Exception;
/**
* Class HttpParse
* @package BeReborn\Http
*/
class HttpParse
{
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $key => $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 $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 mixed ...$object
* @return string
* @throws Exception
*/
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);
}
}
<?php
declare(strict_types=1);
namespace Kiri;
use Exception;
/**
* Class HttpParse
* @package BeReborn\Http
*/
class HttpParse
{
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $key => $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 $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 mixed ...$object
* @return string
* @throws Exception
*/
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);
}
}
+181 -181
View File
@@ -1,181 +1,181 @@
<?php
namespace Http\Client;
use Closure;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
interface IClient
{
/**
* @param string $path
* @param array $params
*/
public function get(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function post(string $path, array $params = []): void;
/**
*
*/
public function close(): void;
/**
* @param string $path
* @param array $params
*/
public function delete(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function options(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function upload(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function put(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function head(string $path, array $params = []): void;
/**
* @param string $method
* @param string $path
* @param array $params
*/
public function request(string $method, string $path, array $params = []): void;
/**
* @param array $header
* @return static
*/
public function withHeader(array $header): static;
/**
* @param array $header
* @return static
*/
public function withHeaders(array $header): static;
/**
* @param string $key
* @param string $value
* @return static
*/
public function withAddedHeader(string $key, string $value): static;
/**
* @param int $value
* @return static
*/
public function withTimeout(int $value): static;
/**
* @param Closure|null $value
* @return static
*/
public function withCallback(?Closure $value): static;
/**
* @param string $value
* @return static
*/
public function withMethod(string $value): static;
/**
* @param bool $isSSL
* @return static
*/
public function withIsSSL(bool $isSSL): static;
/**
* @param string $agent
* @return static
*/
public function withAgent(string $agent): static;
/**
* @param string $ssl_cert_file
* @return static
*/
public function withSslCertFile(string $ssl_cert_file): static;
/**
* @param string $ssl_key_file
* @return static
*/
public function withSslKeyFile(string $ssl_key_file): static;
/**
* @param string $ssl_key_file
* @return static
*/
public function withCa(string $ssl_key_file): static;
/**
* @param string|StreamInterface $data
* @return static
*/
public function withBody(string|StreamInterface $data): static;
/**
* @param int $connect_timeout
* @return static
*/
public function withConnectTimeout(int $connect_timeout): static;
/**
* @param string $contentType
* @return static
*/
public function withContentType(string $contentType): static;
/**
* @return mixed
*/
public function getBody(): mixed;
}
<?php
namespace Kiri;
use Closure;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
interface IClient
{
/**
* @param string $path
* @param array $params
*/
public function get(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function post(string $path, array $params = []): void;
/**
*
*/
public function close(): void;
/**
* @param string $path
* @param array $params
*/
public function delete(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function options(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function upload(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function put(string $path, array $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function head(string $path, array $params = []): void;
/**
* @param string $method
* @param string $path
* @param array $params
*/
public function request(string $method, string $path, array $params = []): void;
/**
* @param array $header
* @return static
*/
public function withHeader(array $header): static;
/**
* @param array $header
* @return static
*/
public function withHeaders(array $header): static;
/**
* @param string $key
* @param string $value
* @return static
*/
public function withAddedHeader(string $key, string $value): static;
/**
* @param int $value
* @return static
*/
public function withTimeout(int $value): static;
/**
* @param Closure|null $value
* @return static
*/
public function withCallback(?Closure $value): static;
/**
* @param string $value
* @return static
*/
public function withMethod(string $value): static;
/**
* @param bool $isSSL
* @return static
*/
public function withIsSSL(bool $isSSL): static;
/**
* @param string $agent
* @return static
*/
public function withAgent(string $agent): static;
/**
* @param string $ssl_cert_file
* @return static
*/
public function withSslCertFile(string $ssl_cert_file): static;
/**
* @param string $ssl_key_file
* @return static
*/
public function withSslKeyFile(string $ssl_key_file): static;
/**
* @param string $ssl_key_file
* @return static
*/
public function withCa(string $ssl_key_file): static;
/**
* @param string|StreamInterface $data
* @return static
*/
public function withBody(string|StreamInterface $data): static;
/**
* @param int $connect_timeout
* @return static
*/
public function withConnectTimeout(int $connect_timeout): static;
/**
* @param string $contentType
* @return static
*/
public function withContentType(string $contentType): static;
/**
* @return mixed
*/
public function getBody(): mixed;
}
+21 -21
View File
@@ -1,21 +1,21 @@
MIT License
Copyright (c) 2021 向林
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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
SOFTWARE.
MIT License
Copyright (c) 2021 向林
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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
SOFTWARE.
+37 -38
View File
@@ -1,38 +1,37 @@
<?php
namespace Http\Client;
use JetBrains\PhpStorm\Pure;
trait TSwooleClient
{
/**
* @return array
*/
#[Pure] private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
$params = [];
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout();
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
$params['ssl_host_name'] = $this->getHost();
$params['ssl_cert_file'] = $this->getSslCertFile();
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = TRUE;
$params['ssl_cafile'] = $sslCa;
return $params;
}
}
<?php
namespace Kiri;
use JetBrains\PhpStorm\Pure;
trait TSwooleClient
{
/**
* @return array
*/
#[Pure] private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
$params = [];
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout();
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
$params['ssl_host_name'] = $this->getHost();
$params['ssl_cert_file'] = $this->getSslCertFile();
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = TRUE;
$params['ssl_cafile'] = $sslCa;
return $params;
}
}
+25 -25
View File
@@ -1,25 +1,25 @@
{
"name": "game-worker/kiri-client",
"description": "db",
"authors": [
{
"name": "XiangLin",
"email": "as2252258@163.com"
}
],
"license": "MIT",
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-swoole": "*",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
"autoload": {
"psr-4": {
"Http\\Client\\": "./"
}
},
"require-dev": {
}
}
{
"name": "game-worker/kiri-client",
"description": "db",
"authors": [
{
"name": "XiangLin",
"email": "as2252258@163.com"
}
],
"license": "MIT",
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-swoole": "*",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
"autoload": {
"psr-4": {
"Kiri\\": "./"
}
},
"require-dev": {
}
}