curl多线程抓取

php代码实现如下:

            $mh = curl_multi_init();
            $conn = array();
            $urls = array(); 
            foreach($material_list as $k=>$vv)
            {
                $urls[$k] = $this->CI->config->item('adving_period_url').'&sdate='.$sd.'&edate='.$ed.'&aid='.$aid.'&mid='.$vv['material_id'].'&ptid='.$ad_row['partner_id'];
                //echo $urls[$k].'<br/>';
                $conn[$k] = curl_init($urls[$k]);
                curl_setopt($conn[$k], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");  
                curl_setopt($conn[$k], CURLOPT_HEADER ,0);  
                curl_setopt($conn[$k], CURLOPT_CONNECTTIMEOUT,60);  
                curl_setopt($conn[$k],CURLOPT_RETURNTRANSFER,true);
                curl_multi_add_handle ($mh,$conn[$k]);    
            }
            do 
            {  
                curl_multi_exec($mh,$active);  
            } while ($active);
            
            foreach($material_list as $k=>$vv)
            {  
                $return_result = json_decode(curl_multi_getcontent($conn[$k]), true);
            }
            foreach($material_list as $k=>$vv)
            {  
                curl_multi_remove_handle($mh,$conn[$k]);  
                curl_close($conn[$k]);
            }  
            curl_multi_close($mh);


你可能感兴趣的:(多线程,PHP,curl)