2021-12-11 16:39:46 +08:00
|
|
|
<?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 JetBrains\PhpStorm\Pure;
|
|
|
|
|
use Kiri\Abstracts\Logger;
|
|
|
|
|
use Kiri\Kiri;
|
|
|
|
|
use Swoole\Client as SwowClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Client
|
|
|
|
|
* @package Kiri\Kiri\Http
|
|
|
|
|
*/
|
|
|
|
|
class AsyncClient extends ClientAbstracts
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
2021-12-11 16:43:07 +08:00
|
|
|
* @throws Exception
|
2021-12-11 16:39:46 +08:00
|
|
|
*/
|
|
|
|
|
private function generate_client($data, $host, $isHttps, $path): void
|
|
|
|
|
{
|
2021-12-11 16:43:44 +08:00
|
|
|
$this->client = new SwowClient(SWOOLE_TCP, FALSE);
|
2021-12-11 16:43:07 +08:00
|
|
|
if (!$this->client->connect($host, $this->getPort())) {
|
|
|
|
|
throw new Exception('链接失败');
|
2021-12-11 16:39:46 +08:00
|
|
|
}
|
2021-12-11 16:44:33 +08:00
|
|
|
if ($isHttps || $this->isSSL()) {
|
|
|
|
|
$this->client->enableSSL();
|
|
|
|
|
}
|
2021-12-11 16:39:46 +08:00
|
|
|
$this->client->set($this->settings());
|
2021-12-11 17:29:52 +08:00
|
|
|
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');
|
|
|
|
|
}
|
2021-12-11 17:25:27 +08:00
|
|
|
|
2021-12-11 16:39:46 +08:00
|
|
|
$path = $this->setParams($path, $data);
|
|
|
|
|
|
2021-12-11 17:25:27 +08:00
|
|
|
$content = $this->getData()->getContents();
|
|
|
|
|
|
2021-12-11 17:27:18 +08:00
|
|
|
$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');
|
2021-12-11 17:25:27 +08:00
|
|
|
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
|
|
|
|
|
|
2021-12-11 16:39:46 +08:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-11 17:21:13 +08:00
|
|
|
$array = implode("\r\n", $array) . "\r\n\r\n";
|
2021-12-11 17:25:27 +08:00
|
|
|
$this->client->send($array . $content);
|
2021-12-11 16:39:46 +08:00
|
|
|
|
|
|
|
|
$revice = $this->client->recv();
|
|
|
|
|
|
|
|
|
|
[$header, $body] = explode("\r\n\r\n", $revice);
|
|
|
|
|
|
2021-12-11 16:48:36 +08:00
|
|
|
$header = explode("\r\n", $header);
|
|
|
|
|
|
|
|
|
|
$status = array_shift($header);
|
|
|
|
|
|
2021-12-11 16:39:46 +08:00
|
|
|
$this->setBody($body);
|
2021-12-11 16:50:26 +08:00
|
|
|
$this->setStatusCode(intval(explode(' ', $status)[1]));
|
2021-12-11 16:48:36 +08:00
|
|
|
$this->setResponseHeader($header);
|
2021-12-11 16:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|