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); //Initialize
- curl_setopt ($conn[$k], curlopt_timeout, $timeout); //Set timeout time
- 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); //Do not header here, 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 screen
- curl_setopt ($conn[$k], curlopt_httpget, true);
- Curl_multi_add_handle ($mh,$conn[$k]);
- }
- //Prevent death cycle consumption of 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); //When data is being accepted
- while ( $active and $mrc = = CURLM_OK) {// When there is no data or a request is paused, the 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_multi_getcontent ($conn[$k]); //Data converted to array
- $header [$k]=curl_getinfo ($conn[$k]); //Return HTTP header information
- Curl_close ($conn[$k]); //Close the 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 ());
- //Instantiation
- $img = New httpimg ();
- $stime = $img ->getmicrotime (); //Start time
- $data = $img ->curl_http ($urlarray,'a '); //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], ' A'); //Full picture data binary
- $j = 0;
- foreach (array) $dataimg as $kk+= $vv) {
- if ($vv ! =") {
- $rand = rand (1000,9999);
- $basename = Time (). "_" . $rand . "." . jpg; //save files in jpg format
- $fname = './img/' . $date . "/" . "$basename" ;
- file_put_contents ($fname, $vv);
- $j ++;
- Echo "Create a section". $j . "Picture" . "$fname" . "
";
- }Else{
- unset ($kk,$vv);
- }
- }
- }Else{
- unset ($matches);
- }
- }
- $etime = $img ->getmicrotime (); //End time
- Echo "time." ( $etime - $stime ). "Seconds" ;
- Exit ;
http://www.bkjia.com/PHPjc/985647.html www.bkjia.com true http://www.bkjia.com/PHPjc/985647.html techarticle PHP multi-threaded Batch collection download beautiful pictures of the implementation of the code using Curl Multi-threading, in addition curl can set the request time, encountered a very slow URL resources, can be decisive to give up, so no ...