2022-01-09 13:54:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2022-01-10 11:39:56 +08:00
|
|
|
namespace Kiri;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Closure;
|
2023-08-18 16:37:18 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-07-12 21:38:27 +08:00
|
|
|
use Swoole\Coroutine;
|
2022-01-09 13:54:34 +08:00
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
const POST = 'post';
|
|
|
|
|
const UPLOAD = 'upload';
|
|
|
|
|
const GET = 'get';
|
|
|
|
|
const DELETE = 'delete';
|
|
|
|
|
const OPTIONS = 'options';
|
|
|
|
|
const HEAD = 'head';
|
|
|
|
|
const PUT = 'put';
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private string $host = '';
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private array $header = [];
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private int $timeout = 0;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private string $method = 'get';
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private bool $isSSL = FALSE;
|
|
|
|
|
private string $agent = '';
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private string $ssl_cert_file = '';
|
|
|
|
|
private string $ssl_key_file = '';
|
|
|
|
|
private string $ca = '';
|
|
|
|
|
private int $port = 80;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
protected int $num = 0;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-05-31 07:46:01 +08:00
|
|
|
private ?array $_responseHeader = [];
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private int $statusCode = 200;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-03-01 18:47:45 +08:00
|
|
|
protected int $retryNum = 0;
|
2022-03-01 18:46:08 +08:00
|
|
|
|
2022-03-01 18:47:45 +08:00
|
|
|
protected int $retryTimeout = 0;
|
2022-03-01 18:46:08 +08:00
|
|
|
|
|
|
|
|
|
2022-05-31 07:46:01 +08:00
|
|
|
private bool $verifyPeer = TRUE;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @var string|null
|
|
|
|
|
*/
|
|
|
|
|
protected ?string $body;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
private string|array|null $_data = NULL;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
private int $connect_timeout = 1;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
|
|
|
|
|
*/
|
|
|
|
|
protected mixed $client;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-03-01 18:46:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param int $retryNum
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function withRetryNum(int $retryNum): static
|
|
|
|
|
{
|
|
|
|
|
$this->retryNum = $retryNum;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $retryTimeout
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function withRetryTimeout(int $retryTimeout): static
|
|
|
|
|
{
|
|
|
|
|
$this->retryTimeout = $retryTimeout;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getRetryNum(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->retryNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getRetryTimeout(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->retryTimeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $bool
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function withVerifyPeer($bool): static
|
|
|
|
|
{
|
2022-05-31 07:46:01 +08:00
|
|
|
$this->verifyPeer = $bool;
|
2022-02-21 15:17:42 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getVerifyPeer(): bool
|
|
|
|
|
{
|
2022-05-31 07:46:01 +08:00
|
|
|
return $this->verifyPeer;
|
2022-02-21 15:17:42 +08:00
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStatusCode(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->statusCode;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getResponseHeaders(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_responseHeader;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return string|int|null
|
|
|
|
|
*/
|
|
|
|
|
public function getResponseHeader(string $key): null|string|int
|
|
|
|
|
{
|
|
|
|
|
return $this->_responseHeader[$key] ?? NULL;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
2022-05-31 07:46:01 +08:00
|
|
|
* @param null|array $responseHeader
|
2022-02-21 15:17:42 +08:00
|
|
|
*/
|
2022-05-31 07:46:01 +08:00
|
|
|
public function setResponseHeader(?array $responseHeader): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->_responseHeader = $responseHeader;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
|
2022-02-21 15:17:42 +08:00
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function post(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::POST, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function put(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::PUT, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $contentType
|
|
|
|
|
* @return ClientAbstracts
|
|
|
|
|
*/
|
|
|
|
|
public function withContentType(string $contentType): static
|
|
|
|
|
{
|
|
|
|
|
$this->header['Content-Type'] = $contentType;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function head(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::HEAD, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function get(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
2023-07-12 21:38:27 +08:00
|
|
|
if (is_array($params)) {
|
|
|
|
|
$params = http_build_query($params);
|
|
|
|
|
}
|
2022-02-21 15:17:42 +08:00
|
|
|
$this->request(self::GET, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function option(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::OPTIONS, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function delete(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::DELETE, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function options(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::OPTIONS, $path, $params);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param array|string $params
|
|
|
|
|
*/
|
|
|
|
|
public function upload(string $path, array|string $params = []): void
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->request(self::UPLOAD, $path, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHost(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function getHostPort(): int
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
if (!empty($this->getPort())) {
|
|
|
|
|
return $this->getPort();
|
|
|
|
|
}
|
|
|
|
|
$port = 80;
|
|
|
|
|
if ($this->isSSL()) $port = 443;
|
|
|
|
|
return $port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $host
|
|
|
|
|
* @return ClientAbstracts
|
|
|
|
|
*/
|
2023-08-17 16:32:02 +08:00
|
|
|
protected function withHost(string $host): static
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$this->host = $host;
|
2023-08-18 16:37:18 +08:00
|
|
|
if (Context::inCoroutine() && !preg_match('/(\d{1,3}\.){3}\d{1,3}/', $host)) {
|
|
|
|
|
$this->host = System::gethostbyname($host);
|
|
|
|
|
$this->withAddedHeader('Host', $host);
|
|
|
|
|
}
|
2022-02-21 15:17:42 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
public function getPort(): int
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 21:38:27 +08:00
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getData(): ?string
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
return $this->_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-12 21:38:27 +08:00
|
|
|
* @param string|null $data
|
2022-02-21 15:17:42 +08:00
|
|
|
* @return ClientAbstracts
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
public function withBody(?string $data): static
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
$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
|
2022-07-08 17:40:25 +08:00
|
|
|
* @return string|null
|
2022-02-21 15:17:42 +08:00
|
|
|
*/
|
2022-07-08 17:40:25 +08:00
|
|
|
protected function mergeParams($newData): ?string
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
2023-07-12 21:38:27 +08:00
|
|
|
if (is_array($newData)) {
|
|
|
|
|
return json_encode($newData,JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
2022-07-08 17:40:25 +08:00
|
|
|
return (string)$newData;
|
2022-02-21 15:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* check isPost Request
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function isPost(): bool
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
return strtolower($this->method) === self::POST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* check isPost Request
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function isUpload(): bool
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
return strtolower($this->method) === self::UPLOAD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* check isGet Request
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function isGet(): bool
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
return strtolower($this->method) === self::GET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $arr
|
|
|
|
|
*
|
|
|
|
|
* @return array|string
|
|
|
|
|
* 将请求参数进行编码
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function paramEncode($arr): array|string
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
2023-07-12 21:38:27 +08:00
|
|
|
protected function matchHost(string $string): array
|
2022-02-21 15:17:42 +08:00
|
|
|
{
|
|
|
|
|
return [$this->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;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
}
|