add clear

This commit is contained in:
2020-11-13 15:59:45 +08:00
parent c37a3995e9
commit dc887b81e3
+153 -140
View File
@@ -11,29 +11,54 @@ use Swoole\Coroutine\Client;
class HttpClient
{
private $host = '';
private string $host = '';
private $header = [];
private array $header = [];
private $callback = null;
private $method = 'get';
private int $timeout = 0;
private $url = '';
private $isSSL = false;
private $agent = '';
private $isFileStream = false;
private $errorCodeField = '';
private $errorMsgField = '';
private $use_swoole = false;
private ?\Closure $callback = null;
private string $method = 'get';
private $ssl_cert_file = '';
private $ssl_key_file = '';
private $ca = '';
private $port = '';
private bool $isSSL = false;
private string $agent = '';
private string $errorCodeField = '';
private string $errorMsgField = '';
private bool $use_swoole = false;
private string $ssl_cert_file = '';
private string $ssl_key_file = '';
private string $ca = '';
private int $port = 80;
/** @var string $_message 错误信息 */
private $_message = '';
private $_data = '';
private string $_message = '';
private string $_data = '';
private int $connect_timeout = 1;
const GET = 'get';
const PUT = 'put';
const POST = 'post';
const DELETE = 'delete';
const OPTIONS = 'option';
/**
* @return int
*/
public function getConnectTimeout(): int
{
return $this->connect_timeout;
}
/**
* @param int $connect_timeout
*/
public function setConnectTimeout(int $connect_timeout): void
{
$this->connect_timeout = $connect_timeout;
}
/**
* @return string
@@ -53,33 +78,21 @@ class HttpClient
/**
* @return string
* @return int
*/
public function getPort(): string
public function getPort(): int
{
return $this->port;
}
/**
* @param string $port
* @param int $port
*/
public function setPort(string $port): void
public function setPort(int $port): void
{
$this->port = $port;
}
const POST = 'post';
const GET = 'get';
const PUT = 'put';
const DELETE = 'delete';
const OPTIONS = 'option';
/**
* HttpClient constructor.
*/
private function __construct()
{
}
/**
* @param $data
@@ -97,6 +110,22 @@ class HttpClient
return $this->ssl_cert_file;
}
/**
* @return bool
*/
public function hasSslCertFile(): bool
{
return !empty($this->ssl_cert_file) && file_exists($this->ssl_cert_file);
}
/**
* @return bool
*/
public function hasSslKeyFile(): bool
{
return !empty($this->ssl_key_file) && file_exists($this->ssl_key_file);
}
/**
* @param string $ssl_cert_file
*/
@@ -125,11 +154,7 @@ class HttpClient
*/
public static function NewRequest()
{
static $client = null;
if (!($client instanceof HttpClient)) {
$client = new HttpClient();
}
return $client;
return new HttpClient();
}
/**
@@ -177,36 +202,46 @@ class HttpClient
}
}
/**
* @param $path
* @param array $data
* @param int $type
* @return Result
*/
public function grpc($path, array $data)
public function sendTo($path, array $data, $type = SWOOLE_TCP)
{
$client = new Client(SWOOLE_TCP);
$client = new Client($type);
if (empty($this->host) || empty($this->port)) {
return new Result(['code' => 500, 'message' => 'Host and port is null']);
}
if (!$client->connect($this->host, $this->port)) {
if (!$client->connect($this->host, $this->port, $this->connect_timeout)) {
return new Result(['code' => 500, 'message' => $client->errMsg]);
}
$path = $this->port . '/' . ltrim($path, '/');
$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)];
$params['path'] = '/' . $this->port . '/' . ltrim($path, '/');
$params['header']['request_uri'] = $params['path'];
$params['header']['request_method'] = 'receive';
if ($client->send(serialize($params))) {
$recv = $this->timeout > 0 ? $client->recv($this->timeout) : $client->recv();
$param = $this->structure(Help::toArray($recv), $data, null, 200);
} else {
$param = ['code' => 500, 'message' => $client->errMsg];
$param = new Result(['code' => 500, 'message' => $client->errMsg]);
}
$client->close();
return new Result($param);
return $param;
}
/**
* @param int $sec
* 设置超时时间
*/
public function setTimeout(int $sec)
{
$this->timeout = $sec;
}
/**
* @param $key
* @param $value
@@ -242,13 +277,8 @@ class HttpClient
}
/**
* @param string $url
* @param string $agent
*/
public function setUrl(string $url)
{
$this->url = $url;
}
public function setAgent(string $agent)
{
$this->agent = $agent;
@@ -273,15 +303,6 @@ class HttpClient
return $this->isSSL;
}
/**
* @param bool $isFIle
* 设置返回类型
*/
public function asFileStream($isFIle = true)
{
$this->isFileStream = $isFIle;
}
/**
* @param $url
* @param array $data
@@ -316,9 +337,7 @@ class HttpClient
if (empty($string)) {
return false;
}
if (!empty($this->host)) {
return $string;
}
if ($this->isHttp($string)) {
$string = str_replace('http://', '', $string);
$hostAndUrls = explode('/', $string);
@@ -379,7 +398,7 @@ class HttpClient
return $this->curl($url, $data);
}
$url = $this->matchHost(ltrim($url, '/'));
if (!empty($this->port) && $this->port != 443) {
if (!empty($this->port)) {
$this->host .= ':' . $this->port;
}
if ($this->isSSL) {
@@ -449,7 +468,7 @@ class HttpClient
unset($this->_data);
$body = $this->resolve($client->getHeaders(), $client->body);
if ($client->getStatusCode() != 200) {
if (!in_array($client->getStatusCode(), [200, 201])) {
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
@@ -458,6 +477,9 @@ class HttpClient
$response['code'] = $client->getStatusCode();
$response['message'] = $message;
$response['data'] = $body;
$response['header'] = $client->getHeaders();
$response = new Result($response);
} else {
$response = $this->structure($body, $data, $client->getHeaders());
}
@@ -466,8 +488,7 @@ class HttpClient
$response['message'] = $exception->getMessage();
$response['data'] = array_slice($exception->getTrace(), 0, 6);
$response['header'] = [];
}
if (!($response instanceof Result)) {
$response = new Result($response);
}
return $response;
@@ -512,6 +533,7 @@ class HttpClient
} else {
$this->_data = $this->mergeParams($data);
}
if (!empty($this->_data)) {
$client->setData($this->_data);
}
@@ -576,8 +598,12 @@ class HttpClient
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
$params = [];
if ($this->connect_timeout > 0) {
$params['timeout'] = $this->connect_timeout;
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return [];
return $params;
}
$params['ssl_host_name'] = $this->host;
@@ -602,7 +628,7 @@ class HttpClient
return new Result(['code' => 500, 'message' => $output]);
}
[$header, $body, $status] = $this->explode($output);
if ($status != 200 && $status != 201) {
if (!in_array($status, [200, 201])) {
$data = new Result(['code' => $status, 'message' => $body, 'header' => $header]);
} else {
$data = $this->structure($body, $data, $header);
@@ -617,6 +643,58 @@ class HttpClient
}
}
/**
* @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));
if ($this->timeout > 0) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // 超时设置
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); // 超时设置
}
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);
}
if (file_exists($cert = $this->getSslCertFile())) {
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
}
if (file_exists($key = $this->getSslKeyFile())) {
curl_setopt($ch, CURLOPT_SSLKEY, $key);
}
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
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 $url
* @param $params
@@ -647,70 +725,6 @@ class HttpClient
}
}
/**
* @param $url
* @param $data
* @return bool|string
* @throws Exception
*/
private function curlParse($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->createRequestUrl($url, $data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
curl_setopt($curl, CURLOPT_TIMEOUT, 120);// 超时设置
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, FALSE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); // 超时设置
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //返回内容
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // 跟踪重定向
// curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
$this->parseHeader($curl);
$this->parseSslFile($curl);
if (!empty($this->agent)) {
curl_setopt($curl, CURLOPT_USERAGENT, $this->agent);
}
if ($this->method == self::POST) {
curl_setopt($curl, CURLOPT_POST, 1);
}
if ($this->method != self::GET) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($curl);
if ($output === false) {
throw new Exception(curl_error($curl));
}
curl_close($curl);
return $output;
}
/**
* @param $curl
*/
private function parseSslFile($curl)
{
$key = $this->getSslKeyFile();
$cert = $this->getSslCertFile();
if (!empty($cert) && file_exists($cert)) {
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
}
if (!empty($key) && file_exists($key)) {
curl_setopt($curl, CURLOPT_SSLKEY, $key);
}
}
/**
* @param $curl
*/
private function parseHeader($curl)
{
$headers = $this->parseHeaderMat();
if (empty($headers)) {
return;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
/**
* @param $output
@@ -815,7 +829,6 @@ class HttpClient
$result['header'] = $header;
$result['httpStatus'] = $statusCode;
return new Result($result);
}