PHP multi-thread batch collection and downloading image implementation code

Source: Internet
Author: User
PHP multi-thread batch collection and downloading image implementation code
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

  1. /**
  2. * Curl multithreading
  3. * @ Param array $ array parallel URL
  4. * @ Param int $ timeout
  5. * @ Return mix
  6. */
  7. Public function Curl_http ($ array, $ timeout = '15 '){
  8. $ Res = array ();
  9. $ Mh = curl_multi_init (); // create multiple curl handles
  10. Foreach ($ array as $ k => $ url ){
  11. $ Conn [$ k] = curl_init ($ url); // Initialization
  12. Curl_setopt ($ conn [$ k], CURLOPT_TIMEOUT, $ timeout); // Set the timeout time
  13. Curl_setopt ($ conn [$ k], CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )');
  14. Curl_setopt ($ conn [$ k], CURLOPT_MAXREDIRS, 7); // HTTp targeting level, top 7
  15. Curl_setopt ($ conn [$ k], CURLOPT_HEADER, false); // no header here, block efficiency
  16. Curl_setopt ($ conn [$ k], CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
  17. Curl_setopt ($ conn [$ k], CURLOPT_RETURNTRANSFER, 1); // The result must be a string and output to the screen.
  18. Curl_setopt ($ conn [$ k], CURLOPT_HTTPGET, true );
  19. Curl_multi_add_handle ($ mh, $ conn [$ k]);
  20. }
  21. // Prevent endless loop consumption of cpu. this section is based on the online statement.
  22. Do {
  23. $ Mrc = curl_multi_exec ($ mh, $ active); // when no data exists, active = true
  24. } While ($ mrc = CURLM_CALL_MULTI_PERFORM); // when receiving data
  25. While ($ active and $ mrc = CURLM_ OK) {// when there is no data or the request is paused, active = true
  26. If (curl_multi_select ($ mh )! =-1 ){
  27. Do {
  28. $ Mrc = curl_multi_exec ($ mh, $ active );
  29. } While ($ mrc = CURLM_CALL_MULTI_PERFORM );
  30. }
  31. }
  32. Foreach ($ array as $ k => $ url ){
  33. If (! Curl_errno ($ conn [$ k]) {
  34. $ Data [$ k] = curl_multi_getcontent ($ conn [$ k]); // convert data to array
  35. $ Header [$ k] = curl_getinfo ($ conn [$ k]); // return http header information
  36. Curl_close ($ conn [$ k]); // Close the slogan.
  37. Curl_multi_remove_handle ($ mh, $ conn [$ k]); // release resources
  38. } Else {
  39. Unset ($ k, $ url );
  40. }
  41. }
  42. Curl_multi_close ($ mh );
  43. Return $ data;
  44. }
  45. // Receive parameters
  46. $ Callback = $ _ GET ['callback'];
  47. $ Hrefs = $ _ GET ['hrefs'];
  48. $ Urlarray = explode (',', trim ($ hrefs ,','));
  49. $ Date = date ('ymmd', time ());
  50. // Instantiate
  51. $ Img = new HttpImg ();
  52. $ Stime = $ img-> getMicrotime (); // start time
  53. $ Data = $ img-> Curl_http ($ urlarray, '20'); // list data
  54. Mkdir ('./img/'. $ date, 0777 );
  55. Foreach (array) $ data as $ k => $ v ){
  56. Preg_match_all ("/(href | src) = ([" | ']?) ([^ "'>] +. (Jpg | png | PNG | JPG | gif) \ 2/I", $ v, $ matches [$ k]);
  57. If (count ($ matches [$ k] [3])> 0 ){
  58. $ Dataimg = $ img-> Curl_http ($ matches [$ k] [3], '20'); // binary of all image data
  59. $ J = 0;
  60. Foreach (array) $ dataimg as $ kk => $ vv ){
  61. If ($ vv! = ''){
  62. $ Rand = rand );
  63. $ Basename = time (). "_". $ rand. ".". jpg; // save as a jpg file
  64. $ Fname = './img/'. $ date. "/". "$ basename ";
  65. File_put_contents ($ fname, $ vv );
  66. $ J ++;
  67. Echo "create the". $ j. "Picture". "$ fname "."
    ";
  68. } Else {
  69. Unset ($ kk, $ vv );
  70. }
  71. }
  72. } Else {
  73. Unset ($ matches );
  74. }
  75. }
  76. $ Etime = $ img-> getMicrotime (); // end time
  77. Echo "time used". ($ etime-$ stime). "seconds ";
  78. Exit;


Multithreading, image download, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.