Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
@@ -12,6 +12,7 @@
|
||||
"php": ">=8.0",
|
||||
"ext-json": "*",
|
||||
"ext-redis": "*",
|
||||
"ext-swoole": "*",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -12,7 +12,7 @@ class Client
|
||||
{
|
||||
|
||||
|
||||
private CoroutineClient|Curl $abstracts;
|
||||
private CoroutineClient|Curl|AsyncClient $abstracts;
|
||||
|
||||
|
||||
/**
|
||||
@@ -25,7 +25,7 @@ class Client
|
||||
if (Context::inCoroutine()) {
|
||||
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
|
||||
} else {
|
||||
$this->abstracts = new Curl($host, $port, $isSsl);
|
||||
$this->abstracts = new AsyncClient($host, $port, $isSsl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+733
-678
File diff suppressed because it is too large
Load Diff
+13
-27
@@ -22,6 +22,8 @@ use Swoole\Coroutine\Http\Client as SwowClient;
|
||||
class CoroutineClient extends ClientAbstracts
|
||||
{
|
||||
|
||||
use TSwooleClient;
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param $path
|
||||
@@ -39,6 +41,16 @@ class CoroutineClient extends ClientAbstracts
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return $this
|
||||
*/
|
||||
public function withCAInfo($path): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array|string $data
|
||||
@@ -53,6 +65,7 @@ class CoroutineClient extends ClientAbstracts
|
||||
}
|
||||
$this->setStatusCode($this->client->getStatusCode());
|
||||
$this->setBody($this->client->getBody());
|
||||
$this->setResponseHeader($this->client->headers);
|
||||
} catch (\Throwable $exception) {
|
||||
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
||||
$this->setStatusCode(-1);
|
||||
@@ -106,7 +119,6 @@ class CoroutineClient extends ClientAbstracts
|
||||
return $path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -114,30 +126,4 @@ class CoroutineClient extends ClientAbstracts
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
+173
-166
@@ -18,187 +18,194 @@ use Psr\Http\Message\ResponseInterface;
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* @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->getCurlHandler($path, $method, $params);
|
||||
|
||||
$this->execute();
|
||||
}
|
||||
$this->execute();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $method
|
||||
* @param $params
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getCurlHandler($path, $method, $params): void
|
||||
{
|
||||
[$host, $isHttps, $path] = $this->matchHost($path);
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
$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());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
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())) {
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute(): void
|
||||
{
|
||||
$output = curl_exec($this->client);
|
||||
if ($output === false) {
|
||||
$this->setStatusCode(404);
|
||||
$this->setBody(curl_error($this->client));
|
||||
} else {
|
||||
$this->explode($output);
|
||||
}
|
||||
}
|
||||
private string $caPath = '';
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
curl_close($this->client);
|
||||
}
|
||||
/**
|
||||
* @param $path
|
||||
* @return $this
|
||||
*/
|
||||
public function withCAInfo($path): static
|
||||
{
|
||||
$this->caPath = $path;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] private function parseHeaderMat(): array
|
||||
{
|
||||
$headers = [];
|
||||
foreach ($this->getHeader() as $key => $val) {
|
||||
$headers[$key] = $key . ': ' . $val;
|
||||
}
|
||||
return array_values($headers);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user