add clear
This commit is contained in:
+80
-59
@@ -7,6 +7,7 @@ use Swoole\Coroutine\System;
|
||||
|
||||
class HttpClient
|
||||
{
|
||||
|
||||
private $host = 'api.weixin.qq.com';
|
||||
|
||||
private $header = [];
|
||||
@@ -25,10 +26,40 @@ class HttpClient
|
||||
private $ssl_cert_file = '';
|
||||
private $ssl_key_file = '';
|
||||
private $ca = '';
|
||||
private $port = '';
|
||||
|
||||
private $port = 80;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCa(): string
|
||||
{
|
||||
return $this->ca;
|
||||
}
|
||||
|
||||
private $_data = '';
|
||||
/**
|
||||
* @param string $ca
|
||||
*/
|
||||
public function setCa(string $ca): void
|
||||
{
|
||||
$this->ca = $ca;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPort(): string
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $port
|
||||
*/
|
||||
public function setPort(string $port): void
|
||||
{
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
const POST = 'post';
|
||||
const GET = 'get';
|
||||
@@ -43,25 +74,6 @@ 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)
|
||||
{
|
||||
$this->_data = $data;
|
||||
@@ -152,12 +164,43 @@ class HttpClient
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param array $data
|
||||
* @return Result
|
||||
*/
|
||||
public function grpc($path, array $data)
|
||||
{
|
||||
$client = new \Swoole\Coroutine\Client(SWOOLE_TCP);
|
||||
if (empty($this->host) || empty($this->port)) {
|
||||
return new Result(['code' => 0, 'message' => '']);
|
||||
}
|
||||
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' => ''];
|
||||
} else {
|
||||
$param = ['code' => 500, 'message' => $client->errMsg];
|
||||
}
|
||||
return new Result($param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $header
|
||||
*/
|
||||
public function setHeader(array $header)
|
||||
public function setHeader($key, $value)
|
||||
{
|
||||
$this->header = $header;
|
||||
$this->header[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,12 +252,6 @@ class HttpClient
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function setPort($port)
|
||||
{
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@@ -298,7 +335,7 @@ class HttpClient
|
||||
* @param $url
|
||||
* @return mixed
|
||||
*/
|
||||
private function cutRequestUrl(&$url)
|
||||
private function cutRequestUrl($url)
|
||||
{
|
||||
$url = $this->replaceHost($url);
|
||||
$explode = explode('/', $url);
|
||||
@@ -310,7 +347,7 @@ class HttpClient
|
||||
}
|
||||
}
|
||||
$url = '/' . implode('/', $explode);
|
||||
return $first;
|
||||
return [$first, $url];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,9 +357,10 @@ class HttpClient
|
||||
private function replaceHost($host)
|
||||
{
|
||||
if ($this->isHttp($host)) {
|
||||
$host = str_replace('http://', '', $host);
|
||||
} else if ($this->isHttps($host)) {
|
||||
$host = str_replace('https://', '', $host);
|
||||
return str_replace('http://', '', $host);
|
||||
}
|
||||
if ($this->isHttps($host)) {
|
||||
return str_replace('https://', '', $host);
|
||||
}
|
||||
return $host;
|
||||
}
|
||||
@@ -366,7 +404,6 @@ class HttpClient
|
||||
private function coroutine($ip, $url, $data = [])
|
||||
{
|
||||
$client = $this->generate_client($ip, $url, $data);
|
||||
$client->close();
|
||||
if ($client->statusCode < 0) {
|
||||
throw new \Exception($client->errMsg);
|
||||
}
|
||||
@@ -434,7 +471,7 @@ class HttpClient
|
||||
$this->header['User-Agent'] = $this->agent;
|
||||
}
|
||||
if (!empty($this->header)) {
|
||||
$client->setHeaders($this->parseHeaderMat());
|
||||
$client->setHeaders($this->header);
|
||||
}
|
||||
if (!empty($this->_data)) {
|
||||
$client->setData($this->_data);
|
||||
@@ -465,6 +502,7 @@ class HttpClient
|
||||
} else {
|
||||
$client->post($url, $data);
|
||||
}
|
||||
$client->close();
|
||||
return $client;
|
||||
}
|
||||
|
||||
@@ -503,9 +541,6 @@ class HttpClient
|
||||
if (!empty($this->_data)) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);
|
||||
} else {
|
||||
if (is_array($data)) {
|
||||
$data = http_build_query($data);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
}
|
||||
}
|
||||
@@ -560,13 +595,7 @@ class HttpClient
|
||||
*/
|
||||
private function createRequestUrl($url, $data)
|
||||
{
|
||||
if (empty($data)) {
|
||||
return $url;
|
||||
}
|
||||
if ($this->isGet()) {
|
||||
if (strpos($url, '?') !== false) {
|
||||
return $url . '&' . $data;
|
||||
}
|
||||
return $url . '?' . $data;
|
||||
}
|
||||
return $url;
|
||||
@@ -585,15 +614,12 @@ class HttpClient
|
||||
$type = $data['content-type'];
|
||||
if (strpos($type, 'text/html') !== false) {
|
||||
return $body;
|
||||
}
|
||||
if (strpos($type, 'json') !== false) {
|
||||
return Help::toArray($body);
|
||||
}
|
||||
if (strpos($type, 'xml') !== false) {
|
||||
return Help::toArray($body);
|
||||
}
|
||||
if (strpos($type, 'plain') !== false) {
|
||||
} else if (strpos($type, 'json') !== false) {
|
||||
return json_decode($body, true);
|
||||
} else if (strpos($type, 'xml') !== false) {
|
||||
return Help::toArray($body);
|
||||
} else if (strpos($type, 'plain') !== false) {
|
||||
return json_decode($body, true);
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
@@ -625,7 +651,6 @@ class HttpClient
|
||||
{
|
||||
$this->setIsSSL(false);
|
||||
$this->setHeaders([]);
|
||||
$this->setData(null);
|
||||
|
||||
if ($this->callback !== NULL) {
|
||||
$result = call_user_func($this->callback, $body, $_data, $header);
|
||||
@@ -819,11 +844,7 @@ class HttpClient
|
||||
return [];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$header = $key . ':' . $val;
|
||||
if (in_array($header, $this->header)) {
|
||||
continue;
|
||||
}
|
||||
$this->header[] = $header;
|
||||
$this->header[$key] = $val;
|
||||
}
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user