Files
kiri-core/http-handler/Client/Client.php
T

142 lines
3.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-09-29 15:41:13 +08:00
namespace Http\Handler\Client;
2020-08-31 01:27:08 +08:00
use Exception;
2021-09-29 15:41:13 +08:00
use Http\Message\Response;
use Http\Message\Stream;
2021-05-06 10:49:25 +08:00
use JetBrains\PhpStorm\Pure;
2021-09-29 15:41:13 +08:00
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Http\Client as SwowClient;
2020-08-31 01:27:08 +08:00
/**
* Class Client
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Http
2020-08-31 01:27:08 +08:00
*/
2020-11-14 04:46:52 +08:00
class Client extends ClientAbstracts
2020-08-31 01:27:08 +08:00
{
2020-11-17 18:46:41 +08:00
/**
* @param string $method
2020-12-17 14:09:14 +08:00
* @param $path
* @param array $params
2021-09-29 15:41:13 +08:00
* @return ResponseInterface
2020-11-17 18:46:41 +08:00
* @throws Exception
*/
2021-09-29 15:41:13 +08:00
public function request(string $method, $path, array $params = []): ResponseInterface
2020-11-17 18:46:41 +08:00
{
2021-09-29 15:41:13 +08:00
return $this->withMethod($method)
2020-11-17 18:46:41 +08:00
->coroutine(
2020-12-17 14:09:14 +08:00
$this->matchHost($path),
$this->paramEncode($params)
2020-11-17 18:46:41 +08:00
);
}
/**
* @param $url
2021-08-13 15:48:38 +08:00
* @param array|string $data
2021-09-29 15:41:13 +08:00
* @return ResponseInterface
2021-08-13 15:48:38 +08:00
* @throws Exception 使用swoole协程方式请求
2020-11-17 18:46:41 +08:00
*/
2021-09-29 15:41:13 +08:00
private function coroutine($url, array|string $data = []): ResponseInterface
2020-11-17 18:46:41 +08:00
{
try {
$client = $this->generate_client($data, ...$url);
if ($client->statusCode < 0) {
throw new Exception($client->errMsg);
}
2021-09-29 15:41:13 +08:00
return (new Response())->withStatus($client->getStatusCode())
->withHeaders($client->getHeaders())
->withBody(new Stream($client->getBody()));
2020-11-17 18:46:41 +08:00
} catch (\Throwable $exception) {
2021-05-06 10:49:25 +08:00
$this->addError($exception, 'rpc');
2021-09-29 15:41:13 +08:00
return (new Response())->withStatus(-1)->withHeaders([])
->withBody(new Stream(jTraceEx($exception)));
2020-11-17 18:46:41 +08:00
}
}
2021-05-06 15:14:13 +08:00
2020-11-17 18:46:41 +08:00
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
2021-09-29 15:41:13 +08:00
* @return SwowClient
2020-11-17 18:46:41 +08:00
*/
2021-09-29 15:41:13 +08:00
private function generate_client($data, $host, $isHttps, $path): SwowClient
2020-11-17 18:46:41 +08:00
{
if ($isHttps || $this->isSSL()) {
2021-09-29 15:41:13 +08:00
$client = new SwowClient($host, 443, true);
2020-11-17 18:46:41 +08:00
} else {
2021-09-29 15:41:13 +08:00
$client = new SwowClient($host, $this->getPort(), false);
2020-11-17 18:46:41 +08:00
}
$client->set($this->settings());
if (!empty($this->getAgent())) {
2021-09-29 15:41:13 +08:00
$this->withAddedHeader('User-Agent', $this->getAgent());
2020-11-17 18:46:41 +08:00
}
$client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod()));
2021-05-06 15:14:13 +08:00
$client->execute($this->setParams($client, $path, $data));
2020-11-17 18:46:41 +08:00
$client->close();
return $client;
}
2021-05-06 15:14:13 +08:00
/**
2021-09-29 15:41:13 +08:00
* @param SwowClient $client
2021-05-06 15:14:13 +08:00
* @param $path
* @param $data
* @return string
*/
2021-09-29 15:41:13 +08:00
private function setParams(SwowClient $client, $path, $data): string
2021-05-06 15:14:13 +08:00
{
2021-09-29 16:04:00 +08:00
$content = $this->getData()->getContents();
if (!empty($content)) {
$client->setData($content);
}
2021-05-06 15:14:13 +08:00
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
2021-09-29 16:04:00 +08:00
$data = $this->mergeParams($data);
if (!empty($data)) {
$client->setData($data);
2021-05-06 15:14:13 +08:00
}
}
return $path;
}
2020-11-17 18:46:41 +08:00
/**
* @return array
*/
2021-05-06 10:49:25 +08:00
#[Pure] private function settings(): array
2020-11-17 18:46:41 +08:00
{
$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;
}
2020-08-31 01:27:08 +08:00
}