Compare commits
83 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1db430f997 | |||
| fdb8816b5e | |||
| 2623deb63f | |||
| 98d9cee8b0 | |||
| cec004b670 | |||
| f268b705f7 | |||
| ac279a257a | |||
| 89ba6effeb | |||
| 29e520b412 | |||
| eec7cb7dcc | |||
| 6c71437e9e | |||
| 33fc0ae396 | |||
| 23f85ef5f0 | |||
| 372dbbb424 | |||
| d07dbf1c8d | |||
| 6ffe1af6d2 | |||
| ca7a4494d8 | |||
| 4f99ec5756 | |||
| 1d1b36155a | |||
| 00da068b53 | |||
| 08793bcf76 | |||
| f5ae86c3d2 | |||
| 4856e76e6f | |||
| 09dc2b4980 | |||
| 510daa1bb6 | |||
| 4dc1120aaf | |||
| 9076b65080 | |||
| adf09f5695 | |||
| 06290f5c9b | |||
| 196cb42e6e | |||
| a0f197d98d | |||
| c2d5ab5e1b | |||
| 597cacd191 | |||
| f48b6bd5d2 | |||
| 2baeb20b8d | |||
| 8d685b93e2 | |||
| 90cada46c4 | |||
| 0f5d277532 | |||
| f230b03ce0 | |||
| beb7c57809 | |||
| 82f046f38b | |||
| 080dbdcf0d | |||
| 04a1bc2fcb | |||
| 61606b2b13 | |||
| 360c88b8e7 | |||
| 6f0c564e97 | |||
| 6b7284085d | |||
| eb2659e777 | |||
| 2f7b17e876 | |||
| 8ac0595fab | |||
| 3784e25f64 | |||
| bd74702a21 | |||
| 6098045ab3 | |||
| 4b7ebb2572 | |||
| 86c76a7587 | |||
| 895533a04f | |||
| 9d24b3314b | |||
| 80a2db8974 | |||
| 6434be4f52 | |||
| eec1efcb61 | |||
| 3491ff8df2 | |||
| a21f1090d0 | |||
| 65958a0ded | |||
| b945c19200 | |||
| 2165797410 | |||
| d604083162 | |||
| b59463c5e9 | |||
| 8202c1861f | |||
| d604d5f46a | |||
| bf7d401158 | |||
| 5e271a6e0a | |||
| a3c89b419b | |||
| 37e96f2853 | |||
| 64f0b9f9d0 | |||
| 3693fd40f6 | |||
| b0fe5ab4c4 | |||
| 791be944a3 | |||
| 43eab7ae27 | |||
| 0a54c021b9 | |||
| af9b88f114 | |||
| 336237d338 | |||
| d075dd73a4 | |||
| 40c946b58e |
+178
@@ -0,0 +1,178 @@
|
|||||||
|
<?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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,13 @@ use Kiri\Context;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin CoroutineClient|Curl
|
* @mixin CoroutineClient|CurlClient
|
||||||
*/
|
*/
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private CoroutineClient|Curl $abstracts;
|
private CoroutineClient|CurlClient|AsyncClient $abstracts;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +25,7 @@ class Client
|
|||||||
if (Context::inCoroutine()) {
|
if (Context::inCoroutine()) {
|
||||||
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
|
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
|
||||||
} else {
|
} else {
|
||||||
$this->abstracts = new Curl($host, $port, $isSsl);
|
$this->abstracts = new CurlClient($host, $port, $isSsl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,779 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,8 +10,6 @@ declare(strict_types=1);
|
|||||||
namespace Http\Client;
|
namespace Http\Client;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Message\Response;
|
|
||||||
use Http\Message\Stream;
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Abstracts\Logger;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
@@ -24,6 +22,8 @@ use Swoole\Coroutine\Http\Client as SwowClient;
|
|||||||
class CoroutineClient extends ClientAbstracts
|
class CoroutineClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use TSwooleClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @param $path
|
* @param $path
|
||||||
@@ -41,6 +41,16 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withCAInfo($path): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param array|string $data
|
* @param array|string $data
|
||||||
@@ -53,15 +63,13 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
if ($this->client->statusCode < 0) {
|
if ($this->client->statusCode < 0) {
|
||||||
throw new Exception($this->client->errMsg);
|
throw new Exception($this->client->errMsg);
|
||||||
}
|
}
|
||||||
$body = (new Response())->withStatus($this->client->getStatusCode())
|
$this->setStatusCode($this->client->getStatusCode());
|
||||||
->withHeaders($this->client->getHeaders())
|
$this->setBody($this->client->getBody());
|
||||||
->withBody(new Stream($this->client->getBody()));
|
$this->setResponseHeader($this->client->headers);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
||||||
$body = (new Response())->withStatus(-1)->withHeaders([])
|
$this->setStatusCode(-1);
|
||||||
->withBody(new Stream(jTraceEx($exception)));
|
$this->setBody(jTraceEx($exception));
|
||||||
} finally {
|
|
||||||
$this->setBody($body);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +119,6 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -119,30 +126,4 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
{
|
{
|
||||||
$this->client->close();
|
$this->client->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+208
@@ -0,0 +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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+2
-2
@@ -11,13 +11,13 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.0",
|
"php": ">=8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-redis": "*",
|
"ext-swoole": "*",
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"psr/http-message": "^1.0"
|
"psr/http-message": "^1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Http\\Client\\": "src/"
|
"Http\\Client\\": "./"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -1,702 +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\ResponseInterface;
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ResponseInterface|null
|
|
||||||
*/
|
|
||||||
protected ?ResponseInterface $body;
|
|
||||||
|
|
||||||
|
|
||||||
private ?StreamInterface $_data = null;
|
|
||||||
|
|
||||||
private int $connect_timeout = 1;
|
|
||||||
|
|
||||||
|
|
||||||
protected mixed $client;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string|ResponseInterface|null
|
|
||||||
*/
|
|
||||||
public function getBody(): string|ResponseInterface|null
|
|
||||||
{
|
|
||||||
return $this->body;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ResponseInterface|null $body
|
|
||||||
*/
|
|
||||||
public function setBody(?ResponseInterface $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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-204
@@ -1,204 +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 Curl
|
|
||||||
* @package Http\Handler\Client
|
|
||||||
*/
|
|
||||||
class Curl 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->ssl_key)) {
|
|
||||||
if (!file_exists($this->ssl_key)) {
|
|
||||||
throw new Exception('SSL protocol certificate not found.');
|
|
||||||
}
|
|
||||||
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
|
||||||
}
|
|
||||||
if (!empty($this->ssl_cert)) {
|
|
||||||
if (!!file_exists($this->ssl_cert)) {
|
|
||||||
throw new Exception('SSL protocol certificate not found.');
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function execute(): void
|
|
||||||
{
|
|
||||||
$output = curl_exec($this->client);
|
|
||||||
if ($output === false) {
|
|
||||||
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client)));
|
|
||||||
} else {
|
|
||||||
$response = $this->explode($output);
|
|
||||||
}
|
|
||||||
$this->setBody($response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function close(): void
|
|
||||||
{
|
|
||||||
curl_close($this->client);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $output
|
|
||||||
* @return ResponseInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function explode($output): ResponseInterface
|
|
||||||
{
|
|
||||||
[$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));
|
|
||||||
|
|
||||||
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
|
|
||||||
->withBody(new Stream($body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user