add clear

This commit is contained in:
2020-03-31 16:10:49 +08:00
parent cdc74e59a9
commit 7ee32b10b6
+54 -17
View File
@@ -24,6 +24,7 @@ class HttpClient
private $ssl_cert_file = ''; private $ssl_cert_file = '';
private $ssl_key_file = ''; private $ssl_key_file = '';
private $ca = '';
private $port = 80; private $port = 80;
@@ -42,6 +43,25 @@ class HttpClient
{ {
} }
/**
* @return string
*/
public function getCa(): string
{
return $this->ca;
}
/**
* @param string $ca
* @return HttpClient
*/
public function setCa(string $ca): HttpClient
{
$this->ca = $ca;
return $this;
}
public function setData($data) public function setData($data)
{ {
$this->_data = $data; $this->_data = $data;
@@ -221,18 +241,22 @@ class HttpClient
private function request($url, $data = []) private function request($url, $data = [])
{ {
$data = $this->paramEncode($data); $data = $this->paramEncode($data);
if ($this->use_swoole === false) { if ($this->use_swoole && $this->isCli()) {
return $this->useCurl($url, $data); [$url, $host] = $this->parseUrlHost($url);
} else if ($this->use_swoole) { return $this->coroutine($host, $url, $data);
return $this->coroutine($this->parseUrlHost($url), $url, $data);
} else { } else {
if (function_exists('getIsCli') && getIsCli()) {
return $this->coroutine($this->parseUrlHost($url), $url, $data);
}
return $this->useCurl($url, $data); return $this->useCurl($url, $data);
} }
} }
/**
* @return bool
*/
private function isCli()
{
return function_exists('getIsCli') && getIsCli();
}
/** /**
* @param $url * @param $url
* @param $data * @param $data
@@ -252,19 +276,22 @@ class HttpClient
/** /**
* @param $url * @param $url
* @return string * @return array
*/ */
private function parseUrlHost(&$url) private function parseUrlHost($url)
{ {
if (!$this->isHttps($url) && !$this->isHttp($url)) { if (strpos($this->host, $url) === false) {
$url = $this->host . '/' . $url; $url = rtrim($this->host, '/') . '/' . ltrim($url, '/');
} }
$host = $this->cutRequestUrl($url); if (!$this->isHttp($url) && !$this->isHttps($url)) {
$url = ($this->isSSL ? 'https://' : 'http://') . $url;
}
[$host, $url] = $this->cutRequestUrl($url);
if ($this->checkIsIp($host)) { if ($this->checkIsIp($host)) {
return $host; return [$url, $host];
} }
$this->host = $host; $this->host = $host;
return System::gethostbyname($host); return [$url, System::gethostbyname($host)];
} }
/** /**
@@ -353,7 +380,12 @@ class HttpClient
unset($this->_data); unset($this->_data);
if ($client->getStatusCode() != 200) { if ($client->getStatusCode() != 200) {
return new Result(['code' => $client->getStatusCode(), 'message' => $this->searchMessageByData($body), 'data' => $body]); if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
}
return new Result(['code' => $client->getStatusCode(), 'message' => $message, 'data' => $body]);
} }
return $this->structure($body, $data, $header); return $this->structure($body, $data, $header);
@@ -391,7 +423,13 @@ class HttpClient
private function generate_client($host, $url, $data) private function generate_client($host, $url, $data)
{ {
$client = new SClient($host, $this->getHostPort(), $this->isSSL); $client = new SClient($host, $this->getHostPort(), $this->isSSL);
echo $host . ':' . $this->getHostPort() . $url . PHP_EOL; $client->set([
'ssl_host_name' => $this->host,
'ssl_cert_file' => $this->getSslCertFile(),
'ssl_key_file' => $this->getSslKeyFile(),
'ssl_verify_peer' => true,
'ssl_cafile' => $this->getCa(),
]);
if (!empty($this->agent)) { if (!empty($this->agent)) {
$this->header['User-Agent'] = $this->agent; $this->header['User-Agent'] = $this->agent;
} }
@@ -461,7 +499,6 @@ class HttpClient
if ($this->method == self::POST) { if ($this->method == self::POST) {
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);
} }
echo $url . PHP_EOL;
if ($this->method != self::GET) { if ($this->method != self::GET) {
if (!empty($this->_data)) { if (!empty($this->_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);