PHP multi-threaded Batch collection download beautiful pictures of the implementation code
Multi-Threading using curl, in addition to curl can set the request time, encountered a very slow URL resources, can be determined to give up, so that there is no blocking, in addition to multi-threaded requests, the efficiency should be higher
Here is the code implementation
/** * Curl Multithreading * @author http://www.lai18.com * @param array $array parallel URLs * @param int $timeout time-out * @return Mix */Public Function curl_http ($array, $timeout = ' + ') {$res = array (); $MH = Curl_multi_init ();//Create Multiple Curl handles foreach ($array as $k = = $url) {$conn [$k]=curl_init ($url);//initialization curl_setopt ($conn [$k], curlopt_timeout, $timeout);//Set time-out curl_setopt ($conn [$k], curlopt_useragent, ' MOZILLA/5 .0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($conn [$k], Curlopt_maxredirs, 7),//http directional level, 7 maximum curl_setopt ($conn [$k], Curlopt_header, false);//Not here Header, add block efficiency curl_setopt ($conn [$k], curlopt_followlocation, 1); 302 redirect Curl_setopt ($conn [$k], curlopt_returntransfer,1);//requires the result to be a string and output to the on-screen curl_setopt ($conn [$k], Curlopt_httpget, true); Curl_multi_add_handle ($MH, $conn [$k]); }//Prevent death cycle consumes the CPU this paragraph is based on the online notation do {$MRC = Curl_multi_exec ($MH, $active);//When no data, active=true} while ($MRC = = Curlm_call_multi_perform);//While data is being accepted while ($active and $MRC = = CURLM_OK) {//when no data or request is paused, active=true if (Curl_multi_select ($MH)! =-1) {do { $MRC = Curl_multi_exec ($MH, $active); } while ($MRC = = Curlm_call_multi_perform); }} foreach ($array as $k = + $url) {if (!curl_errno ($conn [$k])) {$data [$k]=curl_mu Lti_getcontent ($conn [$k]);//data is converted to array $header [$k]=curl_getinfo ($conn [$k]);//return HTTP header information Curl_close ( $conn [$k]);//close handle Curl_multi_remove_handle ($MH, $conn [$k]); Release Resources}else{unset ($k, $url); }} curl_multi_close ($MH); return $data; }//parameter Receive $callback = $_get[' callback ']; $hrefs = $_get[' HREFs ']; $urlarray = explode (', ', trim ($hrefs, ', ')); $date = Date (' Ymd ', Time ());//Instantiate $img = new httpimg (); $stime = $img-> Getmicrotime ();//start time $data = $img->curl_http ($urlarray, ' 20 ');//List Data mkdir ('./img/'. $date, 0777); foreach ((Array ) $data as $k + $v) {Preg_match_all ("/(HREF|SRC) = ([" | ']?) ([^ "' >]+. (jpg|png| Png| Jpg|gif) \2/i ", $v, $matches [$k]); if (count ($matches [$k][3]) >0) {$dataimg = $img->curl_http ($matches [$k][3], ' 20 ');//All picture data binary $j = 0; foreach (array) $dataimg as $kk + = $VV) {if ($VV! = ") {$rand = rand (1000,9999); $basename = Time (). " _ ". $rand.". jpg;//files saved in jpg format $fname = './img/'. $date. " /"." $basename "; File_put_contents ($fname, $VV); $j + +; echo "Create section". $j. " Photo "." $fname "."
"; }else{unset ($KK, $VV); }}}else{unset ($matches);}} $etime = $img->getmicrotime ();//End time echo "Spents". ($etime-$stime). " Seconds "; exit;