add clear
This commit is contained in:
+54
-55
@@ -11,31 +11,31 @@ use Swoole\Coroutine\Client;
|
|||||||
class HttpClient
|
class HttpClient
|
||||||
{
|
{
|
||||||
|
|
||||||
private string $host = '';
|
private $host = '';
|
||||||
|
|
||||||
private array $header = [];
|
private $header = [];
|
||||||
|
|
||||||
private int $timeout = 0;
|
private $timeout = 0;
|
||||||
|
|
||||||
private ?\Closure $callback = null;
|
private $callback = null;
|
||||||
private string $method = 'get';
|
private $method = 'get';
|
||||||
|
|
||||||
private bool $isSSL = false;
|
private $isSSL = false;
|
||||||
private string $agent = '';
|
private $agent = '';
|
||||||
private string $errorCodeField = '';
|
private $errorCodeField = '';
|
||||||
private string $errorMsgField = '';
|
private $errorMsgField = '';
|
||||||
private bool $use_swoole = false;
|
private $use_swoole = false;
|
||||||
|
|
||||||
private string $ssl_cert_file = '';
|
private $ssl_cert_file = '';
|
||||||
private string $ssl_key_file = '';
|
private $ssl_key_file = '';
|
||||||
private string $ca = '';
|
private $ca = '';
|
||||||
private int $port = 80;
|
private $port = 80;
|
||||||
|
|
||||||
/** @var string $_message 错误信息 */
|
/** @var $_message 错误信息 */
|
||||||
private string $_message = '';
|
private $_message = '';
|
||||||
private ?string $_data = '';
|
private $_data = '';
|
||||||
|
|
||||||
private int $connect_timeout = 1;
|
private $connect_timeout = 1;
|
||||||
|
|
||||||
const GET = 'get';
|
const GET = 'get';
|
||||||
const PUT = 'put';
|
const PUT = 'put';
|
||||||
@@ -46,15 +46,15 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getConnectTimeout(): int
|
public function getConnectTimeout()
|
||||||
{
|
{
|
||||||
return $this->connect_timeout;
|
return $this->connect_timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $connect_timeout
|
* @param $connect_timeout
|
||||||
*/
|
*/
|
||||||
public function setConnectTimeout(int $connect_timeout): void
|
public function setConnectTimeout($connect_timeout)
|
||||||
{
|
{
|
||||||
$this->connect_timeout = $connect_timeout;
|
$this->connect_timeout = $connect_timeout;
|
||||||
}
|
}
|
||||||
@@ -63,15 +63,15 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCa(): string
|
public function getCa()
|
||||||
{
|
{
|
||||||
return $this->ca;
|
return $this->ca;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $ca
|
* @param $ca
|
||||||
*/
|
*/
|
||||||
public function setCa(string $ca): void
|
public function setCa($ca)
|
||||||
{
|
{
|
||||||
$this->ca = $ca;
|
$this->ca = $ca;
|
||||||
}
|
}
|
||||||
@@ -80,15 +80,15 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getPort(): int
|
public function getPort()
|
||||||
{
|
{
|
||||||
return $this->port;
|
return $this->port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $port
|
* @param $port
|
||||||
*/
|
*/
|
||||||
public function setPort(int $port): void
|
public function setPort($port)
|
||||||
{
|
{
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
}
|
}
|
||||||
@@ -105,7 +105,7 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSslCertFile(): string
|
public function getSslCertFile()
|
||||||
{
|
{
|
||||||
return $this->ssl_cert_file;
|
return $this->ssl_cert_file;
|
||||||
}
|
}
|
||||||
@@ -127,9 +127,9 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $ssl_cert_file
|
* @param $ssl_cert_file
|
||||||
*/
|
*/
|
||||||
public function setSslCertFile(string $ssl_cert_file)
|
public function setSslCertFile($ssl_cert_file)
|
||||||
{
|
{
|
||||||
$this->ssl_cert_file = $ssl_cert_file;
|
$this->ssl_cert_file = $ssl_cert_file;
|
||||||
}
|
}
|
||||||
@@ -137,15 +137,15 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSslKeyFile(): string
|
public function getSslKeyFile()
|
||||||
{
|
{
|
||||||
return $this->ssl_key_file;
|
return $this->ssl_key_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $ssl_key_file
|
* @param $ssl_key_file
|
||||||
*/
|
*/
|
||||||
public function setSslKeyFile(string $ssl_key_file)
|
public function setSslKeyFile($ssl_key_file)
|
||||||
{
|
{
|
||||||
$this->ssl_key_file = $ssl_key_file;
|
$this->ssl_key_file = $ssl_key_file;
|
||||||
}
|
}
|
||||||
@@ -158,10 +158,10 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param $name
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setErrorField(string $name)
|
public function setErrorField($name)
|
||||||
{
|
{
|
||||||
$this->errorCodeField = $name;
|
$this->errorCodeField = $name;
|
||||||
return $this;
|
return $this;
|
||||||
@@ -181,19 +181,19 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param $name
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setErrorMsgField(string $name)
|
public function setErrorMsgField($name)
|
||||||
{
|
{
|
||||||
$this->errorMsgField = $name;
|
$this->errorMsgField = $name;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $host
|
* @param $host
|
||||||
*/
|
*/
|
||||||
public function setHost(string $host)
|
public function setHost($host)
|
||||||
{
|
{
|
||||||
var_dump($host);
|
var_dump($host);
|
||||||
$this->host = $this->replaceHost($host);
|
$this->host = $this->replaceHost($host);
|
||||||
@@ -207,7 +207,7 @@ class HttpClient
|
|||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param int $type
|
* @param $type
|
||||||
* @return Result
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function sendTo($path, array $data, $type = SWOOLE_TCP)
|
public function sendTo($path, array $data, $type = SWOOLE_TCP)
|
||||||
@@ -234,10 +234,10 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $sec
|
* @param $sec
|
||||||
* 设置超时时间
|
* 设置超时时间
|
||||||
*/
|
*/
|
||||||
public function setTimeout(int $sec)
|
public function setTimeout($sec)
|
||||||
{
|
{
|
||||||
$this->timeout = $sec;
|
$this->timeout = $sec;
|
||||||
}
|
}
|
||||||
@@ -270,17 +270,17 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param $method
|
||||||
*/
|
*/
|
||||||
public function setMethod(string $method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $agent
|
* @param $agent
|
||||||
*/
|
*/
|
||||||
public function setAgent(string $agent)
|
public function setAgent($agent)
|
||||||
{
|
{
|
||||||
$this->agent = $agent;
|
$this->agent = $agent;
|
||||||
}
|
}
|
||||||
@@ -329,35 +329,35 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $string
|
* @param $string
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function matchHost($string = '')
|
private function matchHost($_string = '')
|
||||||
{
|
{
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isHttp($string)) {
|
if ($this->isHttp($string)) {
|
||||||
$string = str_replace('http://', '', $string);
|
$_string = str_replace('http://', '', $string);
|
||||||
$hostAndUrls = explode('/', $string);
|
$hostAndUrls = explode('/', $string);
|
||||||
|
|
||||||
$this->host = array_shift($hostAndUrls);
|
$this->host = array_shift($hostAndUrls);
|
||||||
$string = implode('/', $hostAndUrls);
|
$_string = implode('/', $hostAndUrls);
|
||||||
} else if ($this->isHttps($string)) {
|
} else if ($this->isHttps($string)) {
|
||||||
$string = str_replace('https://', '', $string);
|
$_string = str_replace('https://', '', $string);
|
||||||
$this->setIsSSL(true);
|
$this->setIsSSL(true);
|
||||||
|
|
||||||
$hostAndUrls = explode('/', $string);
|
$hostAndUrls = explode('/', $string);
|
||||||
|
|
||||||
$this->host = array_shift($hostAndUrls);
|
$this->host = array_shift($hostAndUrls);
|
||||||
$string = implode('/', $hostAndUrls);
|
$_string = implode('/', $hostAndUrls);
|
||||||
} else if (empty($this->host)) {
|
} else if (empty($this->host)) {
|
||||||
$hostAndUrls = explode('/', $string);
|
$hostAndUrls = explode('/', $string);
|
||||||
$this->host = array_shift($hostAndUrls);
|
$this->host = array_shift($hostAndUrls);
|
||||||
|
|
||||||
$string = implode('/', $hostAndUrls);
|
$_string = implode('/', $hostAndUrls);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($this->host, ':') !== false) {
|
if (strpos($this->host, ':') !== false) {
|
||||||
@@ -365,7 +365,6 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) {
|
if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) {
|
||||||
var_dump($this->host);
|
|
||||||
$this->host = System::gethostbyname($this->host);
|
$this->host = System::gethostbyname($this->host);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,7 +372,7 @@ class HttpClient
|
|||||||
throw new Exception('Client Host error.');
|
throw new Exception('Client Host error.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user