Compare commits
86 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 | |||
| 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 |
+220
@@ -0,0 +1,220 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/5/24 0024
|
||||||
|
* Time: 11:34
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use Kiri\Abstracts\Logger;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Message\Stream;
|
||||||
|
use Swoole\Client as SwowClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Client
|
||||||
|
* @package Kiri\Http
|
||||||
|
*/
|
||||||
|
class AsyncClient extends ClientAbstracts
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
use TSwooleClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method
|
||||||
|
* @param $path
|
||||||
|
* @param array $params
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function request(string $method, $path, array $params = []): void
|
||||||
|
{
|
||||||
|
$this->withMethod($method)
|
||||||
|
->coroutine(
|
||||||
|
$this->matchHost($path),
|
||||||
|
$this->paramEncode($params)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function withCAInfo($path): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array|string $data
|
||||||
|
* @throws Exception 使用swoole协程方式请求
|
||||||
|
*/
|
||||||
|
private function coroutine($url, array|string $data = []): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->generate_client($data, ...$url);
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
|
||||||
|
$this->setStatusCode(-1);
|
||||||
|
$this->setBody(jTraceEx($exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @param $host
|
||||||
|
* @param $isHttps
|
||||||
|
* @param $path
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function generate_client($data, $host, $isHttps, $path): void
|
||||||
|
{
|
||||||
|
$this->client = new SwowClient(SWOOLE_TCP, FALSE);
|
||||||
|
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
|
||||||
|
if (!$this->client->connect($host, $this->getPort())) {
|
||||||
|
throw new Exception('链接失败');
|
||||||
|
}
|
||||||
|
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
|
||||||
|
if (!empty($this->getAgent())) {
|
||||||
|
$this->withAddedHeader('User-Agent', $this->getAgent());
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $this->setParams($path, $data);
|
||||||
|
|
||||||
|
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9');
|
||||||
|
// $this->withAddedHeader('Accept-Encoding', 'gzip');
|
||||||
|
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
|
||||||
|
|
||||||
|
$this->execute($path, $this->getData()->getContents());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param string $content
|
||||||
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
private function execute(string $path, string $content)
|
||||||
|
{
|
||||||
|
$array = $this->_parseHeaders($path);
|
||||||
|
|
||||||
|
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content . "\r\n\r\n");
|
||||||
|
$receive = '';
|
||||||
|
while ($this->client->isConnected()) {
|
||||||
|
$_tmp = $this->client->recv();
|
||||||
|
if (empty($_tmp)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$receive .= $_tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
Kiri::getDi()->get(Logger::class)->debug(implode("\r\n", $array) . "\r\n\r\n" . $content);
|
||||||
|
Kiri::getDi()->get(Logger::class)->debug($receive);
|
||||||
|
[$header, $body] = explode("\r\n\r\n", $receive);
|
||||||
|
|
||||||
|
$header = explode("\r\n", $header);
|
||||||
|
$status = array_shift($header);
|
||||||
|
|
||||||
|
$this->setStatusCode(intval(explode(' ', $status)[1]));
|
||||||
|
$this->parseResponseHeaders($header);
|
||||||
|
$this->setBody($body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[Pure] private function _parseHeaders(string $path): array
|
||||||
|
{
|
||||||
|
$array = [];
|
||||||
|
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
|
||||||
|
if (!empty($this->getHeader())) {
|
||||||
|
$headers = $this->getHeader();
|
||||||
|
foreach ($headers as $key => $value) {
|
||||||
|
$array[] = sprintf('%s: %s', $key, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $client
|
||||||
|
* @param $string
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function waite(&$client, $string): mixed
|
||||||
|
{
|
||||||
|
$tmp = $client->recv();
|
||||||
|
if (!empty($tmp)) {
|
||||||
|
return $this->waite($client, $string . $tmp);
|
||||||
|
}
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function chunked()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $headers
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function parseResponseHeaders(array $headers)
|
||||||
|
{
|
||||||
|
$array = [];
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
[$key, $value] = explode(': ', $header);
|
||||||
|
|
||||||
|
$array[$key] = trim($value);
|
||||||
|
}
|
||||||
|
$this->setResponseHeader($array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function setParams($path, $data): string
|
||||||
|
{
|
||||||
|
if ($this->isGet()) {
|
||||||
|
if (!empty($data)) $path .= '?' . $data;
|
||||||
|
} else {
|
||||||
|
$data = $this->mergeParams($data);
|
||||||
|
if (!empty($data)) {
|
||||||
|
$this->withBody(new Stream($data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function close(): void
|
||||||
|
{
|
||||||
|
/** @var SwowClient $client */
|
||||||
|
$client = $this->client;
|
||||||
|
if (!$client || !$client->isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$client->close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
use Kiri\Context;
|
use Kiri\Context;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin CoroutineClient|Curl
|
* @mixin CoroutineClient|CurlClient
|
||||||
*/
|
*/
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private CoroutineClient|Curl|AsyncClient $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 AsyncClient($host, $port, $isSsl);
|
$this->abstracts = new CurlClient($host, $port, $isSsl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Http\Message\Stream;
|
use Kiri\Message\Stream;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Context;
|
use Kiri\Context;
|
||||||
use Kiri\Core\Help;
|
use Kiri\Core\Help;
|
||||||
@@ -68,7 +68,7 @@ abstract class ClientAbstracts implements IClient
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client
|
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
|
||||||
*/
|
*/
|
||||||
protected mixed $client;
|
protected mixed $client;
|
||||||
|
|
||||||
@@ -7,17 +7,17 @@
|
|||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Abstracts\Logger;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Swoole\Coroutine\Http\Client as SwowClient;
|
use Swoole\Coroutine\Http\Client as SwowClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Client
|
* Class Client
|
||||||
* @package Kiri\Kiri\Http
|
* @package Kiri\Http
|
||||||
*/
|
*/
|
||||||
class CoroutineClient extends ClientAbstracts
|
class CoroutineClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
@@ -67,7 +67,7 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
$this->setBody($this->client->getBody());
|
$this->setBody($this->client->getBody());
|
||||||
$this->setResponseHeader($this->client->headers);
|
$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', [error_trigger_format($exception)]);
|
||||||
$this->setStatusCode(-1);
|
$this->setStatusCode(-1);
|
||||||
$this->setBody(jTraceEx($exception));
|
$this->setBody(jTraceEx($exception));
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Message\Response;
|
use Kiri\Message\Response;
|
||||||
use Http\Message\Stream;
|
use Kiri\Message\Stream;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Curl
|
* Class CurlClient
|
||||||
* @package Http\Handler\Client
|
* @package Http\Handler\Client
|
||||||
*/
|
*/
|
||||||
class Curl extends ClientAbstracts
|
class CurlClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,9 +74,6 @@ class Curl extends ClientAbstracts
|
|||||||
*/
|
*/
|
||||||
private function curlHandlerSslSet(): void
|
private function curlHandlerSslSet(): void
|
||||||
{
|
{
|
||||||
curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, $this->getVerifyPeer());
|
|
||||||
curl_setopt($this->client, CURLOPT_VERBOSE, TRUE);
|
|
||||||
// curl_setopt($this->client, CURLOPT_SSL_VERIFYHOST, $this->getVerifyPeer());
|
|
||||||
if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
|
if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
|
||||||
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Http\Client;
|
namespace Kiri;
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
|
trait TSwooleClient
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[Pure] private function settings(): array
|
||||||
|
{
|
||||||
|
$sslCert = $this->getSslCertFile();
|
||||||
|
$sslKey = $this->getSslKeyFile();
|
||||||
|
$sslCa = $this->getCa();
|
||||||
|
|
||||||
|
$params = [];
|
||||||
|
if ($this->getConnectTimeout() > 0) {
|
||||||
|
$params['timeout'] = $this->getConnectTimeout();
|
||||||
|
}
|
||||||
|
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
$params['ssl_host_name'] = $this->getHost();
|
||||||
|
$params['ssl_cert_file'] = $this->getSslCertFile();
|
||||||
|
$params['ssl_key_file'] = $this->getSslKeyFile();
|
||||||
|
$params['ssl_verify_peer'] = TRUE;
|
||||||
|
$params['ssl_cafile'] = $sslCa;
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+1
-2
@@ -11,14 +11,13 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.0",
|
"php": ">=8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-redis": "*",
|
|
||||||
"ext-swoole": "*",
|
"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/"
|
"Kiri\\": "./"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -1,161 +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($this->settings());
|
|
||||||
if (!empty($this->getAgent())) {
|
|
||||||
$this->withAddedHeader('User-Agent', $this->getAgent());
|
|
||||||
} else {
|
|
||||||
$this->withAddedHeader('User-Agent', ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36');
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = $this->setParams($path, $data);
|
|
||||||
|
|
||||||
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,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, deflate');
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$array = implode("\r\n", $array) . "\r\n\r\n";
|
|
||||||
$this->client->send($array . $content);
|
|
||||||
|
|
||||||
$revice = $this->client->recv();
|
|
||||||
|
|
||||||
[$header, $body] = explode("\r\n\r\n", $revice);
|
|
||||||
|
|
||||||
$header = explode("\r\n", $header);
|
|
||||||
|
|
||||||
$status = array_shift($header);
|
|
||||||
|
|
||||||
$this->setBody($body);
|
|
||||||
$this->setStatusCode(intval(explode(' ', $status)[1]));
|
|
||||||
$this->setResponseHeader($header);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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,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