更新 Http.php
根据本地文件修改
This commit is contained in:
+138
-54
@@ -2,75 +2,101 @@
|
|||||||
|
|
||||||
namespace wchat;
|
namespace wchat;
|
||||||
|
|
||||||
|
use Swoole\Coroutine\Http\Client;
|
||||||
|
|
||||||
class Http
|
class Http
|
||||||
{
|
{
|
||||||
private $url = 'https://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
|
||||||
* @return Result
|
* @param bool $isSSL
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @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://') === FALSE &&
|
strpos($url, 'http://') === 0 ||
|
||||||
strpos($url, 'https://') === FALSE
|
strpos($url, 'https://') === 0
|
||||||
) {
|
) {
|
||||||
$url = $this->url . '/' . $url;
|
return $this->curl($url, $pushType, $data, $callback, $isSSL);
|
||||||
|
}
|
||||||
|
if (!extension_loaded('swoole')) {
|
||||||
|
return $this->curl($url, $pushType, $data, $callback, $isSSL);
|
||||||
|
} else {
|
||||||
|
$url = 'https://' . $this->url . '/' . $url;
|
||||||
|
return $this->coroutine($url, $pushType, $data, $callback, $isSSL);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->curl_push($url, $pushType, $data, $callback, $isSSL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
* @throws
|
||||||
* @return Result
|
* @return Result
|
||||||
* curl请求
|
* 使用swoole协程方式请求
|
||||||
*/
|
*/
|
||||||
public function curl_push($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
|
private function coroutine($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
|
||||||
{
|
{
|
||||||
$_data = $this->paramEncode($data);
|
$_data = $this->paramEncode($data);
|
||||||
if ($type == 'get' && is_array($_data)) {
|
if ($type == 'get' && is_array($_data)) {
|
||||||
$url .= '?' . http_build_query($_data);
|
$url .= '?' . http_build_query($_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
$host = \Co::getAddrInfo($this->url);
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
$clientInfo = [array_shift($host), 443, TRUE];
|
||||||
curl_setopt($ch, CURLOPT_HEADER, TRUE);
|
|
||||||
if (!empty($this->header)) {
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
|
||||||
}
|
|
||||||
if ($isSSL && is_array($isSSL)) {
|
if ($isSSL && is_array($isSSL)) {
|
||||||
curl_setopt($ch, CURLOPT_SSLCERT, $isSSL[0]);
|
$this->header['ssl_cert_file'] = $isSSL[0];
|
||||||
curl_setopt($ch, CURLOPT_SSLKEY, $isSSL[1]);
|
$this->header['ssl_key_file'] = $isSSL[1];
|
||||||
}
|
}
|
||||||
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);// 超时设置
|
$cli = new Client(...$clientInfo);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
if (!empty($this->header)) {
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
|
$cli->setHeaders($this->header);
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
}
|
||||||
|
strtolower($type) == 'get' ? $cli->get($url) : $cli->post($url, $data);
|
||||||
|
|
||||||
|
if ($cli->statusCode < 0) {
|
||||||
|
throw new \Exception('连接错误!');
|
||||||
|
}
|
||||||
|
$body = $cli->body;
|
||||||
|
$cli->close();
|
||||||
|
return $this->build($body, $callback, $_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);
|
||||||
|
|
||||||
|
if ($type == 'get') $url .= '?' . $_data;
|
||||||
|
|
||||||
|
$ch = $this->buildCurl($url, $isSSL);
|
||||||
switch (strtolower($type)) {
|
switch (strtolower($type)) {
|
||||||
case 'post':
|
case 'post':
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
if (is_array($_data)) {
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_data));
|
|
||||||
} else {
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'delete':
|
case 'delete':
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_data));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $_data);
|
||||||
break;
|
break;
|
||||||
case 'put':
|
case 'put':
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||||
@@ -85,11 +111,46 @@ class Http
|
|||||||
}
|
}
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
list($header, $body) = explode("\r\n\r\n", $output, 2);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function build($body, $callback, $_data)
|
||||||
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
if ($callback === NULL) {
|
if ($callback === NULL) {
|
||||||
if (is_null($results = json_decode($body, TRUE))) {
|
if (is_null($results = json_decode($body, TRUE))) {
|
||||||
return new Result(['code' => 500, 'message' => '服务器连接失败']);
|
$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'])) {
|
if (isset($results['errcode'])) {
|
||||||
$result['code'] = $results['errcode'];
|
$result['code'] = $results['errcode'];
|
||||||
@@ -102,11 +163,9 @@ class Http
|
|||||||
} else {
|
} else {
|
||||||
$result = call_user_func($callback, $body, $_data);
|
$result = call_user_func($callback, $body, $_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_array($result)) {
|
if (!is_array($result)) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Result($result);
|
return new Result($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,65 +190,90 @@ class Http
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param array $header
|
* @param callable|NULL $callback
|
||||||
*
|
* @param array|NULL $header
|
||||||
* @return Result
|
* @param bool $isSSl
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws
|
||||||
*/
|
*/
|
||||||
public static function post($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE)
|
public static function post($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)) {
|
if (!empty($header)) $_class->setHeaders($header);
|
||||||
$_class->setHeaders($header);
|
|
||||||
}
|
|
||||||
return $_class->request($url, 'post', $data, $callback, $isSSl);
|
return $_class->request($url, 'post', $data, $callback, $isSSl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
* @param callable|NULL $callback
|
||||||
|
* @param array|NULL $header
|
||||||
|
* @param bool $isSSl
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public static function put($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE)
|
||||||
|
{
|
||||||
|
static $_class = NULL;
|
||||||
|
if ($_class == NULL) $_class = new Http();
|
||||||
|
if (!empty($header)) $_class->setHeaders($header);
|
||||||
|
return $_class->request($url, 'put', $data, $callback, $isSSl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @param callable|NULL $callback
|
||||||
* @param array $header
|
* @param array $header
|
||||||
*
|
* @return array|mixed|Result
|
||||||
* @return Result
|
* @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);
|
||||||
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 Result
|
* @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);
|
||||||
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 Result
|
* @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);
|
||||||
return $_class->request($url, 'delete', $data);
|
return $_class->request($url, 'delete', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $headers
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function setHeaders(array $headers)
|
public function setHeaders(array $headers)
|
||||||
{
|
{
|
||||||
if (empty($headers)) {
|
if (empty($headers)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user