Use Curl multithreading, in addition curl can set the request time, encountered a very slow URL resources, can decisively give up, so there is no blocking, there are multiple threads request, efficiency should be higher
Here is the code implementation:
- /**
- * Curl Multithreading
- * @author http://www.lai18.com
- * @param array $array parallel URLs
- * @param int $timeout timeout
- * @return Mix
- */
- Public Function Curl_http ($array, $timeout = ' 15 ') {
- $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 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 orientation level, 7 highest
- curl_setopt ($conn [$k], Curlopt_header, false);/No 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]);
- }
- Preventing dead loops from dying the CPU this paragraph is based on the online wording
- do {
- $MRC = Curl_multi_exec ($MH, $active);//When there is 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 when the 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_multi_getcontent ($conn [$k]);//data 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]); Releasing 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 of
- $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 ("/(HREFSRC) = (["]?) ([^ "' >]+.] (jpgpngpngjpggif)) \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 the first". $j. " Picture "." $fname "." <br/> ";
- }else{
- Unset ($kk, $VV);
- }
- }
- }else{
- Unset ($matches);
- }
- }
- $etime = $img->getmicrotime ()/End time
- echo "Spents". ($etime-$stime). " Seconds ";
- Exit