curl

/**
* curl提交数据
* @param $url 服务访问地址
* @param $data 提交数据 array()
* @param $httpHeader 设置host主机头
* @param string $method
* @return mixed
*/
function invoke_curl($url, $data, $method = 'POST', $httpHeader = array(), $timeout = 300)
{
$ch = curl_init();
$timeout = $timeout <= 0 ? 300 : $timeout;
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

if(strtoupper($method) == 'POST') {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {//GET
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data));
}
//$httpHeader = array('Host:127.0.0.1');
if(!empty($httpHeader) && is_array($httpHeader)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$handles = curl_exec($ch);
//$error = curl_error($ch);//排除错误
curl_close($ch);
return $handles;
}

原文地址:https://www.cnblogs.com/nowphp/p/6526356.html