Commit 3d53f0bf authored by yujiwei's avatar yujiwei

update:add new curl post

parent 0a49b637
......@@ -265,8 +265,76 @@ class Request
}
return $ch;
}
static function NEW_CURL($url, $timeout, $retries, $headers, $post = false, $proxy = false, $has_curl_file = null, $method = 'GET', $curl_opts = array())
{
$use_mon = false; //是否使用监控打点服务标志
//$proxy 取值为true,表示依赖第三方服务,要进行监控打点
if($proxy == true){
$use_mon = true;
$proxy = false;
}
$ch = self::CURL($url, $timeout, $headers, $post, $proxy, $has_curl_file, $method, $curl_opts);
$result = false;
if (is_resource($ch) === true)
{
while (($result === false) && ($retries-- > 0))
{
$result = curl_exec($ch);
}
if (curl_errno($ch) !== 0)
{
$GLOBALS['ERROR_MESSAGE'] = curl_error($ch);
}
$GLOBALS['HTTP_CODE'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if( isset($GLOBALS['ENABLE_HEADER_REQUEST_TIME']) )
{
$GLOBALS['BELENDER_CURL_INFO'] = curl_getinfo($ch);
//在返回header中添加blender响应时间
$GLOBALS['DEPENDENT-REQUEST-TIME'] = round(curl_getinfo($ch, CURLINFO_TOTAL_TIME),4).'|'.round(curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME),4).'|'.round(curl_getinfo($ch, CURLINFO_CONNECT_TIME),4);
$GLOBALS['DEPENDENT-REQUEST'] = array('url'=>$url, 'method'=>$method, 'body'=>$post);
$GLOBALS['DEPENDENT-URI'] = parse_url($url, PHP_URL_PATH);
if ($GLOBALS['HTTP_CODE'] === 0)
{
$GLOBALS['DEPENDENT-STATUS'] = 'timeout';
}
else if ($GLOBALS['HTTP_CODE'] !== 200)
{
$GLOBALS['DEPENDENT-STATUS'] = '5xx';
}else
{
$GLOBALS['DEPENDENT-STATUS'] = 'normal';
}
}
// //监控打点,模块名默认为第三方服务url,指标为请求返回的HTTP_code与总响应时间
// //HTTP_CODE如果未设置,默认为-999
// if($use_mon){
// MonUtil::proxy_mon($url,
// isset($GLOBALS['HTTP_CODE']) ? $GLOBALS['HTTP_CODE'] : -999,
// false,
// round(curl_getinfo($ch, CURLINFO_TOTAL_TIME),4) * 1000
// );
// }
unset($GLOBALS['ENABLE_HEADER_REQUEST_TIME']);
// if ($post === false)
// {
// write_log($url, curl_errno($ch), curl_error($ch), __LINE__, __FILE__, '', $result);
// }
// else
// {
// write_log($url, curl_errno($ch), curl_error($ch), __LINE__, __FILE__, 'POST body '.var_export($post, true), $result);
// }
curl_close($ch);
}
return $result;
}
public function put($url, $params = [], $timeout = 0,$content_type = '', $headers = [], $retry = 0, $use_mon = true, $proxy = '')
{
try {
......@@ -287,7 +355,7 @@ class Request
$client = (new Base())->initClient($config, $url, $params);
//构造配置
if (!empty($params)) {
if ($content_type == 'json') {
......@@ -405,6 +473,11 @@ class Request
}
}
public function CURL_POST($url, $post, $timeout = 10000, $retries = 1, $headers = false, $proxy = false, $curl_opts = array())
{
return self::NEW_CURL($url, $timeout,$retries, $headers, $post, $proxy, null, 'POST', $curl_opts);
}
/**
* 并发执行get请求
* @param array $urls [0=>xxx,1=>xxx]
......
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