Files
kiri-client/CoroutineClient.php
T

130 lines
2.6 KiB
PHP
Raw Normal View History

2021-10-09 15:48:59 +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 JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
use Swoole\Coroutine\Http\Client as SwowClient;
/**
* Class Client
* @package Kiri\Kiri\Http
*/
class CoroutineClient extends ClientAbstracts
{
2021-12-11 17:31:21 +08:00
use TSwooleClient;
2021-10-09 15:48:59 +08:00
/**
* @param string $method
* @param $path
* @param array $params
2021-11-18 10:46:24 +08:00
* @return void
2021-10-09 15:48:59 +08:00
* @throws Exception
*/
2021-11-18 10:46:24 +08:00
public function request(string $method, $path, array $params = []): void
2021-10-09 15:48:59 +08:00
{
2021-11-18 10:46:24 +08:00
$this->withMethod($method)
2021-10-09 15:48:59 +08:00
->coroutine(
$this->matchHost($path),
$this->paramEncode($params)
);
}
2021-12-11 16:13:10 +08:00
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
return $this;
}
2021-10-09 15:48:59 +08:00
/**
* @param $url
* @param array|string $data
* @throws Exception 使用swoole协程方式请求
*/
2021-11-18 10:46:24 +08:00
private function coroutine($url, array|string $data = []): void
2021-10-09 15:48:59 +08:00
{
try {
2021-11-18 10:46:24 +08:00
$this->generate_client($data, ...$url);
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
2021-10-09 15:48:59 +08:00
}
2021-11-29 11:28:01 +08:00
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
2021-11-29 11:50:28 +08:00
$this->setResponseHeader($this->client->headers);
2021-10-09 15:48:59 +08:00
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
2021-11-29 11:28:01 +08:00
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
2021-10-09 15:48:59 +08:00
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
*/
2021-11-18 10:46:24 +08:00
private function generate_client($data, $host, $isHttps, $path): void
2021-10-09 15:48:59 +08:00
{
if ($isHttps || $this->isSSL()) {
2021-11-18 10:46:24 +08:00
$this->client = new SwowClient($host, 443, true);
2021-10-09 15:48:59 +08:00
} else {
2021-11-18 10:46:24 +08:00
$this->client = new SwowClient($host, $this->getPort(), false);
2021-10-09 15:48:59 +08:00
}
2021-11-18 10:46:24 +08:00
$this->client->set($this->settings());
2021-10-09 15:48:59 +08:00
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
2021-11-18 10:46:24 +08:00
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
2021-10-09 15:48:59 +08:00
}
/**
* @param $path
* @param $data
* @return string
*/
2021-11-18 10:46:24 +08:00
private function setParams($path, $data): string
2021-10-09 15:48:59 +08:00
{
$content = $this->getData()->getContents();
if (!empty($content)) {
2021-11-18 10:46:24 +08:00
$this->client->setData($content);
2021-10-09 15:48:59 +08:00
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
2021-11-18 10:46:24 +08:00
$this->client->setData($data);
2021-10-09 15:48:59 +08:00
}
}
return $path;
}
2021-11-18 10:46:24 +08:00
/**
*
*/
public function close(): void
{
$this->client->close();
}
2021-10-09 15:48:59 +08:00
}