Files
kiri-core/HttpServer/Client/Client.php
T

137 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
namespace HttpServer\Client;
use Exception;
2021-05-06 10:49:25 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine\Http\Client as SClient;
/**
* Class Client
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\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
* @return array|string|Result
2020-11-17 18:46:41 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function request(string $method, $path, $params = []): array|string|Result
2020-11-17 18:46:41 +08:00
{
return $this->setMethod($method)
->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
* @param array $data
2020-12-17 14:09:14 +08:00
* @return array|string|Result
2020-11-17 18:46:41 +08:00
* @throws Exception
* 使用swoole协程方式请求
*/
2020-12-17 14:09:14 +08:00
private function coroutine($url, $data = []): array|string|Result
2020-11-17 18:46:41 +08:00
{
try {
$client = $this->generate_client($data, ...$url);
2021-04-23 15:49:37 +08:00
$this->setData('');
2020-11-17 18:46:41 +08:00
if ($client->statusCode < 0) {
throw new Exception($client->errMsg);
}
$body = $this->resolve($client->getHeaders(), $client->body);
if (in_array($client->getStatusCode(), [200, 201])) {
return $this->structure($body, $data, $client->getHeaders());
}
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
}
return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders());
} catch (\Throwable $exception) {
2021-05-06 10:49:25 +08:00
$this->addError($exception, 'rpc');
2020-11-17 18:46:41 +08:00
return $this->fail(500, $exception->getMessage(), [
'file' => $exception->getFile(),
'line' => $exception->getLine()
], []);
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
* @return SClient
*/
2020-12-17 14:09:14 +08:00
private function generate_client($data, $host, $isHttps, $path): SClient
2020-11-17 18:46:41 +08:00
{
if ($isHttps || $this->isSSL()) {
2021-01-08 12:01:47 +08:00
$client = new SClient($host, 443, true);
2020-11-17 18:46:41 +08:00
} else {
2021-01-08 12:01:47 +08:00
$client = new SClient($host, $this->getPort(), false);
2020-11-17 18:46:41 +08:00
}
if (strpos($path, '/') !== 0) {
$path = '/' . $path;
}
$client->set($this->settings());
if (!empty($this->getAgent())) {
$this->addHeader('User-Agent', $this->getAgent());
}
$client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod()));
2021-05-06 10:49:25 +08:00
if ($this->isGet() && !empty($data)) {
2020-11-17 18:46:41 +08:00
$path .= '?' . $data;
} else {
$this->setData($this->mergeParams($data));
}
if (!empty($this->getData())) {
$client->setData($this->getData());
}
$client->execute($path);
$client->close();
return $client;
}
/**
* @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
}