This commit is contained in:
2021-12-11 16:13:10 +08:00
parent 2165797410
commit b945c19200
3 changed files with 902 additions and 858 deletions
+30 -7
View File
@@ -38,7 +38,7 @@ abstract class ClientAbstracts implements IClient
private string $method = 'get'; private string $method = 'get';
private bool $isSSL = false; private bool $isSSL = FALSE;
private string $agent = ''; private string $agent = '';
private string $ssl_cert_file = ''; private string $ssl_cert_file = '';
@@ -53,13 +53,16 @@ abstract class ClientAbstracts implements IClient
private int $statusCode = 200; private int $statusCode = 200;
private bool $VERIFYPEER = TRUE;
/** /**
* @var string|null * @var string|null
*/ */
protected ?string $body; protected ?string $body;
private ?StreamInterface $_data = null; private ?StreamInterface $_data = NULL;
private int $connect_timeout = 1; private int $connect_timeout = 1;
@@ -70,6 +73,26 @@ abstract class ClientAbstracts implements IClient
protected mixed $client; protected mixed $client;
/**
* @param $bool
* @return $this
*/
public function withVerifyPeer($bool): static
{
$this->VERIFYPEER = $bool;
return $this;
}
/**
* @return bool
*/
public function getVerifyPeer(): bool
{
return $this->VERIFYPEER;
}
/** /**
* @return int * @return int
*/ */
@@ -94,7 +117,7 @@ abstract class ClientAbstracts implements IClient
*/ */
public function getResponseHeader(string $key): null|string|int public function getResponseHeader(string $key): null|string|int
{ {
return $this->_responseHeader[$key] ?? null; return $this->_responseHeader[$key] ?? NULL;
} }
@@ -139,7 +162,7 @@ abstract class ClientAbstracts implements IClient
* @param $port * @param $port
* @param false $isSSL * @param false $isSSL
*/ */
public function __construct($host, $port, bool $isSSL = false) public function __construct($host, $port, bool $isSSL = FALSE)
{ {
$this->withHost($host)->withPort($port)->withIsSSL($isSSL); $this->withHost($host)->withPort($port)->withIsSSL($isSSL);
} }
@@ -282,7 +305,7 @@ abstract class ClientAbstracts implements IClient
*/ */
public function getContentType(): ?string public function getContentType(): ?string
{ {
return $this->header['Content-Type'] ?? $this->header['content-type'] ?? null; return $this->header['Content-Type'] ?? $this->header['content-type'] ?? NULL;
} }
@@ -625,7 +648,7 @@ abstract class ClientAbstracts implements IClient
if (str_contains($type, 'text/html')) { if (str_contains($type, 'text/html')) {
return $body; return $body;
} else if (str_contains($type, 'json')) { } else if (str_contains($type, 'json')) {
return json_decode($body, true); return json_decode($body, TRUE);
} else if (str_contains($type, 'xml')) { } else if (str_contains($type, 'xml')) {
return Help::xmlToArray($body); return Help::xmlToArray($body);
} else if (str_contains($type, 'plain')) { } else if (str_contains($type, 'plain')) {
@@ -692,7 +715,7 @@ abstract class ClientAbstracts implements IClient
*/ */
protected function matchHost(string $string): array protected function matchHost(string $string): array
{ {
if (($parse = isUrl($string, true)) === false) { if (($parse = isUrl($string, TRUE)) === FALSE) {
return $this->defaultString($string); return $this->defaultString($string);
} }
[$isHttps, $domain, $port, $path] = $parse; [$isHttps, $domain, $port, $path] = $parse;
+10
View File
@@ -39,6 +39,16 @@ class CoroutineClient extends ClientAbstracts
} }
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
return $this;
}
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
+24 -13
View File
@@ -51,7 +51,7 @@ class Curl extends ClientAbstracts
$host .= ':' . $this->getPort(); $host .= ':' . $this->getPort();
} }
$this->do(curl_init($host . $path), $host . $path, $method); $this->do(curl_init($host . $path), $host . $path, $method);
if ($isHttps !== false) { if ($isHttps !== FALSE) {
$this->curlHandlerSslSet(); $this->curlHandlerSslSet();
} }
$contents = $this->getData()->getContents(); $contents = $this->getData()->getContents();
@@ -74,12 +74,12 @@ class Curl extends ClientAbstracts
*/ */
private function curlHandlerSslSet(): void private function curlHandlerSslSet(): void
{ {
// if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) { if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
// curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile()); curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
// } }
// if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) { if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
// curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile()); curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
// } }
} }
@@ -94,14 +94,14 @@ class Curl extends ClientAbstracts
curl_setopt($resource, CURLOPT_URL, $path); curl_setopt($resource, CURLOPT_URL, $path);
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置
curl_setopt($resource, CURLOPT_HEADER, true); curl_setopt($resource, CURLOPT_HEADER, TRUE);
curl_setopt($resource, CURLOPT_FAILONERROR, true); curl_setopt($resource, CURLOPT_FAILONERROR, TRUE);
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
if (defined('CURLOPT_SSL_FALSESTART')) { if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_SSL_FALSESTART, true); curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
} }
curl_setopt($resource, CURLOPT_FORBID_REUSE, false); curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE);
curl_setopt($resource, CURLOPT_FRESH_CONNECT, false); curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE);
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent());
} }
@@ -117,13 +117,24 @@ class Curl extends ClientAbstracts
} }
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
curl_setopt($this->client, CURLOPT_CAINFO, $path);
return $this;
}
/** /**
* @throws Exception * @throws Exception
*/ */
private function execute(): void private function execute(): void
{ {
$output = curl_exec($this->client); $output = curl_exec($this->client);
if ($output === false) { if ($output === FALSE) {
$this->setStatusCode(curl_errno($this->client)); $this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client)); $this->setBody(curl_error($this->client));
} else { } else {