add clear
This commit is contained in:
@@ -0,0 +1,522 @@
|
||||
<?php
|
||||
|
||||
namespace common;
|
||||
|
||||
use Swoole\Coroutine\Http\Client;
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
class HttpClient
|
||||
{
|
||||
private $host = 'api.weixin.qq.com';
|
||||
|
||||
private $header = [];
|
||||
|
||||
private $callback = null;
|
||||
private $method = 'get';
|
||||
|
||||
private $url = '';
|
||||
private $isSSL = false;
|
||||
private $agent = '';
|
||||
private $isFileStream = false;
|
||||
private $errorCodeField = '';
|
||||
private $errorMsgField = '';
|
||||
|
||||
const POST = 'post';
|
||||
const GET = 'get';
|
||||
const PUT = 'put';
|
||||
const DELETE = 'delete';
|
||||
const OPTIONS = 'option';
|
||||
|
||||
/**
|
||||
* HttpClient constructor.
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HttpClient
|
||||
*/
|
||||
public static function NewRequest()
|
||||
{
|
||||
static $client = null;
|
||||
if (!($client instanceof HttpClient)) {
|
||||
$client = new HttpClient();
|
||||
}
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return $this
|
||||
*/
|
||||
public function setErrorField(string $name)
|
||||
{
|
||||
$this->errorCodeField = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return $this
|
||||
*/
|
||||
public function setErrorMsgField(string $name)
|
||||
{
|
||||
$this->errorMsgField = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
*/
|
||||
public function setHost(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $header
|
||||
*/
|
||||
public function setHeader(array $header)
|
||||
{
|
||||
$this->header = $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
public function setAgent(string $agent)
|
||||
{
|
||||
$this->agent = $agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isSSL
|
||||
*/
|
||||
public function setIsSSL(bool $isSSL)
|
||||
{
|
||||
$this->isSSL = $isSSL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsSSL()
|
||||
{
|
||||
return $this->isSSL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isFIle
|
||||
* 设置返回类型
|
||||
*/
|
||||
public function asFileStream($isFIle = true)
|
||||
{
|
||||
$this->isFileStream = $isFIle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function request($url, $data = [])
|
||||
{
|
||||
$data = $this->paramEncode($data);
|
||||
if (function_exists('getIsCli') && getIsCli()) {
|
||||
return $this->coroutine($this->parseUrlHost($url), $url, $data);
|
||||
}
|
||||
if ($this->isHttp($url) || $this->isHttps($url)) {
|
||||
return $this->curl($url, $data);
|
||||
}
|
||||
if ($this->isSSL) {
|
||||
return $this->curl('https://' . $this->host . '/' . $url, $data);
|
||||
} else {
|
||||
return $this->curl('http://' . $this->host . '/' . $url, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @return string
|
||||
*/
|
||||
private function parseUrlHost(&$url)
|
||||
{
|
||||
if ($this->isHttp($url)) {
|
||||
$url = str_replace('http://', '', $url);
|
||||
} else if ($this->isHttps($url)) {
|
||||
$url = str_replace('https://', '', $url);
|
||||
}
|
||||
$explode = explode('/', $url);
|
||||
return System::gethostbyname(array_shift($explode));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $ip
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
* 使用swoole协程方式请求
|
||||
*/
|
||||
private function coroutine($ip, $url, $data = [])
|
||||
{
|
||||
$client = $this->generate_client($ip, $url, $data);
|
||||
if ($client->statusCode < 0) {
|
||||
throw new \Exception($client->errMsg);
|
||||
}
|
||||
|
||||
$body = $client->body;
|
||||
$client->close();
|
||||
|
||||
return $this->structure($body, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function getHostIp()
|
||||
{
|
||||
$host = \Co::getAddrInfo($this->host);
|
||||
return array_shift($host);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function getHostPort()
|
||||
{
|
||||
$port = 80;
|
||||
if ($this->isSSL) $port = 443;
|
||||
return $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $host
|
||||
* @param $port
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return Client
|
||||
*/
|
||||
private function generate_client($host, $url, $data)
|
||||
{
|
||||
$client = new Client($host, $this->getHostPort(), $this->isSSL);
|
||||
if (!empty($this->agent)) {
|
||||
$this->header['User-Agent'] = $this->agent;
|
||||
}
|
||||
if (!empty($this->header)) {
|
||||
$client->setHeaders($this->header);
|
||||
}
|
||||
if (strtolower($this->method) == self::GET) {
|
||||
$client->get($url . '?' . $data);
|
||||
} else {
|
||||
$client->post($url, $data);
|
||||
}
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
*/
|
||||
private function curl($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 (!empty($this->header)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
||||
}
|
||||
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) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
} else if ($this->method == self::POST) {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function createRequestUrl($url, $data)
|
||||
{
|
||||
if ($this->isGet()) {
|
||||
return $url . '?' . $data;
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param $body
|
||||
* @return mixed
|
||||
*/
|
||||
private function resolve($data, $body)
|
||||
{
|
||||
if (strpos('json', $data['Content-Type']) !== false) {
|
||||
return json_decode($body, true);
|
||||
} else if (strpos('xml', $data['Content-Type']) !== false) {
|
||||
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
} else if (strpos('plain', $data['Content-Type']) !== false) {
|
||||
return json_decode($data, TRUE);
|
||||
} else {
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $headers
|
||||
* @return array
|
||||
*/
|
||||
private function headerFormat($headers)
|
||||
{
|
||||
$_tmp = [];
|
||||
foreach ($headers as $key => $val) {
|
||||
$trim = explode(': ', trim($val));
|
||||
|
||||
$_tmp[$trim[0]] = $trim[1] ?? '';
|
||||
}
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $body
|
||||
* @param $_data
|
||||
* @param $header
|
||||
* @return array|mixed|Result
|
||||
* 构建返回体
|
||||
*/
|
||||
private function structure($body, $_data, $header = [])
|
||||
{
|
||||
$this->setIsSSL(false);
|
||||
$this->setHeaders([]);
|
||||
|
||||
if ($this->callback !== NULL) {
|
||||
$result = call_user_func($this->callback, $body, $_data, $header);
|
||||
$this->setCallback(null);
|
||||
|
||||
return $result;
|
||||
}
|
||||
if (!is_array($body)) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
$result['code'] = $body[$this->errorCodeField] ?? 0;
|
||||
$result['message'] = $body[$this->errorMsgField] ?? 'system success.';
|
||||
$result['data'] = $body;
|
||||
$result['header'] = $header;
|
||||
return new Result($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* check isPost Request
|
||||
*/
|
||||
public function isPost()
|
||||
{
|
||||
return strtolower($this->method) === self::POST;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*
|
||||
* check isGet Request
|
||||
*/
|
||||
public function isGet()
|
||||
{
|
||||
return strtolower($this->method) === self::GET;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $arr
|
||||
*
|
||||
* @return array|string
|
||||
* 将请求参数进行编码
|
||||
*/
|
||||
private function paramEncode($arr)
|
||||
{
|
||||
if (!is_array($arr)) {
|
||||
return $arr;
|
||||
}
|
||||
$_tmp = [];
|
||||
foreach ($arr as $Key => $val) {
|
||||
$_tmp[$Key] = $val;
|
||||
}
|
||||
if ($this->isGet()) {
|
||||
return http_build_query($_tmp);
|
||||
}
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws
|
||||
*/
|
||||
public function post($url, $data = [])
|
||||
{
|
||||
$this->setMethod(self::POST);
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws
|
||||
*/
|
||||
public function put($url, $data = [])
|
||||
{
|
||||
$this->setMethod(self::PUT);
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws
|
||||
*/
|
||||
public function get($url, $data = [])
|
||||
{
|
||||
$this->setMethod(self::GET);
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function option($url, $data = [])
|
||||
{
|
||||
$this->setMethod(self::OPTIONS);
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete($url, $data = [])
|
||||
{
|
||||
$this->setMethod(self::DELETE);
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($url, $data = [])
|
||||
{
|
||||
return $this->request($url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @return array
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
if (empty($headers)) {
|
||||
return [];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$this->header[] = $key . ':' . $val;
|
||||
}
|
||||
return $this->header;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user