时间:2021-07-01 10:21:17 帮助过:6人阅读
1、老的curl访问方式以及耗时统计
0) { $data = false; } curl_close($ch); return $data;}function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);}$url_arr=array( "taobao"=>"http://www.taobao.com", "sohu"=>"http://www.sohu.com", "lai18"=>"http://www.lai18.com",);$time_start = microtime_float();$data=array();foreach ($url_arr as $key=>$val){ $data[$key]=curl_fetch($val);}$time_end = microtime_float();$time = $time_end - $time_start;echo "耗时:{$time}";?>
$url) { $timeout=2; $ch[$nch] = curl_init(); curl_setopt_array($ch[$nch], array( CURLOPT_URL => $url, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, )); curl_multi_add_handle($mh, $ch[$nch]); ++$nch; } /* wait for performing request */ do { $mrc = curl_multi_exec($mh, $running); } while (CURLM_CALL_MULTI_PERFORM == $mrc); while ($running && $mrc == CURLM_OK) { // wait for network if (curl_multi_select($mh, 0.5) > -1) { // pull in new data; do { $mrc = curl_multi_exec($mh, $running); } while (CURLM_CALL_MULTI_PERFORM == $mrc); } } if ($mrc != CURLM_OK) { error_log("CURL Data Error"); } /* get data */ $nch = 0; foreach ($urlarr as $moudle=>$node) { if (($err = curl_error($ch[$nch])) == '') { $res[$nch]=curl_multi_getcontent($ch[$nch]); $result[$moudle]=$res[$nch]; } else { error_log("curl error"); } curl_multi_remove_handle($mh,$ch[$nch]); curl_close($ch[$nch]); ++$nch; } curl_multi_close($mh); return $result;}$url_arr=array( "taobao"=>"http://www.taobao.com", "sohu"=>"http://www.sohu.com", "lai18"=>"http://www.lai18.com", );function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);}$time_start = microtime_float();$data=curl_multi_fetch($url_arr);$time_end = microtime_float();$time = $time_end - $time_start; echo "耗时:{$time}";?>
耗时:0.316秒 帅气吧整个页面访问后端接口的时间节省了一半
http://blog.csdn.net/hello_katty/article/details/45557423