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

145 lines
3.2 KiB
PHP
Raw Normal View History

2020-09-11 19:37:32 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-11 19:37:32 +08:00
namespace HttpServer\Client;
use Exception;
2020-11-01 05:10:38 +08:00
use HttpServer\Http\Context;
2020-09-11 19:37:32 +08:00
use Snowflake\Abstracts\Component;
2020-09-11 19:43:19 +08:00
use Snowflake\Core\Help;
2020-09-11 19:37:32 +08:00
use Snowflake\Core\JSON;
use Swoole\Http2\Request;
use \Swoole\Coroutine\Http2\Client as H2Client;
/**
* Class Http2
* @package HttpServer\Client
*/
class Http2 extends Component
{
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
2020-09-12 02:53:27 +08:00
public function get($domain, $path, $params = [], $timeout = -1)
2020-09-11 19:37:32 +08:00
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'GET', $params));
2020-09-11 19:43:19 +08:00
return new Result(['code' => 0, 'data' => Help::toArray($client->recv())]);
2020-09-11 19:37:32 +08:00
}
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
2020-09-12 02:53:27 +08:00
public function post($domain, $path, $params = [], $timeout = -1)
2020-09-11 19:37:32 +08:00
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'POST', $params));
2020-09-11 19:43:19 +08:00
return new Result(['code' => 0, 'data' => Help::toArray($client->recv())]);
2020-09-11 19:37:32 +08:00
}
2020-09-12 02:53:27 +08:00
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
public function delete($domain, $path, $params = [], $timeout = -1)
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'DELETE', $params));
return new Result(['code' => 0, 'data' => Help::toArray($client->recv())]);
}
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
public function put($domain, $path, $params = [], $timeout = -1)
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'PUT', $params));
return new Result(['code' => 0, 'data' => Help::toArray($client->recv())]);
}
2020-09-11 19:37:32 +08:00
/**
* @param $domain
* @param $path
* @param $method
* @param $params
* @return Request
* @throws Exception
*/
public function getRequest($domain, $path, $method, $params)
{
2020-11-01 05:10:38 +08:00
if (Context::hasContext($domain . $path)) {
$req = Context::getContext($domain . $path);
2020-09-11 19:37:32 +08:00
} else {
$req = new Request();
2020-11-01 05:10:38 +08:00
$req->method = $method;
$req->path = $path;
$req->headers = [
'host' => $domain,
'user-agent' => 'Chrome/49.0.2587.3',
'accept' => 'text/html,application/json',
'accept-encoding' => 'gzip'
];
if (!is_string($params)) {
$params = JSON::encode($params);
}
Context::setContext($domain . $path, $req);
2020-09-11 19:37:32 +08:00
}
$req->data = $params;
return $req;
}
/**
* @param $domain
* @param $path
* @param int $timeout
* @return H2Client
* @throws Exception
*/
private function getClient($domain, $path, $timeout = -1)
{
2020-11-01 05:10:38 +08:00
if (Context::hasContext($domain)) {
return Context::getContext($domain);
2020-09-11 19:37:32 +08:00
}
$client = new H2Client($domain, 443, true);
$client->set([
'timeout' => $timeout,
'ssl_host_name' => $domain
]);
if (!$client->connect()) {
throw new Exception('Connected fail.');
}
2020-11-01 05:10:38 +08:00
return Context::setContext($domain, $client);
2020-09-11 19:37:32 +08:00
}
}