add clear

This commit is contained in:
2020-04-30 22:09:08 +08:00
parent 7bda1dbcce
commit 44146ad916
6 changed files with 236 additions and 257 deletions
+187 -192
View File
@@ -4,6 +4,7 @@ namespace wchat\common;
use Swoole\Coroutine\Http\Client as SClient;
use Swoole\Coroutine\System;
use Swoole\Coroutine\Client;
class HttpClient
{
@@ -28,6 +29,10 @@ class HttpClient
private $ca = '';
private $port = '';
/** @var string $_message 错误信息 */
private $_message = '';
private $_data = '';
/**
* @return string
*/
@@ -74,6 +79,9 @@ class HttpClient
{
}
/**
* @param $data
*/
public function setData($data)
{
$this->_data = $data;
@@ -174,27 +182,26 @@ class HttpClient
*/
public function grpc($path, array $data)
{
$client = new \Swoole\Coroutine\Client(SWOOLE_TCP);
$client = new Client(SWOOLE_TCP);
if (empty($this->host) || empty($this->port)) {
return new Result(['code' => 0, 'message' => '']);
return new Result(['code' => 500, 'message' => 'Host and port is null']);
}
if (!$client->connect($this->host, $this->port)) {
return new Result(['code' => 500, 'message' => $client->errMsg]);
}
$path = $this->port . '/' . ltrim($path, '/');
$send = $client->send(serialize([
'body' => $data,
'header' => [
'request_uri' => $path,
'request_method' => 'grpc'
]
]));
$client->close();
if ($send) {
$param = ['code' => 0, 'message' => ''];
$params['body'] = $data;
$params['header']['request_uri'] = $path;
$params['header']['request_method'] = 'grpc';
if ($client->send($params)) {
$recv = $client->recv();
$param = ['code' => 0, 'message' => Help::toArray($recv)];
} else {
$param = ['code' => 500, 'message' => $client->errMsg];
}
$client->close();
return new Result($param);
}
@@ -306,60 +313,40 @@ class HttpClient
return false;
}
// 手动设置请求域名
if (!empty($this->host)) {
if (!$this->checkIsIp($this->host)) {
$this->host = System::gethostbyname($this->host);
}
if ($this->isHttp($string)) {
$string = str_replace('http://', '', $string);
$hostAndUrls = explode('/', $string);
if ($this->isHttp($string)) {
$string = str_replace('http://', '', $string);
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
} else if ($this->isHttps($string)) {
$string = str_replace('https://', '', $string);
$this->setIsSSL(true);
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
}
return $string;
}
// 替换请求头为空
$string = str_replace('http://', '', $string);
if ($this->isHttps($string)) {
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
} else if ($this->isHttps($string)) {
$string = str_replace('https://', '', $string);
$this->setIsSSL(true);
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
} else if (empty($this->host)) {
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
}
$hostAndUrls = explode('/', $string);
if (empty($hostAndUrls)) {
return false;
}
$this->host = array_shift($hostAndUrls);
$hostAndUrls = implode('/', $hostAndUrls);
if (strpos($this->host, ':') !== false) {
[$this->host, $this->port] = explode(':', $this->host);
if ($this->checkIsIp($this->host)) {
return $hostAndUrls;
}
}
if (!$this->isDomainName($this->host)) {
return false;
if (!$this->checkIsIp($this->host)) {
$this->host = System::gethostbyname($this->host);
}
if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) {
throw new \Exception('Client Host error.');
}
$this->host = System::gethostbyname($this->host);
return $hostAndUrls;
return $string;
}
/**
@@ -378,12 +365,17 @@ class HttpClient
* @param $url
* @param $data
* @return array|Result|mixed
* @throws
*/
private function useCurl($url, $data)
{
if ($this->isHttp($url) || $this->isHttps($url)) {
return $this->curl($url, $data);
}
$url = $this->matchHost(ltrim($url, '/'));
if (!empty($this->port)) {
$this->host .= ':' . $this->port;
}
if ($this->isSSL) {
return $this->curl('https://' . $this->host . '/' . $url, $data);
} else {
@@ -391,41 +383,6 @@ class HttpClient
}
}
/**
* @param $url
* @return array
*/
private function parseUrlHost($url)
{
if (!$this->isHttp($url) && !$this->isHttps($url)) {
$url = ($this->isSSL ? 'https://' : 'http://') . $url;
}
$url = $this->matchHost($url);
if ($this->checkIsIp($this->host)) {
return [$url, $this->host];
}
return [$url, System::gethostbyname($this->host)];
}
/**
* @param $url
* @return mixed
*/
private function cutRequestUrl($url)
{
$url = $this->replaceHost($url);
$explode = explode('/', $url);
$first = array_shift($explode);
if (strpos($first, ':') !== false) {
[$first, $this->port] = explode(':', $first);
if ($this->port !== 443) {
$this->isSSL = false;
}
}
$url = '/' . implode('/', $explode);
return [$first, $url];
}
/**
* @param $host
* @return string|string[]
@@ -478,38 +435,36 @@ class HttpClient
*/
private function coroutine($url, $data = [])
{
$client = $this->generate_client($this->host, $url, $data);
if ($client->statusCode < 0) {
throw new \Exception($client->errMsg);
}
$body = $client->body;
$header = $client->getHeaders();
$body = $this->resolve($header, $body);
unset($this->_data);
if ($client->getStatusCode() != 200) {
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
try {
$client = $this->generate_client($this->host, $url, $data);
if ($client->statusCode < 0) {
throw new \Exception($client->errMsg);
}
return new Result(['code' => $client->getStatusCode(), 'message' => $message, 'data' => $body]);
unset($this->_data);
$body = $this->resolve($client->getHeaders(), $client->body);
if ($client->getStatusCode() != 200) {
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
}
$response['code'] = $client->getStatusCode();
$response['message'] = $message;
$response['data'] = $body;
} else {
$response = $this->structure($body, $data, $client->getHeaders());
}
} catch (\Throwable $exception) {
$response['code'] = 500;
$response['message'] = $exception->getMessage();
$response['data'] = array_slice($exception->getTrace(), 0, 6);
$response['header'] = [];
}
return $this->structure($body, $data, $header);
}
/**
* @return mixed
*/
private function getHostIp()
{
$host = \Co::getAddrInfo($this->host);
return array_shift($host);
if (!($response instanceof Result)) {
$response = new Result($response);
}
return $response;
}
/**
@@ -532,12 +487,13 @@ class HttpClient
* @param $data
* @return \Swoole\Coroutine\Http\Client
*/
private function generate_client($host, $url, $data)
private function generate_client($host, $url, $data = [])
{
$client = new SClient($host, $this->getHostPort(), $this->isSSL);
if (strpos($url, '/') !== 0) {
$url = '/' . $url;
}
$client->set($this->settings());
if (!empty($this->agent)) {
$this->header['User-Agent'] = $this->agent;
@@ -545,40 +501,65 @@ class HttpClient
if (!empty($this->header)) {
$client->setHeaders($this->header);
}
$client->setMethod(strtoupper($this->method));
if (strtolower($this->method) == self::GET && !empty($data)) {
$url .= '?' . $data;
} else {
$this->_data = $this->mergeParams($data);
}
if (!empty($this->_data)) {
$client->setData($this->_data);
}
if (strtolower($this->method) == self::GET) {
if (!empty($data)) {
$url .= '?' . $data;
}
$client->get($url);
} else if (strtolower($this->method) == self::PUT) {
$client->setMethod('PUT');
if (!is_string($data)) {
$data = json_encode($data);
}
if (!empty($data)) {
$client->setData($data);
}
$client->execute($url);
} else if (strtolower($this->method) == self::DELETE) {
$client->setMethod('DELETE');
if (!is_string($data)) {
$data = json_encode($data);
}
if (!empty($data)) {
$client->setData($data);
}
$client->execute($url);
} else {
$client->post($url, $data);
}
$client->execute($url);
$client->close();
return $client;
}
/**
* @param $newData
* @return mixed
*/
private function mergeParams($newData)
{
if (empty($this->_data)) {
return $this->toRequest($newData);
} else if (empty($newData)) {
return $this->toRequest($this->_data);
}
$newData = Help::toArray($newData);
$array = Help::toArray($this->_data);
$params = array_merge($array, $newData);
return $this->toRequest($params);
}
/**
* @param $data
* @return false|mixed|string
*/
private function toRequest($data)
{
if (!is_array($data)) {
$data = Help::toArray($data);
}
$contentType = $this->header['Content-Type'] ?? 'application/json';
if (strpos($contentType, 'json') !== false) {
return Help::toJson($data);
} else if (strpos($contentType, 'xml') !== false) {
return Help::toXml($data);
} else {
return Help::toJson($data);
}
}
/**
* @return array
*/
private function settings()
{
$sslCert = $this->getSslCertFile();
@@ -606,58 +587,72 @@ class HttpClient
private function curl($url, $data = [])
{
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, true);
if ($headers = $this->parseHeaderMat()) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if (!empty($this->agent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
}
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile());
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if ($this->method == self::POST) {
curl_setopt($ch, CURLOPT_POST, 1);
}
if ($this->method != self::GET) {
if (!empty($this->_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
$output = curl_exec($ch);
curl_close($ch);
$output = $this->curlParse($url, $this->mergeParams($data));
if ($output === FALSE) {
$data = new Result(['code' => 500, 'message' => curl_error($ch)]);
} else {
[$header, $body, $status] = $this->explode($output);
if ($status != 200) {
$data = new Result(['code' => 500, 'message' => $body, 'header' => $header]);
} else {
$data = $this->structure($body, $data, $header);
}
return new Result(['code' => 500, 'message' => $output]);
}
[$header, $body, $status] = $this->explode($output);
if ($status != 200 && $status != 201) {
$data = new Result(['code' => $status, 'message' => $body, 'header' => $header]);
} else {
$data = $this->structure($body, $data, $header);
}
unset($ch);
return $data;
} catch (\Exception $exception) {
return new Result(['code' => 500, 'message' => $exception->getMessage(), 'header' => []]);
} catch (\Throwable $exception) {
$response['code'] = 500;
$response['message'] = $exception->getMessage();
$response['data'] = array_slice($exception->getTrace(), 0, 6);
$response['header'] = [];
return new Result($response);
}
}
/**
* @param $url
* @param $data
* @return bool|string
* @throws \Exception
*/
private function curlParse($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, true);
if ($headers = $this->parseHeaderMat()) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if (!empty($this->agent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
}
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile());
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if ($this->method == self::POST) {
curl_setopt($ch, CURLOPT_POST, 1);
}
if ($this->method != self::GET) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
$output = curl_exec($ch);
if ($output === false) {
throw new \Exception(curl_error($ch));
}
curl_close($ch);
return $output;
}
/**
* @param $output
* @return array