function http($callbackUrl, $dataStr = '', $isPost = 0, $isSkipSSL = 0, $retries = 1, $http_v = 0, $con_time_out=3, $ex_time_out=3) {
$httpInfo = array ();
$httpCode = 0;
$ch = curl_init ();
if($http_v == 0){
curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
}else if($http_v == 1){
curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
}else{
curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE );
}
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $con_time_out );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $ex_time_out );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
if ($isSkipSSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 有的https,跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if ($isPost) {
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $dataStr );
curl_setopt ( $ch, CURLOPT_URL, $callbackUrl );
} else {
curl_setopt ( $ch, CURLOPT_URL, $callbackUrl . '?' . $dataStr );
}
if(is_array($dataStr)){
$dataStr = http_build_query($dataStr);
}
$response = false;
$reqnum = 1;
while( $response === false && $retries-- > 0 ){
$response = curl_exec ( $ch );
$reqnum++;
}
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
$httpInfo = array_merge ( $httpInfo, curl_getinfo ( $ch ) );
if (curl_errno ( $ch )) {
Logger::warning ( 'curl err info : %s', curl_error ( $ch ) );
throw new Exception ( 'fake' );
}
curl_close ( $ch );
return $response;
}05
2019
07
简单的http请求方法
作者:小布 | 分类:PHP | 浏览:863