Compare commits
103 Commits
v1.6.2
...
d1adc532e1
| Author | SHA1 | Date | |
|---|---|---|---|
| d1adc532e1 | |||
| ec22ac39a3 | |||
| b96bd159d3 | |||
| c31211a6fa | |||
| 45d8931267 | |||
| 7bcb789ed2 | |||
| 1d9d761798 | |||
| 22f425ceb7 | |||
| a52610cf2a | |||
| 5697102a85 | |||
| 0c0a2fadd5 | |||
| 5061a17342 | |||
| 9c1bc63f78 | |||
| 345af9e727 | |||
| 196db497c1 | |||
| 6da3575619 | |||
| 9814e06e8d | |||
| df32502723 | |||
| 7f702e5784 | |||
| fd61b6f1d9 | |||
| 9e062a7a33 | |||
| 43c8f9b8e6 | |||
| 7b14fa075e | |||
| 165f62113c | |||
| 3c3b6f2e65 | |||
| 2ca20d3a96 | |||
| b2800b806c | |||
| 092daa022a | |||
| 57337cbe16 | |||
| ad8b20723b | |||
| f2421b71e7 | |||
| 02685a9e61 | |||
| 1b5232f5e7 | |||
| c76fb40689 | |||
| 18b44b32f2 | |||
| 14574e7a6c | |||
| 13fc812ae4 | |||
| edd1751f7a | |||
| 7dbce8b1e8 | |||
| eb7ffecdb9 | |||
| dd85596907 | |||
| e6b10bbd70 | |||
| c149afa3f8 | |||
| f195d001c6 | |||
| b45666d9db | |||
| 650aaee974 | |||
| d589e096af | |||
| f17a55f8d0 | |||
| 3cc98214c7 | |||
| aaeb538849 | |||
| 0c7b8eb57f | |||
| 48808ded98 | |||
| eb25fe06ed | |||
| 4d51f6e5d6 | |||
| 21fbbe52e5 | |||
| a0a753d8b4 | |||
| dcc5e67f0a | |||
| b8e7a5f086 | |||
| 88a414f0bb | |||
| cba27222fa | |||
| 593f05754d | |||
| 044c5c4a28 | |||
| 121642e825 | |||
| 5eecf66b43 | |||
| a88787a530 | |||
| aed0e61531 | |||
| c94883375a | |||
| 6565f1d7a3 | |||
| cefe7ae010 | |||
| 56f42ac97b | |||
| 9c7355d7f6 | |||
| 2ff2e3cdb8 | |||
| 18c6f9594e | |||
| a5c2467cdf | |||
| 698868b6ee | |||
| cbee38ae54 | |||
| e19536ce99 | |||
| b92ab0bf1d | |||
| bc049a3c77 | |||
| a026a610a1 | |||
| e71a17cbce | |||
| 86c09f3b25 | |||
| 788ecee65d | |||
| 98cbb07845 | |||
| 93bab6d5eb | |||
| 99f79b7616 | |||
| 47dc988e49 | |||
| 072de12cc1 | |||
| 77f755d07c | |||
| bc5f9b1085 | |||
| 4660025775 | |||
| 336cc875a1 | |||
| d943b2ebc2 | |||
| f982b51ff6 | |||
| 5b7a33f48b | |||
| 753e521a41 | |||
| d479f36662 | |||
| 6984f78746 | |||
| 030f337e74 | |||
| ae3b0c411b | |||
| f838795983 | |||
| 1db430f997 | |||
| fdb8816b5e |
+37
@@ -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
|
||||||
+66
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin CoroutineClient|CurlClient
|
||||||
|
*/
|
||||||
|
class Client
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private CoroutineClient|CurlClient $abstracts;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
* @param int $port
|
||||||
|
* @param bool $isSsl
|
||||||
|
*/
|
||||||
|
public function __construct(string $host, int $port, bool $isSsl = false)
|
||||||
|
{
|
||||||
|
if (class_exists(Coroutine::class) && Coroutine::getCid() > -1) {
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __get(string $name)
|
||||||
|
{
|
||||||
|
// TODO: Implement __get() method.
|
||||||
|
$getter = 'get' . ucfirst($name);
|
||||||
|
if (method_exists($this, $getter)) {
|
||||||
|
return $this->$getter();
|
||||||
|
} else if (method_exists($this, $name)) {
|
||||||
|
return $this->$name();
|
||||||
|
} else if (property_exists($this, $name)) {
|
||||||
|
return $this->$name;
|
||||||
|
} else {
|
||||||
|
throw new Exception('Property|Method "' . $name . '" does not exist.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,589 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
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]+=.*)?)?/');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ClientAbstracts
|
||||||
|
* @package Http\Handler\Client
|
||||||
|
*/
|
||||||
|
abstract class ClientAbstracts implements IClient
|
||||||
|
{
|
||||||
|
|
||||||
|
const string POST = 'post';
|
||||||
|
const string UPLOAD = 'upload';
|
||||||
|
const string GET = 'get';
|
||||||
|
const string DELETE = 'delete';
|
||||||
|
const string OPTIONS = 'options';
|
||||||
|
const string HEAD = 'head';
|
||||||
|
const string PUT = 'put';
|
||||||
|
|
||||||
|
public string $host = '';
|
||||||
|
protected array $header = [];
|
||||||
|
protected int $timeout = 0;
|
||||||
|
protected string $method = 'get';
|
||||||
|
protected bool $isSSL = FALSE;
|
||||||
|
protected string $agent = '';
|
||||||
|
public string $ssl_cert_file = '';
|
||||||
|
public string $ssl_key_file = '';
|
||||||
|
public string $ca = '';
|
||||||
|
private int $port = 80;
|
||||||
|
protected int $num = 0;
|
||||||
|
private ?array $_responseHeader = [];
|
||||||
|
public int $statusCode = 200;
|
||||||
|
protected int $retryNum = 0;
|
||||||
|
protected int $retryTimeout = 0;
|
||||||
|
private bool $verifyPeer = TRUE;
|
||||||
|
public string $proxyHost = '';
|
||||||
|
public int $proxyPort = 0;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
public ?string $body;
|
||||||
|
|
||||||
|
|
||||||
|
protected string|array|null $_data = NULL;
|
||||||
|
|
||||||
|
public int $connect_timeout = 1;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|CurlHandle
|
||||||
|
*/
|
||||||
|
protected mixed $client;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $retryNum
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withRetryNum(int $retryNum): static
|
||||||
|
{
|
||||||
|
$this->retryNum = $retryNum;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $proxy
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withProxyHost(string $proxy): static
|
||||||
|
{
|
||||||
|
$this->proxyHost = $proxy;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $proxy
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withProxyPort(int $proxy): static
|
||||||
|
{
|
||||||
|
$this->proxyPort = $proxy;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $retryTimeout
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withRetryTimeout(int $retryTimeout): static
|
||||||
|
{
|
||||||
|
$this->retryTimeout = $retryTimeout;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $bool
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withVerifyPeer(bool $bool): static
|
||||||
|
{
|
||||||
|
$this->verifyPeer = $bool;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getResponseHeaders(): array
|
||||||
|
{
|
||||||
|
return $this->_responseHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return string|int|null
|
||||||
|
*/
|
||||||
|
public function getResponseHeader(string $key): null|string|int
|
||||||
|
{
|
||||||
|
return $this->_responseHeader[$key] ?? NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null|array $responseHeader
|
||||||
|
*/
|
||||||
|
public function setResponseHeader(?array $responseHeader): void
|
||||||
|
{
|
||||||
|
$this->_responseHeader = $responseHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
* @param int $port
|
||||||
|
* @param false $isSSL
|
||||||
|
*/
|
||||||
|
public function __construct(string $host, int $port, bool $isSSL = FALSE)
|
||||||
|
{
|
||||||
|
$this->withHost($host)->withPort($port)->withIsSSL($isSSL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function post(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::POST, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function put(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::PUT, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $contentType
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withContentType(string $contentType): static
|
||||||
|
{
|
||||||
|
$this->header['Content-Type'] = $contentType;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function head(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::HEAD, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function get(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
if (is_array($params)) {
|
||||||
|
$params = http_build_query($params);
|
||||||
|
}
|
||||||
|
$this->request(self::GET, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function option(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::OPTIONS, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function delete(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::DELETE, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function options(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::OPTIONS, $path, $params);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*/
|
||||||
|
public function upload(string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
$this->request(self::UPLOAD, $path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function getHostPort(): int
|
||||||
|
{
|
||||||
|
if (!empty($this->getPort())) {
|
||||||
|
return $this->getPort();
|
||||||
|
}
|
||||||
|
$port = 80;
|
||||||
|
if ($this->isSSL)
|
||||||
|
$port = 443;
|
||||||
|
return $port;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
protected function withHost(string $host): static
|
||||||
|
{
|
||||||
|
$this->host = $host;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed|null
|
||||||
|
*/
|
||||||
|
public function getContentType(): ?string
|
||||||
|
{
|
||||||
|
return $this->header['Content-Type'] ?? $this->header['content-type'] ?? NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $header
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withHeader(array $header): static
|
||||||
|
{
|
||||||
|
$this->header = $header;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $header
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withHeaders(array $header): static
|
||||||
|
{
|
||||||
|
if (empty($header)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
foreach ($header as $key => $val) {
|
||||||
|
$this->header[$key] = $val;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @param string|array $value
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withAddedHeader(string $key, string|array $value): static
|
||||||
|
{
|
||||||
|
$this->header[$key] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $value
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withTimeout(int $value): static
|
||||||
|
{
|
||||||
|
$this->timeout = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Closure|null $value
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withCallback(?Closure $value): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function withMethod(string $value): static
|
||||||
|
{
|
||||||
|
$this->method = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $isSSL
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withIsSSL(bool $isSSL): static
|
||||||
|
{
|
||||||
|
$this->isSSL = $isSSL;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $agent
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withAgent(string $agent): static
|
||||||
|
{
|
||||||
|
$this->agent = $agent;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_cert_file
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withSslCertFile(string $ssl_cert_file): static
|
||||||
|
{
|
||||||
|
$this->ssl_cert_file = $ssl_cert_file;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_key_file
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withSslKeyFile(string $ssl_key_file): static
|
||||||
|
{
|
||||||
|
$this->ssl_key_file = $ssl_key_file;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_key_file
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function withCa(string $ssl_key_file): static
|
||||||
|
{
|
||||||
|
$this->ca = $ssl_key_file;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getPort(): int
|
||||||
|
{
|
||||||
|
if ($this->isSSL) {
|
||||||
|
return 443;
|
||||||
|
}
|
||||||
|
if (empty($this->port)) {
|
||||||
|
return 80;
|
||||||
|
}
|
||||||
|
return $this->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $port
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
private function withPort(int $port): static
|
||||||
|
{
|
||||||
|
$this->port = $port;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $data
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withBody(?string $data): static
|
||||||
|
{
|
||||||
|
$this->_data = $data;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $connect_timeout
|
||||||
|
* @return ClientAbstracts
|
||||||
|
*/
|
||||||
|
public function withConnectTimeout(int $connect_timeout): static
|
||||||
|
{
|
||||||
|
$this->connect_timeout = $connect_timeout;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
* @return string|string[]
|
||||||
|
*/
|
||||||
|
protected function replaceHost(string $host): array|string
|
||||||
|
{
|
||||||
|
if ($this->isHttp($host)) {
|
||||||
|
return str_replace('http://', '', $host);
|
||||||
|
}
|
||||||
|
if ($this->isHttps($host)) {
|
||||||
|
return str_replace('https://', '', $host);
|
||||||
|
}
|
||||||
|
return $host;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @return false|int
|
||||||
|
*/
|
||||||
|
protected function checkIsIp(string $url): bool|int
|
||||||
|
{
|
||||||
|
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isHttp(string $url): bool
|
||||||
|
{
|
||||||
|
return str_starts_with($url, 'http://');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isHttps(string $url): bool
|
||||||
|
{
|
||||||
|
return str_starts_with($url, 'https://');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array|string $newData
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
protected function mergeParams(array|string $newData): ?string
|
||||||
|
{
|
||||||
|
if (is_array($newData)) {
|
||||||
|
return json_encode($newData, JSON_UNESCAPED_UNICODE);
|
||||||
|
}
|
||||||
|
return $newData;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* check isPost Request
|
||||||
|
*/
|
||||||
|
protected function isPost(): bool
|
||||||
|
{
|
||||||
|
return strtolower($this->method) === self::POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* check isPost Request
|
||||||
|
*/
|
||||||
|
protected function isUpload(): bool
|
||||||
|
{
|
||||||
|
return strtolower($this->method) === self::UPLOAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* check isGet Request
|
||||||
|
*/
|
||||||
|
protected function isGet(): bool
|
||||||
|
{
|
||||||
|
return strtolower($this->method) === self::GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array|string $arr
|
||||||
|
*
|
||||||
|
* @return array|string
|
||||||
|
* 将请求参数进行编码
|
||||||
|
*/
|
||||||
|
protected function paramEncode(array|string $arr): array|string
|
||||||
|
{
|
||||||
|
if (!is_array($arr)) {
|
||||||
|
return $arr;
|
||||||
|
}
|
||||||
|
$_tmp = [];
|
||||||
|
foreach ($arr as $Key => $val) {
|
||||||
|
$_tmp[$Key] = $val;
|
||||||
|
}
|
||||||
|
if ($this->isGet()) {
|
||||||
|
return http_build_query($_tmp);
|
||||||
|
}
|
||||||
|
return $_tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function matchHost(string $string): array
|
||||||
|
{
|
||||||
|
return [$this->host, $this->isSSL, $string];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function joinGetParams(string $path, array|string $params): string
|
||||||
|
{
|
||||||
|
if (empty($params)) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
if (!is_string($params)) {
|
||||||
|
$params = http_build_query($params);
|
||||||
|
}
|
||||||
|
if (str_contains($path, '?')) {
|
||||||
|
[$path, $getParams] = explode('?', $path);
|
||||||
|
}
|
||||||
|
if (empty($getParams)) {
|
||||||
|
return $path . '?' . $params;
|
||||||
|
}
|
||||||
|
return $path . '?' . $params . '&' . $getParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/5/24 0024
|
||||||
|
* Time: 11:34
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
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|string $params
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function request(string $method, $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
if (!str_starts_with($path, '/')) {
|
||||||
|
$path = '/' . $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$host = $this->host;
|
||||||
|
if (!preg_match('/(\d{1,3}\.){3}\d{1,3}/', $host)) {
|
||||||
|
$this->withAddedHeader('Host', $host);
|
||||||
|
}
|
||||||
|
$this->withMethod($method)
|
||||||
|
->coroutine(
|
||||||
|
$path,
|
||||||
|
$this->paramEncode($params)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withCAInfo(string $path): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @param array|string $data
|
||||||
|
*/
|
||||||
|
private function coroutine(string $url, array|string $data = []): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->generate_client($this->host, $this->isSSL);
|
||||||
|
if ($this->client->statusCode < 0) {
|
||||||
|
throw new Exception($this->client->errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->execute($url, $data);
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
$this->statusCode = -1;
|
||||||
|
$this->body = json_encode(['code' => 500, 'message' => $exception->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $data
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function execute(string $path, array|string $data): void
|
||||||
|
{
|
||||||
|
$this->client->execute($this->setParams($path, $data));
|
||||||
|
if (in_array($this->client->getStatusCode(), [502, 404])) {
|
||||||
|
$this->retry($path, $data);
|
||||||
|
} else {
|
||||||
|
$this->statusCode = $this->client->getStatusCode();
|
||||||
|
$this->body = $this->client->getBody();
|
||||||
|
$this->setResponseHeader($this->client->headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $data
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function retry(string $path, array|string $data): void
|
||||||
|
{
|
||||||
|
if (($this->num += 1) <= $this->retryNum) {
|
||||||
|
sleep($this->retryTimeout);
|
||||||
|
|
||||||
|
$this->execute($path, $data);
|
||||||
|
} else {
|
||||||
|
$this->statusCode = $this->client->statusCode;
|
||||||
|
$this->body = $this->client->errMsg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
* @param bool $isHttps
|
||||||
|
*/
|
||||||
|
private function generate_client(string $host, bool $isHttps): 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->agent)) {
|
||||||
|
$this->withAddedHeader('User-Agent', $this->agent);
|
||||||
|
}
|
||||||
|
$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
@@ -0,0 +1,230 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CurlClient
|
||||||
|
*
|
||||||
|
* @package Http\Handler\Client
|
||||||
|
*/
|
||||||
|
class CurlClient extends ClientAbstracts
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method
|
||||||
|
* @param string $path
|
||||||
|
* @param array|string $params
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function request(string $method, string $path, array|string $params = []): void
|
||||||
|
{
|
||||||
|
if (!str_starts_with($path, '/')) {
|
||||||
|
$path = '/' . $path;
|
||||||
|
}
|
||||||
|
if ($method == self::GET) {
|
||||||
|
$path = $this->joinGetParams($path, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getCurlHandler($path, $method, $params);
|
||||||
|
|
||||||
|
$this->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param string $method
|
||||||
|
* @param $params
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function getCurlHandler(string $path, string $method, $params): void
|
||||||
|
{
|
||||||
|
$host = $this->isSSL ? 'https://' . $this->host : 'http://' . $this->host;
|
||||||
|
if ($this->getPort() != 443 && $this->getPort() != 80) {
|
||||||
|
$host .= ':' . $this->getPort();
|
||||||
|
}
|
||||||
|
$this->do(curl_init($host . $path), $host . $path, $method);
|
||||||
|
if ($this->isSSL) {
|
||||||
|
$this->curlHandlerSslSet();
|
||||||
|
}
|
||||||
|
$contents = $this->_data;
|
||||||
|
if (empty($params) && empty($contents)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!empty($contents)) {
|
||||||
|
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
|
||||||
|
} else if ($method === self::UPLOAD) {
|
||||||
|
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
|
||||||
|
} else if ($method === self::POST) {
|
||||||
|
if (is_array($params)) {
|
||||||
|
$params = http_build_query($params);
|
||||||
|
}
|
||||||
|
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function curlHandlerSslSet(): void
|
||||||
|
{
|
||||||
|
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->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 mixed $resource
|
||||||
|
* @param string $path
|
||||||
|
* @param string $method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function do(mixed $resource, string $path, string $method): void
|
||||||
|
{
|
||||||
|
curl_setopt($resource, CURLOPT_URL, $path);
|
||||||
|
curl_setopt($resource, CURLOPT_TIMEOUT, $this->timeout); // 超时设置
|
||||||
|
curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout); // 超时设置
|
||||||
|
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->agent)) {
|
||||||
|
curl_setopt($resource, CURLOPT_USERAGENT, $this->agent);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
[$proxy, $port] = [$this->proxyHost, $this->proxyPort];
|
||||||
|
if (!empty($proxy) && $port > 0) {
|
||||||
|
curl_setopt($resource, CURLOPT_PROXYPORT, $port);
|
||||||
|
curl_setopt($resource, CURLOPT_PROXY, $proxy);
|
||||||
|
}
|
||||||
|
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
|
||||||
|
$this->client = $resource;
|
||||||
|
if (!empty($this->caPath)) {
|
||||||
|
curl_setopt($this->client, CURLOPT_CAINFO, $this->caPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private string $caPath = '';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withCAInfo(string $path): static
|
||||||
|
{
|
||||||
|
$this->caPath = $path;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function execute(): void
|
||||||
|
{
|
||||||
|
$output = curl_exec($this->client);
|
||||||
|
if ($output !== FALSE) {
|
||||||
|
$this->explode($output);
|
||||||
|
} else {
|
||||||
|
$this->statusCode = curl_errno($this->client);
|
||||||
|
$this->body = curl_error($this->client);
|
||||||
|
}
|
||||||
|
$this->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function retry(): void
|
||||||
|
{
|
||||||
|
if (($this->num += 1) <= $this->retryNum) {
|
||||||
|
sleep($this->retryTimeout);
|
||||||
|
|
||||||
|
$this->execute();
|
||||||
|
} else {
|
||||||
|
$this->statusCode = curl_errno($this->client);
|
||||||
|
$this->body = curl_error($this->client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function close(): void
|
||||||
|
{
|
||||||
|
if (PHP_VERSION < '8.0') {
|
||||||
|
curl_close($this->client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $output
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function explode(string $output): void
|
||||||
|
{
|
||||||
|
// 获取 HTTP 状态码
|
||||||
|
$statusCode = +curl_getinfo($this->client, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
// 获取 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
@@ -0,0 +1,108 @@
|
|||||||
|
<?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 $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 array|string $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function parse(array|string $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
|
||||||
|
*/
|
||||||
|
private static function ifElse(string $t, mixed $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
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+178
-181
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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.
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
trait TSwooleClient
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function settings(): array
|
||||||
|
{
|
||||||
|
$sslCert = $this->ssl_cert_file;
|
||||||
|
$sslKey = $this->ssl_key_file;
|
||||||
|
$sslCa = $this->ca;
|
||||||
|
|
||||||
|
$params = [];
|
||||||
|
if ($this->connect_timeout > 0) {
|
||||||
|
$params['timeout'] = $this->connect_timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$proxy, $port] = [$this->proxyHost, $this->proxyPort];
|
||||||
|
if (!empty($proxy) && $port > 0) {
|
||||||
|
$params['http_proxy_host'] = $proxy;
|
||||||
|
$params['http_proxy_port'] = $port;
|
||||||
|
}
|
||||||
|
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
$params['ssl_host_name'] = $this->host;
|
||||||
|
$params['ssl_cert_file'] = $this->ssl_cert_file;
|
||||||
|
$params['ssl_key_file'] = $this->ssl_key_file;
|
||||||
|
$params['ssl_verify_peer'] = TRUE;
|
||||||
|
$params['ssl_cafile'] = $sslCa;
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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
-26
@@ -1,26 +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-redis": "*",
|
"ext-swoole": "*"
|
||||||
"ext-swoole": "*",
|
},
|
||||||
"psr/http-client": "^1.0",
|
"autoload": {
|
||||||
"psr/http-message": "^1.0"
|
"psr-4": {
|
||||||
},
|
"Kiri\\": "./"
|
||||||
"autoload": {
|
}
|
||||||
"psr-4": {
|
},
|
||||||
"Http\\Client\\": "src/"
|
"require-dev": {
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"require-dev": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,779 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Http\Client;
|
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Http\Message\Stream;
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
|
||||||
use Kiri\Context;
|
|
||||||
use Kiri\Core\Help;
|
|
||||||
use Psr\Http\Message\StreamInterface;
|
|
||||||
use Swoole\Coroutine\System;
|
|
||||||
|
|
||||||
defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/');
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ClientAbstracts
|
|
||||||
* @package Http\Handler\Client
|
|
||||||
*/
|
|
||||||
abstract class ClientAbstracts implements IClient
|
|
||||||
{
|
|
||||||
|
|
||||||
const POST = 'post';
|
|
||||||
const UPLOAD = 'upload';
|
|
||||||
const GET = 'get';
|
|
||||||
const DELETE = 'delete';
|
|
||||||
const OPTIONS = 'options';
|
|
||||||
const HEAD = 'head';
|
|
||||||
const PUT = 'put';
|
|
||||||
|
|
||||||
private string $host = '';
|
|
||||||
|
|
||||||
private array $header = [];
|
|
||||||
|
|
||||||
private int $timeout = 0;
|
|
||||||
|
|
||||||
private string $method = 'get';
|
|
||||||
|
|
||||||
private bool $isSSL = FALSE;
|
|
||||||
private string $agent = '';
|
|
||||||
|
|
||||||
private string $ssl_cert_file = '';
|
|
||||||
private string $ssl_key_file = '';
|
|
||||||
private string $ca = '';
|
|
||||||
private int $port = 80;
|
|
||||||
|
|
||||||
|
|
||||||
private array $_responseHeader = [];
|
|
||||||
|
|
||||||
|
|
||||||
private int $statusCode = 200;
|
|
||||||
|
|
||||||
|
|
||||||
private bool $VERIFYPEER = TRUE;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string|null
|
|
||||||
*/
|
|
||||||
protected ?string $body;
|
|
||||||
|
|
||||||
|
|
||||||
private ?StreamInterface $_data = NULL;
|
|
||||||
|
|
||||||
private int $connect_timeout = 1;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
|
|
||||||
*/
|
|
||||||
protected mixed $client;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $bool
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function withVerifyPeer($bool): static
|
|
||||||
{
|
|
||||||
$this->VERIFYPEER = $bool;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function getVerifyPeer(): bool
|
|
||||||
{
|
|
||||||
return $this->VERIFYPEER;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getStatusCode(): int
|
|
||||||
{
|
|
||||||
return $this->statusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getResponseHeaders(): array
|
|
||||||
{
|
|
||||||
return $this->_responseHeader;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return string|int|null
|
|
||||||
*/
|
|
||||||
public function getResponseHeader(string $key): null|string|int
|
|
||||||
{
|
|
||||||
return $this->_responseHeader[$key] ?? NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $responseHeader
|
|
||||||
*/
|
|
||||||
public function setResponseHeader(array $responseHeader): void
|
|
||||||
{
|
|
||||||
$this->_responseHeader = $responseHeader;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $statusCode
|
|
||||||
*/
|
|
||||||
public function setStatusCode(int $statusCode): void
|
|
||||||
{
|
|
||||||
$this->statusCode = $statusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getBody(): string|null
|
|
||||||
{
|
|
||||||
return $this->body;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ?string $body
|
|
||||||
*/
|
|
||||||
public function setBody(?string $body): void
|
|
||||||
{
|
|
||||||
$this->body = $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $host
|
|
||||||
* @param $port
|
|
||||||
* @param false $isSSL
|
|
||||||
*/
|
|
||||||
public function __construct($host, $port, bool $isSSL = FALSE)
|
|
||||||
{
|
|
||||||
$this->withHost($host)->withPort($port)->withIsSSL($isSSL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function post(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::POST, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function put(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::PUT, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $contentType
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withContentType(string $contentType): static
|
|
||||||
{
|
|
||||||
$this->header['Content-Type'] = $contentType;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function head(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::HEAD, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function get(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::GET, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function option(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::OPTIONS, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function delete(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::DELETE, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function options(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::OPTIONS, $path, $params);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param array $params
|
|
||||||
*/
|
|
||||||
public function upload(string $path, array $params = []): void
|
|
||||||
{
|
|
||||||
$this->request(self::UPLOAD, $path, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getHost(): string
|
|
||||||
{
|
|
||||||
return $this->host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
#[Pure] protected function getHostPort(): int
|
|
||||||
{
|
|
||||||
if (!empty($this->getPort())) {
|
|
||||||
return $this->getPort();
|
|
||||||
}
|
|
||||||
$port = 80;
|
|
||||||
if ($this->isSSL()) $port = 443;
|
|
||||||
return $port;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $host
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
private function withHost(string $host): static
|
|
||||||
{
|
|
||||||
$this->host = $host;
|
|
||||||
if (Context::inCoroutine()) {
|
|
||||||
$this->host = System::gethostbyname($host);
|
|
||||||
}
|
|
||||||
return $this->withAddedHeader('Host', $host);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getHeader(): array
|
|
||||||
{
|
|
||||||
return $this->header;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function getContentType(): ?string
|
|
||||||
{
|
|
||||||
return $this->header['Content-Type'] ?? $this->header['content-type'] ?? NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $header
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withHeader(array $header): static
|
|
||||||
{
|
|
||||||
$this->header = $header;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $header
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withHeaders(array $header): static
|
|
||||||
{
|
|
||||||
if (empty($header)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
foreach ($header as $key => $val) {
|
|
||||||
$this->header[$key] = $val;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $value
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withAddedHeader($key, $value): static
|
|
||||||
{
|
|
||||||
$this->header[$key] = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getTimeout(): int
|
|
||||||
{
|
|
||||||
return $this->timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $value
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withTimeout(int $value): static
|
|
||||||
{
|
|
||||||
$this->timeout = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Closure|null $value
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withCallback(?Closure $value): static
|
|
||||||
{
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMethod(): string
|
|
||||||
{
|
|
||||||
return $this->method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $value
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function withMethod(string $value): static
|
|
||||||
{
|
|
||||||
$this->method = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isSSL(): bool
|
|
||||||
{
|
|
||||||
return $this->isSSL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $isSSL
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withIsSSL(bool $isSSL): static
|
|
||||||
{
|
|
||||||
$this->isSSL = $isSSL;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAgent(): string
|
|
||||||
{
|
|
||||||
return $this->agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $agent
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withAgent(string $agent): static
|
|
||||||
{
|
|
||||||
$this->agent = $agent;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslCertFile(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_cert_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_cert_file
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withSslCertFile(string $ssl_cert_file): static
|
|
||||||
{
|
|
||||||
$this->ssl_cert_file = $ssl_cert_file;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslKeyFile(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_key_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_key_file
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withSslKeyFile(string $ssl_key_file): static
|
|
||||||
{
|
|
||||||
$this->ssl_key_file = $ssl_key_file;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCa(): string
|
|
||||||
{
|
|
||||||
return $this->ca;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_key_file
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function withCa(string $ssl_key_file): static
|
|
||||||
{
|
|
||||||
$this->ca = $ssl_key_file;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
#[Pure] public function getPort(): int
|
|
||||||
{
|
|
||||||
if ($this->isSSL()) {
|
|
||||||
return 443;
|
|
||||||
}
|
|
||||||
if (empty($this->port)) {
|
|
||||||
return 80;
|
|
||||||
}
|
|
||||||
return $this->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $port
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
private function withPort(int $port): static
|
|
||||||
{
|
|
||||||
$this->port = $port;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return StreamInterface
|
|
||||||
*/
|
|
||||||
public function getData(): StreamInterface
|
|
||||||
{
|
|
||||||
if (!$this->_data) {
|
|
||||||
$this->_data = new Stream();
|
|
||||||
}
|
|
||||||
return $this->_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|StreamInterface $data
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withBody(string|StreamInterface $data): static
|
|
||||||
{
|
|
||||||
if (is_string($data)) {
|
|
||||||
$data = new Stream($data);
|
|
||||||
}
|
|
||||||
$this->_data = $data;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getConnectTimeout(): int
|
|
||||||
{
|
|
||||||
return $this->connect_timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $connect_timeout
|
|
||||||
* @return ClientAbstracts
|
|
||||||
*/
|
|
||||||
public function withConnectTimeout(int $connect_timeout): static
|
|
||||||
{
|
|
||||||
$this->connect_timeout = $connect_timeout;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $host
|
|
||||||
* @return string|string[]
|
|
||||||
*/
|
|
||||||
protected function replaceHost($host): array|string
|
|
||||||
{
|
|
||||||
if ($this->isHttp($host)) {
|
|
||||||
return str_replace('http://', '', $host);
|
|
||||||
}
|
|
||||||
if ($this->isHttps($host)) {
|
|
||||||
return str_replace('https://', '', $host);
|
|
||||||
}
|
|
||||||
return $host;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return false|int
|
|
||||||
*/
|
|
||||||
protected function checkIsIp($url): bool|int
|
|
||||||
{
|
|
||||||
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function isHttp($url): bool
|
|
||||||
{
|
|
||||||
return str_starts_with($url, 'http://');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function isHttps($url): bool
|
|
||||||
{
|
|
||||||
return str_starts_with($url, 'https://');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $newData
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function mergeParams($newData): string
|
|
||||||
{
|
|
||||||
if (empty($data)) return '';
|
|
||||||
if (!is_string($newData)) {
|
|
||||||
return $this->toRequest($newData);
|
|
||||||
}
|
|
||||||
return $newData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function toRequest($data): string
|
|
||||||
{
|
|
||||||
if (is_string($data)) {
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
$contentType = 'application/x-www-form-urlencoded';
|
|
||||||
if (isset($this->header['Content-Type'])) {
|
|
||||||
$contentType = $this->header['Content-Type'];
|
|
||||||
} else if (isset($this->header['content-type'])) {
|
|
||||||
$contentType = $this->header['content-type'];
|
|
||||||
}
|
|
||||||
if (str_contains($contentType, 'json')) {
|
|
||||||
return Help::toJson($data);
|
|
||||||
} else if (str_contains($contentType, 'xml')) {
|
|
||||||
return Help::toXml($data);
|
|
||||||
} else {
|
|
||||||
return http_build_query($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @param $body
|
|
||||||
* @return array|string|null
|
|
||||||
*/
|
|
||||||
protected function resolve($data, $body): array|string|null
|
|
||||||
{
|
|
||||||
if (is_array($body)) {
|
|
||||||
return $body;
|
|
||||||
}
|
|
||||||
$type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html';
|
|
||||||
if (str_contains($type, 'text/html')) {
|
|
||||||
return $body;
|
|
||||||
} else if (str_contains($type, 'json')) {
|
|
||||||
return json_decode($body, TRUE);
|
|
||||||
} else if (str_contains($type, 'xml')) {
|
|
||||||
return Help::xmlToArray($body);
|
|
||||||
} else if (str_contains($type, 'plain')) {
|
|
||||||
return Help::toArray($body);
|
|
||||||
}
|
|
||||||
return $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* check isPost Request
|
|
||||||
*/
|
|
||||||
#[Pure] protected function isPost(): bool
|
|
||||||
{
|
|
||||||
return strtolower($this->method) === self::POST;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* check isPost Request
|
|
||||||
*/
|
|
||||||
#[Pure] protected function isUpload(): bool
|
|
||||||
{
|
|
||||||
return strtolower($this->method) === self::UPLOAD;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* check isGet Request
|
|
||||||
*/
|
|
||||||
#[Pure] protected function isGet(): bool
|
|
||||||
{
|
|
||||||
return strtolower($this->method) === self::GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $arr
|
|
||||||
*
|
|
||||||
* @return array|string
|
|
||||||
* 将请求参数进行编码
|
|
||||||
*/
|
|
||||||
#[Pure] protected function paramEncode($arr): array|string
|
|
||||||
{
|
|
||||||
if (!is_array($arr)) {
|
|
||||||
return $arr;
|
|
||||||
}
|
|
||||||
$_tmp = [];
|
|
||||||
foreach ($arr as $Key => $val) {
|
|
||||||
$_tmp[$Key] = $val;
|
|
||||||
}
|
|
||||||
if ($this->isGet()) {
|
|
||||||
return http_build_query($_tmp);
|
|
||||||
}
|
|
||||||
return $_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function matchHost(string $string): array
|
|
||||||
{
|
|
||||||
if (($parse = isUrl($string, TRUE)) === FALSE) {
|
|
||||||
return $this->defaultString($string);
|
|
||||||
}
|
|
||||||
[$isHttps, $domain, $port, $path] = $parse;
|
|
||||||
if (str_contains($domain, ':' . $port)) {
|
|
||||||
$domain = str_replace(':' . $port, '', $domain);
|
|
||||||
}
|
|
||||||
$this->port = $isHttps ? 443 : $this->port;
|
|
||||||
if (isIp($domain)) {
|
|
||||||
$this->host = $domain;
|
|
||||||
} else if (Context::inCoroutine()) {
|
|
||||||
$this->host = System::gethostbyname($domain) ?? $domain;
|
|
||||||
} else {
|
|
||||||
$this->host = $domain;
|
|
||||||
}
|
|
||||||
$this->header['Host'] = $domain;
|
|
||||||
if (!str_starts_with($path, '/')) {
|
|
||||||
$path = '/' . $path;
|
|
||||||
}
|
|
||||||
return [$this->host, $isHttps, $path];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $string
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function defaultString($string): array
|
|
||||||
{
|
|
||||||
$host = $this->getHost();
|
|
||||||
if ($string == '/') {
|
|
||||||
$string = '/';
|
|
||||||
} else if (!str_starts_with($string, '/')) {
|
|
||||||
$string = '/' . $string;
|
|
||||||
}
|
|
||||||
return [$host, $this->isSSL(), $string];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
* @param $params
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function joinGetParams($path, $params): string
|
|
||||||
{
|
|
||||||
if (empty($params)) {
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
if (!is_string($params)) {
|
|
||||||
$params = http_build_query($params);
|
|
||||||
}
|
|
||||||
if (str_contains($path, '?')) {
|
|
||||||
[$path, $getParams] = explode('?', $path);
|
|
||||||
}
|
|
||||||
if (empty($getParams)) {
|
|
||||||
return $path . '?' . $params;
|
|
||||||
}
|
|
||||||
return $path . '?' . $params . '&' . $getParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,129 +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 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,208 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user