add clear

This commit is contained in:
as2252258@163.com
2019-07-12 18:44:54 +08:00
parent c6e9b9a34d
commit cac575718b
+253 -249
View File
@@ -6,279 +6,283 @@ use Swoole\Coroutine\Http\Client;
class Http class Http
{ {
private $url = 'api.weixin.qq.com'; private $url = 'api.weixin.qq.com';
private $header = []; private $header = [];
/** /**
* @param $url * @param $url
* @param string $pushType * @param string $pushType
* @param array $data * @param array $data
* @param callable|NULL $callback * @param callable|NULL $callback
* @param bool $isSSL * @param bool $isSSL
* @return array|mixed|Result * @return array|mixed|Result
* @throws \Exception * @throws \Exception
*/ */
private function request($url, $pushType = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE) private function request($url, $pushType = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
{ {
if ( if (
strpos($url, 'http://') === 0 || strpos($url, 'http://') === 0 ||
strpos($url, 'https://') === 0 strpos($url, 'https://') === 0
) { ) {
return $this->curl($url, $pushType, $data, $callback, $isSSL); return $this->curl($url, $pushType, $data, $callback, $isSSL);
} }
$url = 'https://' . $this->url . '/' . $url; if (function_exists('getIsCli') && getIsCli()) {
return $this->coroutine($url, $pushType, $data, $callback, $isSSL);
}
return $this->curl($url, $pushType, $data, $callback, $isSSL); $url = 'https://' . $this->url . '/' . $url;
}
/** return $this->curl($url, $pushType, $data, $callback, $isSSL);
* @param $url }
* @param string $type
* @param array $data
* @throws
* @return Result
* 使用swoole协程方式请求
*/
private function coroutine($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
{
$_data = $this->paramEncode($data);
if ($type == 'get' && is_array($_data)) {
$url .= '?' . http_build_query($_data);
}
$host = \Co::getAddrInfo($this->url); /**
* @param $url
* @param string $type
* @param array $data
* @return Result
* 使用swoole协程方式请求
* @throws
*/
private function coroutine($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
{
$_data = $this->paramEncode($data);
if ($type == 'get' && is_array($_data)) {
$url .= '?' . http_build_query($_data);
}
$clientInfo = [array_shift($host), 443, TRUE]; $host = \Co::getAddrInfo($this->url);
if ($isSSL && is_array($isSSL)) { $clientInfo = [array_shift($host), 443, TRUE];
$this->header['ssl_cert_file'] = $isSSL[0];
$this->header['ssl_key_file'] = $isSSL[1];
}
$cli = new Client(...$clientInfo); if ($isSSL && is_array($isSSL)) {
if (!empty($this->header)) { $this->header['ssl_cert_file'] = $isSSL[0];
$cli->setHeaders($this->header); $this->header['ssl_key_file'] = $isSSL[1];
} }
strtolower($type) == 'get' ? $cli->get($url) : $cli->post($url, $data);
if ($cli->statusCode < 0) { $cli = new Client(...$clientInfo);
throw new \Exception('连接错误!'); if (!empty($this->header)) {
} $cli->setHeaders($this->header);
$body = $cli->body; }
$cli->close(); strtolower($type) == 'get' ? $cli->get($url) : $cli->post($url, $data);
return $this->build($body, $callback, $_data);
}
/** if ($cli->statusCode < 0) {
* @param $url throw new \Exception('连接错误!');
* @param string $type }
* @param array $data $body = $cli->body;
* @param callable|NULL $callback $cli->close();
* @param bool $isSSL return $this->build($body, $callback, $_data);
* @return array|mixed|Result }
*/
private function curl($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
{
$_data = $this->paramEncode($data);
if (is_array($_data)) $_data = http_build_query($_data);
if ($type == 'get') $url .= '?' . $_data; /**
* @param $url
* @param string $type
* @param array $data
* @param callable|NULL $callback
* @param bool $isSSL
* @return array|mixed|Result
*/
private function curl($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
{
$_data = $this->paramEncode($data);
if (is_array($_data)) $_data = http_build_query($_data);
$ch = $this->buildCurl($url, $isSSL); if ($type == 'get') $url .= '?' . $_data;
switch (strtolower($type)) {
case 'post':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
$output = curl_exec($ch);
if ($output === FALSE) {
return new Result(['code' => 500, 'message' => curl_error($ch)]);
}
curl_close($ch);
return $this->build($output, $callback, $_data); $ch = $this->buildCurl($url, $isSSL);
} switch (strtolower($type)) {
case 'post':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
$output = curl_exec($ch);
if ($output === FALSE) {
return new Result(['code' => 500, 'message' => curl_error($ch)]);
}
curl_close($ch);
/** return $this->build($output, $callback, $_data);
* @param $url }
* @param $isSSL
* @return resource
*/
private function buildCurl($url, $isSSL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, FALSE);
if (!empty($this->header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
}
if ($isSSL && is_array($isSSL)) {
curl_setopt($ch, CURLOPT_SSLCERT, $isSSL[0]);
curl_setopt($ch, CURLOPT_SSLKEY, $isSSL[1]);
}
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');
return $ch; /**
} * @param $url
* @param $isSSL
* @return resource
*/
private function buildCurl($url, $isSSL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, FALSE);
if (!empty($this->header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
}
if ($isSSL && is_array($isSSL)) {
curl_setopt($ch, CURLOPT_SSLCERT, $isSSL[0]);
curl_setopt($ch, CURLOPT_SSLKEY, $isSSL[1]);
}
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');
private function build($body, $callback, $_data) return $ch;
{ }
$result = [];
if ($callback !== NULL) {
return call_user_func($callback, $body, $_data);
}
if (is_null($results = json_decode($body, TRUE))) {
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
$results = json_decode(json_encode($data), TRUE);
}
if (!is_array($results)) {
return new Result(['code' => 505, 'message' => '服务器返回体错误!']);
}
if (isset($results['errcode'])) {
$result['code'] = $results['errcode'];
$result['message'] = $results['errmsg'];
} else {
$result['code'] = 0;
$result['message'] = 'system success.';
$result['data'] = $results;
}
if (!is_array($result)) {
return $result;
}
return new Result($result);
}
/** private function build($body, $callback, $_data)
* @param $arr {
* @param string $pushType $result = [];
* if ($callback !== NULL) {
* @return array|string return call_user_func($callback, $body, $_data);
* 将请求参数进行编码 }
*/ if (is_null($results = json_decode($body, TRUE))) {
private function paramEncode($arr, $pushType = 'post') $data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
{ $results = json_decode(json_encode($data), TRUE);
if (!is_array($arr)) { }
return $arr; if (!is_array($results)) {
} return new Result(['code' => 505, 'message' => '服务器返回体错误!']);
$_tmp = []; }
foreach ($arr as $Key => $val) { if (isset($results['errcode'])) {
$_tmp[$Key] = $val; $result['code'] = $results['errcode'];
} $result['message'] = $results['errmsg'];
} else {
$result['code'] = 0;
$result['message'] = 'system success.';
$result['data'] = $results;
}
if (!is_array($result)) {
return $result;
}
return new Result($result);
}
return ($pushType == 'post' ? $_tmp : http_build_query($_tmp)); /**
} * @param $arr
* @param string $pushType
*
* @return array|string
* 将请求参数进行编码
*/
private function paramEncode($arr, $pushType = 'post')
{
if (!is_array($arr)) {
return $arr;
}
$_tmp = [];
foreach ($arr as $Key => $val) {
$_tmp[$Key] = $val;
}
/** return ($pushType == 'post' ? $_tmp : http_build_query($_tmp));
* @param $url }
* @param array $data
* @param callable|NULL $callback /**
* @param array|NULL $header * @param $url
* @param bool $isSSl * @param array $data
* @return array|mixed|Result * @param callable|NULL $callback
* @throws * @param array|NULL $header
*/ * @param bool $isSSl
public static function post($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE) * @return array|mixed|Result
{ * @throws
static $_class = NULL; */
if ($_class == NULL) $_class = new Http(); public static function post($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE)
if (!empty($header)) $_class->setHeaders($header); {
return $_class->request($url, 'post', $data, $callback, $isSSl); static $_class = NULL;
} if ($_class == NULL) $_class = new Http();
if (!empty($header)) $_class->setHeaders($header);
return $_class->request($url, 'post', $data, $callback, $isSSl);
}
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @param callable|NULL $callback * @param callable|NULL $callback
* @param array|NULL $header * @param array|NULL $header
* @param bool $isSSl * @param bool $isSSl
* @return array|mixed|Result * @return array|mixed|Result
* @throws * @throws
*/ */
public static function put($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE) public static function put($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE)
{ {
static $_class = NULL; static $_class = NULL;
if ($_class == NULL) $_class = new Http(); if ($_class == NULL) $_class = new Http();
if (!empty($header)) $_class->setHeaders($header); if (!empty($header)) $_class->setHeaders($header);
return $_class->request($url, 'put', $data, $callback, $isSSl); return $_class->request($url, 'put', $data, $callback, $isSSl);
} }
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @param callable|NULL $callback * @param callable|NULL $callback
* @param array $header * @param array $header
* @return array|mixed|Result * @return array|mixed|Result
* @throws * @throws
*/ */
public static function get($url, $data = [], callable $callback = NULL, $header = []) public static function get($url, $data = [], callable $callback = NULL, $header = [])
{ {
static $_class = NULL; static $_class = NULL;
if ($_class == NULL) $_class = new Http(); if ($_class == NULL) $_class = new Http();
if (!empty($header)) $_class->setHeaders($header); if (!empty($header)) $_class->setHeaders($header);
return $_class->request($url, 'get', $data, $callback); return $_class->request($url, 'get', $data, $callback);
} }
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @param array $header * @param array $header
* @return array|mixed|Result * @return array|mixed|Result
* @throws \Exception * @throws \Exception
*/ */
public static function option($url, $data = [], $header = []) public static function option($url, $data = [], $header = [])
{ {
static $_class = NULL; static $_class = NULL;
if ($_class == NULL) $_class = new Http(); if ($_class == NULL) $_class = new Http();
if (!empty($header)) $_class->setHeaders($header); if (!empty($header)) $_class->setHeaders($header);
return $_class->request($url, 'option', $data); return $_class->request($url, 'option', $data);
} }
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @param array $header * @param array $header
* @return array|mixed|Result * @return array|mixed|Result
* @throws \Exception * @throws \Exception
*/ */
public static function delete($url, $data = [], $header = []) public static function delete($url, $data = [], $header = [])
{ {
static $_class = NULL; static $_class = NULL;
if ($_class == NULL) $_class = new Http(); if ($_class == NULL) $_class = new Http();
if (!empty($header)) $_class->setHeaders($header); if (!empty($header)) $_class->setHeaders($header);
return $_class->request($url, 'delete', $data); return $_class->request($url, 'delete', $data);
} }
/** /**
* @param array $headers * @param array $headers
* @return array * @return array
*/ */
public function setHeaders(array $headers) public function setHeaders(array $headers)
{ {
if (empty($headers)) { if (empty($headers)) {
return []; return [];
} }
foreach ($headers as $key => $val) { foreach ($headers as $key => $val) {
$this->header[] = $key . ':' . $val; $this->header[] = $key . ':' . $val;
} }
return $this->header; return $this->header;
} }
} }