你还在用file_get_contents()吗
main.php 代码: <?php $url = 'http://www.baidu.com'; $time_total = 0; for($i = 0; $i < 100; $i++){ $time_start = get_time(); get_html($url); $time_end = get_time(); $time_spent = $time_end - $time_start; $time_total += $time_spent; echo "counter:{$i} time:{$time_spent}\n"; usleep(200000);// 0.2 sec } echo "Total time:{$time_total}\n"; function get_time(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } 第一个get_html()实现 代码: function get_html($url){ return file_get_contents($url); } 测试情况.
第二个get_html()实现
代码:
function get_html($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);$html = curl_exec($ch); curl_close($ch);
return $html; }
测试情况