3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //CURL获得远程内容 function getCurlContent($geturl='',$http_code='',$timeout=600,$connect_timout=100,$method='',$data=array()){ if(empty($geturl)){ return ""; } if($data){ $data = http_build_query($data); } $start= getMicrotime(); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $geturl); curl_setopt($curl, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个 curl_setopt($curl, 156, $connect_timout); //尝试连接等待的时间, CURLOPT_TIMEOUT_MS => 155 , CURLOPT_CONNECTTIMEOUT_MS => 156 curl_setopt($curl, 155, $timeout); //设置cURL允许执行的最长毫秒数 curl_setopt($curl, CURLOPT_USERAGENT, _USERAGENT_); if($method == 'post' ){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 } curl_setopt($curl, CURLOPT_REFERER,_REFERER_); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); $curl_errno = curl_errno($curl); $curl_errstr = curl_error($curl); curl_close($curl); $end = getMicrotime(); $host = parse_url ( $geturl , PHP_URL_HOST ); $curl_time = $end-$start; writeAmonitorLog($host,$curl_time); if($result === false){ $err_line = $host."_".$geturl."_".'curl-err:'.$curl_errno.'['.$curl_errstr.']'."_".date("Y-m-d H:i:s")."\n"; file_put_contents("/tmp/502_log_".date("md").".txt",$err_line,FILE_APPEND); $result = ''; if($http_code == '502' && $curl_errno == 28){ header("HTTP/1.1 502 Bad Gateway"); //请求超时响应502 echo '502 Bad Gateway!'; exit; } } if('shenma'==$_GET['debug']){ var_dump("curl_time:",$curl_time,$geturl,urldecode($geturl)); } return $result; }
Output for 4.3.0 - 7.1.0