Files
kiri-wchat/wchat/common/HttpClient.php
T

947 lines
18 KiB
PHP
Raw Normal View History

2018-07-19 12:10:22 +08:00
<?php
2020-03-05 12:41:49 +08:00
namespace wchat\common;
2018-07-19 12:10:22 +08:00
2020-05-08 19:03:11 +08:00
use Exception;
2020-05-21 16:42:42 +08:00
use Swoole\Coroutine;
2019-12-02 17:07:04 +08:00
use Swoole\Coroutine\Http\Client as SClient;
2019-09-29 17:10:14 +08:00
use Swoole\Coroutine\System;
2020-04-30 22:09:08 +08:00
use Swoole\Coroutine\Client;
2019-05-21 16:01:42 +08:00
2019-11-11 18:14:47 +08:00
class HttpClient
2018-07-19 12:10:22 +08:00
{
2020-03-31 16:14:43 +08:00
2020-04-03 17:37:19 +08:00
private $host = '';
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
private $header = [];
2018-07-19 12:10:22 +08:00
2019-07-17 17:17:37 +08:00
private $callback = null;
private $method = 'get';
private $url = '';
private $isSSL = false;
2019-09-19 18:34:06 +08:00
private $agent = '';
2019-11-11 18:14:47 +08:00
private $isFileStream = false;
private $errorCodeField = '';
private $errorMsgField = '';
2019-12-02 17:07:04 +08:00
private $use_swoole = false;
private $ssl_cert_file = '';
private $ssl_key_file = '';
2020-03-31 16:10:49 +08:00
private $ca = '';
2020-03-31 16:14:43 +08:00
private $port = '';
2019-07-17 17:17:37 +08:00
2020-04-30 22:09:08 +08:00
/** @var string $_message 错误信息 */
private $_message = '';
private $_data = '';
2020-03-31 16:14:43 +08:00
/**
* @return string
*/
public function getCa(): string
{
return $this->ca;
}
2019-07-17 17:17:37 +08:00
2019-11-11 18:14:47 +08:00
/**
2020-03-31 16:14:43 +08:00
* @param string $ca
2019-11-11 18:14:47 +08:00
*/
2020-03-31 16:14:43 +08:00
public function setCa(string $ca): void
2019-09-19 19:04:43 +08:00
{
2020-03-31 16:14:43 +08:00
$this->ca = $ca;
2019-09-19 19:04:43 +08:00
}
2020-03-31 16:14:43 +08:00
2020-03-31 16:10:49 +08:00
/**
* @return string
*/
2020-03-31 16:14:43 +08:00
public function getPort(): string
2020-03-31 16:10:49 +08:00
{
2020-03-31 16:14:43 +08:00
return $this->port;
2020-03-31 16:10:49 +08:00
}
/**
2020-03-31 16:14:43 +08:00
* @param string $port
2020-03-31 16:10:49 +08:00
*/
2020-03-31 16:14:43 +08:00
public function setPort(string $port): void
2020-03-31 16:10:49 +08:00
{
2020-03-31 16:14:43 +08:00
$this->port = $port;
2020-03-31 16:10:49 +08:00
}
2020-03-31 16:14:43 +08:00
const POST = 'post';
const GET = 'get';
const PUT = 'put';
const DELETE = 'delete';
const OPTIONS = 'option';
/**
* HttpClient constructor.
*/
private function __construct()
{
}
2020-03-31 16:10:49 +08:00
2020-04-30 22:09:08 +08:00
/**
* @param $data
*/
2019-12-19 15:57:51 +08:00
public function setData($data)
{
$this->_data = $data;
}
2019-09-19 19:04:43 +08:00
/**
2019-12-02 17:07:04 +08:00
* @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;
}
/**
2019-09-19 19:04:43 +08:00
*/
2019-11-11 18:14:47 +08:00
public static function NewRequest()
2019-09-19 19:04:43 +08:00
{
2019-10-18 10:40:38 +08:00
static $client = null;
2019-11-11 18:14:47 +08:00
if (!($client instanceof HttpClient)) {
$client = new HttpClient();
2019-09-19 19:04:43 +08:00
}
2019-10-18 10:40:38 +08:00
return $client;
2019-09-19 19:04:43 +08:00
}
2019-11-11 18:14:47 +08:00
/**
* @param string $name
* @return $this
*/
public function setErrorField(string $name)
{
$this->errorCodeField = $name;
return $this;
}
2019-12-02 17:07:04 +08:00
/**
* @param $bool
* @return $this
*/
public function setUseSwoole($bool)
{
$this->use_swoole = $bool;
2020-04-03 17:46:43 +08:00
if ($this->use_swoole) {
function_exists('setCli') && setCli(true);
}
2019-12-02 17:07:04 +08:00
return $this;
}
2019-11-11 18:14:47 +08:00
/**
* @param string $name
* @return $this
*/
public function setErrorMsgField(string $name)
{
$this->errorMsgField = $name;
return $this;
}
2019-07-17 17:17:37 +08:00
/**
* @param string $host
*/
public function setHost(string $host)
{
2020-01-07 14:46:50 +08:00
$this->host = $this->replaceHost($host);
$match_quest = '/^[a-zA-Z\-](\.[a-zA-Z\-])*/';
if (preg_match($match_quest, $this->host)) {
$this->addHeader('Host', $this->host);
}
2019-07-17 17:17:37 +08:00
}
2020-03-31 16:14:43 +08:00
/**
* @param $path
* @param array $data
* @return Result
*/
public function grpc($path, array $data)
{
2020-04-30 22:09:08 +08:00
$client = new Client(SWOOLE_TCP);
2020-03-31 16:14:43 +08:00
if (empty($this->host) || empty($this->port)) {
2020-04-30 22:09:08 +08:00
return new Result(['code' => 500, 'message' => 'Host and port is null']);
2020-03-31 16:14:43 +08:00
}
if (!$client->connect($this->host, $this->port)) {
return new Result(['code' => 500, 'message' => $client->errMsg]);
}
$path = $this->port . '/' . ltrim($path, '/');
2020-04-30 22:09:08 +08:00
$params['body'] = $data;
$params['header']['request_uri'] = $path;
$params['header']['request_method'] = 'grpc';
if ($client->send($params)) {
$recv = $client->recv();
$param = ['code' => 0, 'message' => Help::toArray($recv)];
2020-03-31 16:14:43 +08:00
} else {
$param = ['code' => 500, 'message' => $client->errMsg];
}
2020-04-30 22:09:08 +08:00
$client->close();
2020-03-31 16:14:43 +08:00
return new Result($param);
}
2019-07-17 17:17:37 +08:00
/**
* @param array $header
*/
2020-03-31 16:14:43 +08:00
public function setHeader($key, $value)
2019-07-17 17:17:37 +08:00
{
2020-03-31 16:14:43 +08:00
$this->header[$key] = $value;
2019-07-17 17:17:37 +08:00
}
/**
* @param $key
* @param $value
*/
public function addHeader($key, $value)
{
$this->header[$key] = $value;
}
/**
* @param null $callback
*/
public function setCallback($callback)
{
$this->callback = $callback;
}
/**
* @param string $method
*/
public function setMethod(string $method)
{
$this->method = $method;
}
/**
* @param string $url
*/
public function setUrl(string $url)
{
$this->url = $url;
}
2019-09-19 18:34:06 +08:00
public function setAgent(string $agent)
{
$this->agent = $agent;
}
2019-07-17 17:17:37 +08:00
/**
* @param bool $isSSL
*/
public function setIsSSL(bool $isSSL)
{
$this->isSSL = $isSSL;
2019-12-19 10:53:19 +08:00
if ($this->isSSL) {
$this->port = 443;
}
2019-12-18 20:08:51 +08:00
}
2019-11-11 18:14:47 +08:00
/**
* @return bool
*/
2019-11-04 14:39:59 +08:00
public function getIsSSL()
{
return $this->isSSL;
2019-07-17 17:17:37 +08:00
}
2019-11-11 18:14:47 +08:00
/**
* @param bool $isFIle
* 设置返回类型
*/
public function asFileStream($isFIle = true)
{
$this->isFileStream = $isFIle;
}
2019-07-17 17:17:37 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
2020-05-08 19:03:11 +08:00
* @throws Exception
2019-07-12 18:44:54 +08:00
*/
2019-07-17 17:17:37 +08:00
private function request($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-11-11 18:14:47 +08:00
$data = $this->paramEncode($data);
2020-04-03 17:47:22 +08:00
if ($this->use_swoole) {
2020-04-03 18:41:06 +08:00
return $this->coroutine($this->matchHost($url), $data);
2020-01-07 12:02:55 +08:00
} else {
return $this->useCurl($url, $data);
2019-09-29 17:10:14 +08:00
}
2019-12-02 17:07:04 +08:00
}
2020-03-31 16:10:49 +08:00
/**
* @return bool
*/
private function isCli()
{
return function_exists('getIsCli') && getIsCli();
}
2020-04-03 18:27:06 +08:00
/**
* @param string $string
2020-05-08 19:03:11 +08:00
* @return bool|string
* @throws Exception
2020-04-03 18:27:06 +08:00
*/
private function matchHost($string = '')
{
if (empty($string)) {
return false;
}
2020-04-30 22:09:08 +08:00
if ($this->isHttp($string)) {
$string = str_replace('http://', '', $string);
$hostAndUrls = explode('/', $string);
2020-04-03 18:31:29 +08:00
2020-04-30 22:09:08 +08:00
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
} else if ($this->isHttps($string)) {
2020-04-03 18:27:06 +08:00
$string = str_replace('https://', '', $string);
2020-04-03 18:31:29 +08:00
$this->setIsSSL(true);
2020-04-03 18:27:06 +08:00
2020-04-30 22:09:08 +08:00
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
} else if (empty($this->host)) {
$hostAndUrls = explode('/', $string);
$this->host = array_shift($hostAndUrls);
$string = implode('/', $hostAndUrls);
2020-04-03 18:27:06 +08:00
}
2020-04-03 18:31:29 +08:00
2020-04-03 18:27:06 +08:00
if (strpos($this->host, ':') !== false) {
[$this->host, $this->port] = explode(':', $this->host);
}
2020-04-03 18:31:29 +08:00
2020-05-21 16:42:42 +08:00
if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) {
2020-04-30 22:09:08 +08:00
$this->host = System::gethostbyname($this->host);
2020-04-03 18:27:06 +08:00
}
2020-04-03 18:31:29 +08:00
2020-04-30 22:09:08 +08:00
if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) {
2020-05-08 19:03:11 +08:00
throw new Exception('Client Host error.');
2020-04-30 22:09:08 +08:00
}
2020-04-30 18:11:59 +08:00
2020-04-30 22:09:08 +08:00
return $string;
2020-04-03 18:27:06 +08:00
}
/**
* @param $name
* @return bool|mixed
*/
private function isDomainName($name)
{
if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) {
return false;
}
return $out[0];
}
2019-12-02 17:07:04 +08:00
/**
* @param $url
* @param $data
* @return array|Result|mixed
2020-04-30 22:09:08 +08:00
* @throws
2019-12-02 17:07:04 +08:00
*/
private function useCurl($url, $data)
{
2019-11-11 18:14:47 +08:00
if ($this->isHttp($url) || $this->isHttps($url)) {
2019-07-17 17:17:37 +08:00
return $this->curl($url, $data);
2019-07-12 18:44:54 +08:00
}
2020-04-30 22:09:08 +08:00
$url = $this->matchHost(ltrim($url, '/'));
if (!empty($this->port)) {
$this->host .= ':' . $this->port;
}
2019-07-17 17:17:37 +08:00
if ($this->isSSL) {
return $this->curl('https://' . $this->host . '/' . $url, $data);
} else {
return $this->curl('http://' . $this->host . '/' . $url, $data);
}
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2020-01-07 12:35:04 +08:00
/**
* @param $host
* @return string|string[]
*/
private function replaceHost($host)
{
if ($this->isHttp($host)) {
2020-03-31 16:14:43 +08:00
return str_replace('http://', '', $host);
}
if ($this->isHttps($host)) {
return str_replace('https://', '', $host);
2019-12-18 20:02:47 +08:00
}
2020-01-07 12:35:04 +08:00
return $host;
}
/**
* @param $url
* @return false|int
*/
private function checkIsIp($url)
{
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
2019-11-11 18:14:47 +08:00
}
/**
* @param $url
* @return bool
*/
private function isHttp($url)
{
return strpos($url, 'http://') === 0;
}
/**
* @param $url
* @return bool
*/
private function isHttps($url)
{
return strpos($url, 'https://') === 0;
}
2019-07-12 18:44:54 +08:00
/**
2019-07-12 18:50:05 +08:00
* @param $url
2019-07-12 18:44:54 +08:00
* @param array $data
2019-07-12 18:50:05 +08:00
* @return array|mixed|Result
2020-05-08 19:03:11 +08:00
* @throws Exception
2019-07-12 18:44:54 +08:00
* 使用swoole协程方式请求
*/
2020-04-03 18:41:06 +08:00
private function coroutine($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2020-04-30 22:09:08 +08:00
try {
$client = $this->generate_client($this->host, $url, $data);
if ($client->statusCode < 0) {
2020-05-08 19:03:11 +08:00
throw new Exception($client->errMsg);
2020-04-30 22:09:08 +08:00
}
unset($this->_data);
2020-01-10 19:58:39 +08:00
2020-04-30 22:09:08 +08:00
$body = $this->resolve($client->getHeaders(), $client->body);
if ($client->getStatusCode() != 200) {
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
}
$response['code'] = $client->getStatusCode();
$response['message'] = $message;
$response['data'] = $body;
2020-03-31 16:10:49 +08:00
} else {
2020-04-30 22:09:08 +08:00
$response = $this->structure($body, $data, $client->getHeaders());
2020-03-31 16:10:49 +08:00
}
2020-04-30 22:09:08 +08:00
} catch (\Throwable $exception) {
$response['code'] = 500;
$response['message'] = $exception->getMessage();
$response['data'] = array_slice($exception->getTrace(), 0, 6);
$response['header'] = [];
2019-12-02 17:07:04 +08:00
}
2020-04-30 22:09:08 +08:00
if (!($response instanceof Result)) {
$response = new Result($response);
}
return $response;
2019-07-17 17:17:37 +08:00
}
2019-05-21 16:01:42 +08:00
2019-07-17 17:17:37 +08:00
/**
* @return int
*/
private function getHostPort()
{
2019-12-18 20:08:51 +08:00
if (!empty($this->port)) {
return $this->port;
}
2019-07-17 17:17:37 +08:00
$port = 80;
if ($this->isSSL) $port = 443;
return $port;
}
/**
2019-09-29 17:10:14 +08:00
* @param $host
2019-07-17 17:17:37 +08:00
* @param $port
* @param $url
* @param $data
2019-12-02 17:07:04 +08:00
* @return \Swoole\Coroutine\Http\Client
2019-07-17 17:17:37 +08:00
*/
2020-04-30 22:09:08 +08:00
private function generate_client($host, $url, $data = [])
2019-07-17 17:17:37 +08:00
{
2019-12-02 17:07:04 +08:00
$client = new SClient($host, $this->getHostPort(), $this->isSSL);
2020-04-05 23:41:18 +08:00
if (strpos($url, '/') !== 0) {
$url = '/' . $url;
}
2020-04-30 22:09:08 +08:00
2020-04-06 02:07:44 +08:00
$client->set($this->settings());
2019-09-19 18:34:06 +08:00
if (!empty($this->agent)) {
$this->header['User-Agent'] = $this->agent;
}
2019-07-12 18:44:54 +08:00
if (!empty($this->header)) {
2020-03-31 16:14:43 +08:00
$client->setHeaders($this->header);
2019-07-12 18:44:54 +08:00
}
2020-04-30 22:09:08 +08:00
$client->setMethod(strtoupper($this->method));
if (strtolower($this->method) == self::GET && !empty($data)) {
$url .= '?' . $data;
} else {
$this->_data = $this->mergeParams($data);
}
2020-01-13 17:32:04 +08:00
if (!empty($this->_data)) {
$client->setData($this->_data);
}
2020-04-30 22:09:08 +08:00
$client->execute($url);
2020-03-31 16:14:43 +08:00
$client->close();
2019-07-17 17:17:37 +08:00
return $client;
2019-07-12 18:44:54 +08:00
}
2019-05-21 16:01:42 +08:00
2020-04-30 22:09:08 +08:00
/**
* @param $newData
* @return mixed
*/
private function mergeParams($newData)
{
if (empty($this->_data)) {
return $this->toRequest($newData);
} else if (empty($newData)) {
return $this->toRequest($this->_data);
}
$newData = Help::toArray($newData);
$array = Help::toArray($this->_data);
$params = array_merge($array, $newData);
return $this->toRequest($params);
}
/**
* @param $data
* @return false|mixed|string
*/
private function toRequest($data)
{
if (!is_array($data)) {
$data = Help::toArray($data);
2020-05-08 19:03:11 +08:00
if (is_string($data)) {
return $data;
}
2020-04-30 22:09:08 +08:00
}
$contentType = $this->header['Content-Type'] ?? 'application/json';
if (strpos($contentType, 'json') !== false) {
return Help::toJson($data);
} else if (strpos($contentType, 'xml') !== false) {
return Help::toXml($data);
} else {
return Help::toJson($data);
}
}
2020-04-03 17:22:44 +08:00
2020-04-30 22:09:08 +08:00
/**
* @return array
*/
2020-04-03 17:22:44 +08:00
private function settings()
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
2020-04-03 17:41:14 +08:00
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return [];
}
2020-04-03 17:42:47 +08:00
2020-04-03 17:22:44 +08:00
$params['ssl_host_name'] = $this->host;
$params['ssl_cert_file'] = $this->getSslCertFile();
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = true;
$params['ssl_cafile'] = $sslCa;
return $params;
}
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
*/
2019-07-17 17:17:37 +08:00
private function curl($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-12-02 17:07:04 +08:00
try {
2020-04-30 22:09:08 +08:00
$output = $this->curlParse($url, $this->mergeParams($data));
2019-12-02 17:07:04 +08:00
if ($output === FALSE) {
2020-04-30 22:09:08 +08:00
return new Result(['code' => 500, 'message' => $output]);
}
[$header, $body, $status] = $this->explode($output);
if ($status != 200 && $status != 201) {
$data = new Result(['code' => $status, 'message' => $body, 'header' => $header]);
2020-01-07 11:57:56 +08:00
} else {
2020-04-30 22:09:08 +08:00
$data = $this->structure($body, $data, $header);
2019-12-02 17:07:04 +08:00
}
2020-01-07 11:57:56 +08:00
return $data;
2020-04-30 22:09:08 +08:00
} catch (\Throwable $exception) {
$response['code'] = 500;
$response['message'] = $exception->getMessage();
$response['data'] = array_slice($exception->getTrace(), 0, 6);
$response['header'] = [];
return new Result($response);
}
}
/**
* @param $url
* @param $data
* @return bool|string
2020-05-08 19:03:11 +08:00
* @throws Exception
2020-04-30 22:09:08 +08:00
*/
private function curlParse($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, true);
if ($headers = $this->parseHeaderMat()) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
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);
if ($output === false) {
2020-05-08 19:03:11 +08:00
throw new Exception(curl_error($ch));
2019-11-11 18:14:47 +08:00
}
2020-04-30 22:09:08 +08:00
curl_close($ch);
return $output;
2019-07-12 18:44:54 +08:00
}
2019-05-21 16:01:42 +08:00
2020-01-03 15:12:06 +08:00
/**
* @param $output
* @return array
*/
private function explode($output)
{
2020-01-03 15:35:28 +08:00
[$header, $body] = explode("\r\n\r\n", $output, 2);
2020-01-03 15:38:45 +08:00
if ($header == 'HTTP/1.1 100 Continue') {
[$header, $body] = explode("\r\n\r\n", $body, 2);
} else if (strpos($body, "\r\n\r\n") !== false) {
2020-01-03 15:36:18 +08:00
[$header, $body] = explode("\r\n\r\n", $body, 2);
}
2020-01-03 15:32:26 +08:00
$header = explode("\r\n", $header);
2020-01-03 15:12:06 +08:00
unset($output);
$status = (int)explode(' ', trim($header[0]))[1];
$header = $this->headerFormat($header);
return [$header, $this->resolve($header, $body), $status];
}
2019-07-12 18:44:54 +08:00
/**
* @param $url
2019-11-11 18:14:47 +08:00
* @param $data
* @return string
2019-07-12 18:44:54 +08:00
*/
2019-11-11 18:14:47 +08:00
private function createRequestUrl($url, $data)
2019-07-12 18:44:54 +08:00
{
2019-11-11 18:14:47 +08:00
if ($this->isGet()) {
return $url . '?' . $data;
2019-07-17 17:17:37 +08:00
}
2019-11-11 18:14:47 +08:00
return $url;
}
/**
* @param $data
* @param $body
* @return mixed
*/
private function resolve($data, $body)
{
2020-01-03 11:43:20 +08:00
if (is_array($body)) {
return $body;
}
2020-04-03 17:27:30 +08:00
$type = $data['content-type'] ?? 'text/html';
2020-01-03 11:55:14 +08:00
if (strpos($type, 'text/html') !== false) {
2019-11-11 18:14:47 +08:00
return $body;
2020-03-31 16:14:43 +08:00
} else if (strpos($type, 'json') !== false) {
return json_decode($body, true);
} else if (strpos($type, 'xml') !== false) {
2020-05-21 16:12:20 +08:00
return Help::xmlToArray($body);
2020-03-31 16:14:43 +08:00
} else if (strpos($type, 'plain') !== false) {
2020-04-01 13:09:16 +08:00
return Help::toArray($body);
2019-09-19 18:34:06 +08:00
}
2020-01-03 11:55:14 +08:00
return $body;
2019-11-11 18:14:47 +08:00
}
/**
* @param $headers
* @return array
*/
private function headerFormat($headers)
{
$_tmp = [];
foreach ($headers as $key => $val) {
$trim = explode(': ', trim($val));
2020-01-03 15:15:00 +08:00
$_tmp[strtolower($trim[0])] = $trim[1] ?? '';
2019-07-12 18:44:54 +08:00
}
2019-11-11 18:14:47 +08:00
return $_tmp;
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-17 17:17:37 +08:00
/**
* @param $body
* @param $_data
2019-11-11 18:14:47 +08:00
* @param $header
2019-12-02 17:07:04 +08:00
* @param $statusCode
2019-07-17 17:17:37 +08:00
* @return array|mixed|Result
* 构建返回体
*/
2019-12-02 17:07:04 +08:00
private function structure($body, $_data, $header = [], $statusCode = 200)
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
$this->setIsSSL(false);
2019-08-21 13:27:12 +08:00
$this->setHeaders([]);
2019-07-17 17:17:37 +08:00
if ($this->callback !== NULL) {
2019-11-11 18:14:47 +08:00
$result = call_user_func($this->callback, $body, $_data, $header);
$this->setCallback(null);
2019-07-17 17:17:37 +08:00
return $result;
}
2020-01-09 19:09:41 +08:00
if (is_string($body)) {
$result['code'] = 0;
$result['message'] = '';
} else {
$result['code'] = $body[$this->errorCodeField] ?? 0;
2020-01-10 19:41:58 +08:00
$result['message'] = $this->searchMessageByData($body);
2020-01-09 19:09:41 +08:00
}
2019-11-11 18:14:47 +08:00
$result['data'] = $body;
$result['header'] = $header;
2019-12-02 17:07:04 +08:00
$result['httpStatus'] = $statusCode;
2020-04-01 13:08:12 +08:00
2019-07-17 17:17:37 +08:00
return new Result($result);
}
2020-01-10 19:41:58 +08:00
/**
* @param $body
* @return array|mixed|string
*/
private function searchMessageByData($body)
{
$parent = [];
if (empty($this->errorMsgField)) {
return 'system success.';
}
$explode = explode('.', $this->errorMsgField);
2020-01-10 20:00:19 +08:00
if (!isset($body[$explode[0]])) {
return 'system success.';
}
2020-01-10 19:41:58 +08:00
foreach ($explode as $item) {
if (empty($item)) {
continue;
}
if (empty($parent)) {
$parent = $body[$item];
continue;
}
2020-01-10 19:45:01 +08:00
if (is_string($parent) || !isset($parent[$item])) {
2020-01-10 19:41:58 +08:00
break;
}
2020-01-10 19:45:01 +08:00
$parent = $parent[$item];
2020-01-10 19:41:58 +08:00
}
return !empty($parent) ? $parent : 'system success.';
}
2019-07-17 17:17:37 +08:00
/**
2019-11-11 18:14:47 +08:00
* @return bool
* check isPost Request
2019-07-17 17:17:37 +08:00
*/
2019-11-11 18:14:47 +08:00
public function isPost()
2019-07-17 17:17:37 +08:00
{
2019-11-11 18:14:47 +08:00
return strtolower($this->method) === self::POST;
}
/**
* @return bool
*
* check isGet Request
*/
public function isGet()
{
return strtolower($this->method) === self::GET;
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $arr
*
* @return array|string
* 将请求参数进行编码
*/
2019-11-11 18:14:47 +08:00
private function paramEncode($arr)
2019-07-12 18:44:54 +08:00
{
if (!is_array($arr)) {
return $arr;
}
$_tmp = [];
foreach ($arr as $Key => $val) {
$_tmp[$Key] = $val;
}
2019-11-11 18:14:47 +08:00
if ($this->isGet()) {
return http_build_query($_tmp);
}
return $_tmp;
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws
*/
2019-07-17 17:17:37 +08:00
public function post($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
$this->setMethod(self::POST);
return $this->request($url, $data);
2019-07-12 18:44:54 +08:00
}
2019-05-21 16:01:42 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws
*/
2019-07-17 17:17:37 +08:00
public function put($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
$this->setMethod(self::PUT);
return $this->request($url, $data);
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws
*/
2019-07-17 17:17:37 +08:00
public function get($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
$this->setMethod(self::GET);
return $this->request($url, $data);
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
2020-05-08 19:03:11 +08:00
* @throws Exception
2019-07-12 18:44:54 +08:00
*/
2019-07-17 17:17:37 +08:00
public function option($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
$this->setMethod(self::OPTIONS);
return $this->request($url, $data);
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
2020-05-08 19:03:11 +08:00
* @throws Exception
2019-07-17 17:17:37 +08:00
*/
public function delete($url, $data = [])
{
$this->setMethod(self::DELETE);
return $this->request($url, $data);
2019-07-12 18:44:54 +08:00
}
2018-07-19 12:10:22 +08:00
2019-07-12 18:44:54 +08:00
/**
* @param $url
* @param array $data
* @return array|mixed|Result
2020-05-08 19:03:11 +08:00
* @throws Exception
2019-07-12 18:44:54 +08:00
*/
2019-07-17 17:17:37 +08:00
public function send($url, $data = [])
2019-07-12 18:44:54 +08:00
{
2019-07-17 17:17:37 +08:00
return $this->request($url, $data);
2019-07-12 18:44:54 +08:00
}
2019-12-02 17:07:04 +08:00
/**
* @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;
}
2019-07-12 18:44:54 +08:00
/**
* @param array $headers
* @return array
*/
public function setHeaders(array $headers)
{
if (empty($headers)) {
return [];
}
foreach ($headers as $key => $val) {
2020-03-31 16:14:43 +08:00
$this->header[$key] = $val;
2019-07-12 18:44:54 +08:00
}
return $this->header;
}
2018-07-19 12:10:22 +08:00
}