add clear
This commit is contained in:
+153
-52
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace common;
|
namespace common;
|
||||||
|
|
||||||
use Swoole\Coroutine\Http\Client;
|
use Swoole\Coroutine\Http\Client as SClient;
|
||||||
use Swoole\Coroutine\System;
|
use Swoole\Coroutine\System;
|
||||||
|
|
||||||
class HttpClient
|
class HttpClient
|
||||||
@@ -20,6 +20,10 @@ class HttpClient
|
|||||||
private $isFileStream = false;
|
private $isFileStream = false;
|
||||||
private $errorCodeField = '';
|
private $errorCodeField = '';
|
||||||
private $errorMsgField = '';
|
private $errorMsgField = '';
|
||||||
|
private $use_swoole = false;
|
||||||
|
|
||||||
|
private $ssl_cert_file = '';
|
||||||
|
private $ssl_key_file = '';
|
||||||
|
|
||||||
const POST = 'post';
|
const POST = 'post';
|
||||||
const GET = 'get';
|
const GET = 'get';
|
||||||
@@ -35,7 +39,38 @@ class HttpClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return HttpClient
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslCertFile(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_cert_file
|
||||||
|
*/
|
||||||
|
public function setSslCertFile(string $ssl_cert_file)
|
||||||
|
{
|
||||||
|
$this->ssl_cert_file = $ssl_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslKeyFile(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_key_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_key_file
|
||||||
|
*/
|
||||||
|
public function setSslKeyFile(string $ssl_key_file)
|
||||||
|
{
|
||||||
|
$this->ssl_key_file = $ssl_key_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
*/
|
*/
|
||||||
public static function NewRequest()
|
public static function NewRequest()
|
||||||
{
|
{
|
||||||
@@ -56,6 +91,16 @@ class HttpClient
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $bool
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setUseSwoole($bool)
|
||||||
|
{
|
||||||
|
$this->use_swoole = $bool;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -154,9 +199,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) {
|
||||||
|
return $this->useCurl($url, $data);
|
||||||
|
}
|
||||||
if (function_exists('getIsCli') && getIsCli()) {
|
if (function_exists('getIsCli') && getIsCli()) {
|
||||||
return $this->coroutine($this->parseUrlHost($url), $url, $data);
|
return $this->coroutine($this->parseUrlHost($url), $url, $data);
|
||||||
}
|
}
|
||||||
|
return $this->useCurl($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return array|Result|mixed
|
||||||
|
*/
|
||||||
|
private function useCurl($url, $data)
|
||||||
|
{
|
||||||
if ($this->isHttp($url) || $this->isHttps($url)) {
|
if ($this->isHttp($url) || $this->isHttps($url)) {
|
||||||
return $this->curl($url, $data);
|
return $this->curl($url, $data);
|
||||||
}
|
}
|
||||||
@@ -219,7 +277,15 @@ class HttpClient
|
|||||||
$body = $client->body;
|
$body = $client->body;
|
||||||
$client->close();
|
$client->close();
|
||||||
|
|
||||||
return $this->structure($body, $data);
|
$header = $client->getHeaders();
|
||||||
|
|
||||||
|
$body = $this->resolve($header, $body);
|
||||||
|
|
||||||
|
if ($client->getStatusCode() != 200) {
|
||||||
|
return new Result(['code' => $client->getStatusCode(), 'message' => $body, 'header' => $header]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->structure($body, $data, $header);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,20 +312,23 @@ class HttpClient
|
|||||||
* @param $port
|
* @param $port
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param $data
|
* @param $data
|
||||||
* @return Client
|
* @return \Swoole\Coroutine\Http\Client
|
||||||
*/
|
*/
|
||||||
private function generate_client($host, $url, $data)
|
private function generate_client($host, $url, $data)
|
||||||
{
|
{
|
||||||
$client = new Client($host, $this->getHostPort(), $this->isSSL);
|
$client = new SClient($host, $this->getHostPort(), $this->isSSL);
|
||||||
if (!empty($this->agent)) {
|
if (!empty($this->agent)) {
|
||||||
$this->header['User-Agent'] = $this->agent;
|
$this->header['User-Agent'] = $this->agent;
|
||||||
}
|
}
|
||||||
if (!empty($this->header)) {
|
if (!empty($this->header)) {
|
||||||
$client->setHeaders($this->header);
|
$client->setHeaders($this->parseHeaderMat());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strtolower($this->method) == self::GET) {
|
if (strtolower($this->method) == self::GET) {
|
||||||
|
echo $host . ':' . $this->getHostPort() . ' ' . $url . '?' . $data;
|
||||||
$client->get($url . '?' . $data);
|
$client->get($url . '?' . $data);
|
||||||
} else {
|
} else {
|
||||||
|
echo $host . ':' . $this->getHostPort() . ' ' . $url;
|
||||||
$client->post($url, $data);
|
$client->post($url, $data);
|
||||||
}
|
}
|
||||||
return $client;
|
return $client;
|
||||||
@@ -272,49 +341,54 @@ class HttpClient
|
|||||||
*/
|
*/
|
||||||
private function curl($url, $data = [])
|
private function curl($url, $data = [])
|
||||||
{
|
{
|
||||||
$ch = curl_init();
|
echo $url . PHP_EOL;
|
||||||
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
|
try {
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
|
||||||
if (!empty($this->header)) {
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
}
|
|
||||||
if (!empty($this->agent)) {
|
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
|
|
||||||
}
|
|
||||||
if ($this->isSSL) {
|
|
||||||
curl_setopt($ch, CURLOPT_SSLCERT, $this->header['ssl_cert_file']);
|
|
||||||
curl_setopt($ch, CURLOPT_SSLKEY, $this->header['ssl_key_file']);
|
|
||||||
}
|
|
||||||
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
|
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
|
||||||
|
|
||||||
if ($this->method != self::GET) {
|
if ($headers = $this->parseHeaderMat()) {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
} else if ($this->method == self::POST) {
|
}
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
|
||||||
|
if (!empty($this->agent)) {
|
||||||
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
|
||||||
|
}
|
||||||
|
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
|
||||||
|
curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
||||||
|
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
|
||||||
|
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
||||||
|
|
||||||
|
if ($this->method == self::POST) {
|
||||||
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
|
}
|
||||||
|
if ($this->method != self::GET) {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
|
||||||
|
$output = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($output === FALSE) {
|
||||||
|
return new Result(['code' => 500, 'message' => curl_error($ch)]);
|
||||||
|
}
|
||||||
|
list($header, $body) = explode("\r\n\r\n", $output, 2);
|
||||||
|
$header = explode(PHP_EOL, $header);
|
||||||
|
$status = (int)explode(' ', trim($header[0]))[1];
|
||||||
|
$header = $this->headerFormat($header);
|
||||||
|
if ($status != 200) {
|
||||||
|
return new Result(['code' => 500, 'message' => $body, 'header' => $header]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->structure($this->resolve($header, $body), $data, $header);
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
return new Result(['code' => 500, 'message' => $exception->getMessage(), 'header' => []]);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
|
|
||||||
$output = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
if ($output === FALSE) {
|
|
||||||
return new Result(['code' => 500, 'message' => curl_error($ch)]);
|
|
||||||
}
|
|
||||||
list($header, $body) = explode("\r\n\r\n", $output, 2);
|
|
||||||
$header = explode(PHP_EOL, $header);
|
|
||||||
$status = (int)explode(' ', trim($header[0]))[1];
|
|
||||||
$header = $this->headerFormat($header);
|
|
||||||
|
|
||||||
if ($status != 200) {
|
|
||||||
return new Result(['code' => 500, 'message' => $body, 'header' => $header]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->structure($this->resolve($header, $body), $data, $header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -337,12 +411,13 @@ class HttpClient
|
|||||||
*/
|
*/
|
||||||
private function resolve($data, $body)
|
private function resolve($data, $body)
|
||||||
{
|
{
|
||||||
if (strpos('json', $data['Content-Type']) !== false) {
|
$type = $data['Content-Type'] ?? $data['content-type'];
|
||||||
|
if (strpos($type, 'json') !== false) {
|
||||||
return json_decode($body, true);
|
return json_decode($body, true);
|
||||||
} else if (strpos('xml', $data['Content-Type']) !== false) {
|
} else if (strpos($type, 'xml') !== false) {
|
||||||
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
|
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||||
return json_decode(json_encode($data), TRUE);
|
return json_decode(json_encode($data), TRUE);
|
||||||
} else if (strpos('plain', $data['Content-Type']) !== false) {
|
} else if (strpos($type, 'plain') !== false) {
|
||||||
return json_decode($data, TRUE);
|
return json_decode($data, TRUE);
|
||||||
} else {
|
} else {
|
||||||
return $body;
|
return $body;
|
||||||
@@ -368,10 +443,11 @@ class HttpClient
|
|||||||
* @param $body
|
* @param $body
|
||||||
* @param $_data
|
* @param $_data
|
||||||
* @param $header
|
* @param $header
|
||||||
|
* @param $statusCode
|
||||||
* @return array|mixed|Result
|
* @return array|mixed|Result
|
||||||
* 构建返回体
|
* 构建返回体
|
||||||
*/
|
*/
|
||||||
private function structure($body, $_data, $header = [])
|
private function structure($body, $_data, $header = [], $statusCode = 200)
|
||||||
{
|
{
|
||||||
$this->setIsSSL(false);
|
$this->setIsSSL(false);
|
||||||
$this->setHeaders([]);
|
$this->setHeaders([]);
|
||||||
@@ -390,6 +466,7 @@ class HttpClient
|
|||||||
$result['message'] = $body[$this->errorMsgField] ?? 'system success.';
|
$result['message'] = $body[$this->errorMsgField] ?? 'system success.';
|
||||||
$result['data'] = $body;
|
$result['data'] = $body;
|
||||||
$result['header'] = $header;
|
$result['header'] = $header;
|
||||||
|
$result['httpStatus'] = $statusCode;
|
||||||
return new Result($result);
|
return new Result($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -505,6 +582,26 @@ class HttpClient
|
|||||||
return $this->request($url, $data);
|
return $this->request($url, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function parseHeaderMat()
|
||||||
|
{
|
||||||
|
if ($this->use_swoole) {
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
$headers = [];
|
||||||
|
foreach ($this->header as $key => $val) {
|
||||||
|
$header = $key . ':' . $val;
|
||||||
|
if (in_array($header, $headers)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$headers[] = $header;
|
||||||
|
}
|
||||||
|
$this->header = [];
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $headers
|
* @param array $headers
|
||||||
* @return array
|
* @return array
|
||||||
@@ -515,7 +612,11 @@ class HttpClient
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
foreach ($headers as $key => $val) {
|
foreach ($headers as $key => $val) {
|
||||||
$this->header[] = $key . ':' . $val;
|
$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