The use of curl multithreading, in addition, curl can set the request time, in the event of a very slow url resource, you can decisively give up, so there is no blocking, and there are multi-threaded requests, the efficiency should be relatively high
-
- /**
- * Curl multithreading
- * @ Param array $ array parallel URL
- * @ Param int $ 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 the 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 targeting level, top 7
- Curl_setopt ($ conn [$ k], CURLOPT_HEADER, false); // no header here, block efficiency
- Curl_setopt ($ conn [$ k], CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
- Curl_setopt ($ conn [$ k], CURLOPT_RETURNTRANSFER, 1); // The result must be a string and output to the screen.
- Curl_setopt ($ conn [$ k], CURLOPT_HTTPGET, true );
-
- Curl_multi_add_handle ($ mh, $ conn [$ k]);
- }
- // Prevent endless loop consumption of cpu. this section is based on the online statement.
- Do {
- $ Mrc = curl_multi_exec ($ mh, $ active); // when no data exists, active = true
- } While ($ mrc = CURLM_CALL_MULTI_PERFORM); // when receiving data
- While ($ active and $ mrc = CURLM_ OK) {// when there is no data or 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]); // convert data to array
- $ Header [$ k] = curl_getinfo ($ conn [$ k]); // return http header information
- Curl_close ($ conn [$ k]); // Close the slogan.
- Curl_multi_remove_handle ($ mh, $ conn [$ k]); // release resources
- } Else {
- Unset ($ k, $ url );
- }
- }
-
- Curl_multi_close ($ mh );
-
- Return $ data;
-
- }
-
- // Receive parameters
- $ Callback = $ _ GET ['callback'];
- $ Hrefs = $ _ GET ['hrefs'];
- $ Urlarray = explode (',', trim ($ hrefs ,','));
- $ Date = date ('ymmd', 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'); // binary of all image data
- $ J = 0;
- Foreach (array) $ dataimg as $ kk => $ vv ){
- If ($ vv! = ''){
- $ Rand = rand );
- $ Basename = time (). "_". $ rand. ".". jpg; // save as a jpg file
- $ Fname = './img/'. $ date. "/". "$ basename ";
- File_put_contents ($ fname, $ vv );
- $ J ++;
- Echo "create the". $ j. "Picture". "$ fname "."
";
- } Else {
- Unset ($ kk, $ vv );
- }
- }
- } Else {
- Unset ($ matches );
- }
- }
- $ Etime = $ img-> getMicrotime (); // end time
- Echo "time used". ($ etime-$ stime). "seconds ";
- Exit;
-
|