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