Commit bcba6f64 authored by mengweifu's avatar mengweifu

update: HttpUtil

parent a69c1304
...@@ -25,6 +25,8 @@ use Api\PhpUtils\Mon\MonUtil; ...@@ -25,6 +25,8 @@ use Api\PhpUtils\Mon\MonUtil;
class HttpUtil class HttpUtil
{ {
private const CODE_SUCCESS = 0;
private const CODE_FAIL = 999999;
static function HEAD($url, $timeout = 10000, $retries = 1, $headers = false, $proxy = false) static function HEAD($url, $timeout = 10000, $retries = 1, $headers = false, $proxy = false)
{ {
...@@ -53,7 +55,12 @@ class HttpUtil ...@@ -53,7 +55,12 @@ class HttpUtil
{ {
$ch = self::CH($url, $timeout, $headers, $post, $proxy, $method, $curl_opts); $ch = self::CH($url, $timeout, $headers, $post, $proxy, $method, $curl_opts);
$result = false;
$result = ''; //返回结果
$result_code = self::CODE_FAIL; //错误代码
$result_http_code = 0; // http code
$error_msg = ''; // ch error
if (is_resource($ch) === true) if (is_resource($ch) === true)
{ {
...@@ -61,6 +68,8 @@ class HttpUtil ...@@ -61,6 +68,8 @@ class HttpUtil
{ {
$result = curl_exec($ch); $result = curl_exec($ch);
} }
// { todo
if (curl_errno($ch) !== 0) if (curl_errno($ch) !== 0)
{ {
$GLOBALS['ERROR_MESSAGE'] = curl_error($ch); $GLOBALS['ERROR_MESSAGE'] = curl_error($ch);
...@@ -85,20 +94,34 @@ class HttpUtil ...@@ -85,20 +94,34 @@ class HttpUtil
$GLOBALS['DEPENDENT-STATUS'] = 'normal'; $GLOBALS['DEPENDENT-STATUS'] = 'normal';
} }
} }
unset($GLOBALS['ENABLE_HEADER_REQUEST_TIME']); unset($GLOBALS['ENABLE_HEADER_REQUEST_TIME']);
// }
// 监控
self::addMon( self::addMon(
$url, $url,
$GLOBALS['HTTP_CODE'] ?? -999, $GLOBALS['HTTP_CODE'] ?? -999,
round(curl_getinfo($ch, CURLINFO_TOTAL_TIME),4) * 1000 round(curl_getinfo($ch, CURLINFO_TOTAL_TIME),4) * 1000
); );
// 日志
self::addLog($method, $url, $post, $result); self::addLog($method, $url, $post, $result);
if (curl_errno($ch) !== 0)
{
$error_msg = curl_error($ch);
}
$result_code = curl_errno($ch) === 0 ? self::CODE_SUCCESS : self::CODE_FAIL;
$result_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
} }
return $result; return [
'code' => $result_code,
'response' => empty(json_decode($result, true)) ? $result : json_decode($result, true),
'http_code' => $result_http_code,
'msg' => $error_msg,
];
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment