本文共 1802 字,大约阅读时间需要 6 分钟。
- function curl_multi($urls) {
- if (!is_array($urls) or count($urls) == 0) {
- return false;
- }
- $num=count($urls);
- $curl = $curl2 = $text = array();
- $handle = curl_multi_init();
- function createCh($url) {
- $ch = curl_init();
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko');
- curl_setopt ($ch, CURLOPT_REFERER, $url);
- curl_setopt ($ch, CURLOPT_ENCODING, "gzip");
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt ($ch, CURLOPT_MAXREDIRS, 5);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
- curl_setopt ($ch, CURLOPT_HEADER, 0);
- return $ch;
- }
- foreach($urls as $k=>$v){
- $url=$urls[$k];
- $curl[$k] = createCh($url);
- curl_multi_add_handle ($handle,$curl[$k]);
- }
- $active = null;
- do {
- $mrc = curl_multi_exec($handle, $active);
- } while ($mrc == CURLM_CALL_MULTI_PERFORM);
-
- while ($active && $mrc == CURLM_OK) {
- if (curl_multi_select($handle) != -1) {
- usleep(100);
- }
- do {
- $mrc = curl_multi_exec($handle, $active);
- } while ($mrc == CURLM_CALL_MULTI_PERFORM);
- }
-
- foreach ($curl as $k => $v) {
- if (curl_error($curl[$k]) == "") {
- $text[$k] = (string) curl_multi_getcontent($curl[$k]);
- }
- curl_multi_remove_handle($handle, $curl[$k]);
- curl_close($curl[$k]);
- }
- curl_multi_close($handle);
- return $text;
- }
- $urls=array('http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com',
- 'http://www.baidu.com'
- );
- $res=curl_multi($urls);
- print_r($res);
转载于:https://www.cnblogs.com/wenxinphp/p/8556841.html