测试循环12个URL 类似采集请求的结果
CURL: total 5.11485290527 s
MULTI CURL: total 0.369098901749 s
FILE GET CONTENTS: total 9.39328789711 s
file_get_content函数最慢,CURL次之 在可以忍受的范围类
不得不说下CURL的多线程并发请求 并行请求队列
相当于请求1个url的时间 并不是队列方式的请求
测试脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php $data = array( 'http://www.marca.com', 'http://www.wowebook.be', 'http://www.meneame.net', 'http://www.google.com', 'http://www.elpais.es', 'http://www.reddit.com', 'http://www.digg.com', 'http://www.elmundo.es', 'http://www.itbooks.com', 'http://www.as.com', 'http://www.sport.es', 'http://www.elmundodeportivo.es' ); /*******************************************************/ /************************* file_get_contents ************************/ /*******************************************************/ /* $first = microtime(true); foreach ($data as $url1) { $file = file_get_contents($url1); //$ch = curl_init($url); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //$content[] = curl_exec($ch); //curl_close($ch); } $total = microtime(true) - $first; echo "File GET Content: total " . $total . " s"; */ echo "<br>"; /*******************************************************/ /************************* CURL ************************/ /*******************************************************/ $first = microtime(true); foreach ($data as $url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $content[] = curl_exec($ch); curl_close($ch); } $total = microtime(true) - $first; echo "CURL: total " . $total . " s"; echo "<br>"; /*******************************************************/ /********************** MULTI CURL *********************/ /*******************************************************/ $first = microtime(true); // array of curl handles $curly = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); foreach ($data as $id => $d) { $curly[$id] = curl_init(); curl_setopt($curly[$id], CURLOPT_URL, $url); curl_setopt($curly[$id], CURLOPT_HEADER, 0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST, 1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); } } curl_multi_add_handle($mh, $curly[$id]); } // execute the handles $running = null; do { curl_multi_exec($mh, $running); } while($running > 0); // get content and remove handles foreach($curly as $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } curl_multi_close($mh); $total = microtime(true) - $first; echo "MULTI CURL: total " . $total . ' s'; echo "<br>"; /*******************************************************/ /****************** FILE GET CONTENTS ******************/ /*******************************************************/ $first = microtime(true); foreach ($data as $url) { $content[] = file_get_contents($url); } $total = microtime(true) - $first; echo "FILE GET CONTENTS: total " . $total . " s"; ?> |