PHP Multi-threaded Batch acquisition download image implementation code

Source: Internet
Author: User
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
  1. /**
  2. * Curl Multithreading
  3. * @param array $array parallel URLs
  4. * @param int $timeout time-out
  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 time-out
  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 orientation level, 7 maximum
  15. curl_setopt ($conn [$k], Curlopt_header, false);//Do not HEADER, add block efficiency
  16. curl_setopt ($conn [$k], curlopt_followlocation, 1); 302 redirect
  17. curl_setopt ($conn [$k], curlopt_returntransfer,1);//requires the result to 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. To prevent the death cycle from killing the CPU. This is based on the online wording
  22. do {
  23. $MRC = Curl_multi_exec ($MH, $active);//When no data, active=true
  24. } while ($MRC = = Curlm_call_multi_perform);//When data is being accepted
  25. while ($active and $MRC = = CURLM_OK) {//when there is no data or when a 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]);//data converted to array
  35. $header [$k]=curl_getinfo ($conn [$k]);//return HTTP header information
  36. Curl_close ($conn [$k]);//Closing the handle
  37. Curl_multi_remove_handle ($MH, $conn [$k]); Freeing resources
  38. }else{
  39. Unset ($k, $url);
  40. }
  41. }
  42. Curl_multi_close ($MH);
  43. return $data;
  44. }
  45. Parameter Receive
  46. $callback = $_get[' callback ');
  47. $hrefs = $_get[' hrefs ');
  48. $urlarray = Explode (', ', trim ($hrefs, ', '));
  49. $date = Date (' Ymd ', Time ());
  50. Instantiation of
  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 ');//All picture data binary
  59. $j = 0;
  60. foreach ((array) $dataimg as $kk = + $vv) {
  61. if ($vv! = ") {
  62. $rand = rand (1000,9999);
  63. $basename = Time (). " _ ". $rand.". jpg;//files saved in JPG format
  64. $fname = './img/'. $date. " /"." $basename ";
  65. File_put_contents ($fname, $VV);
  66. $j + +;
  67. echo "Create section". $j. " Photo "." $fname "."
    ";
  68. }else{
  69. Unset ($kk, $VV);
  70. }
  71. }
  72. }else{
  73. Unset ($matches);
  74. }
  75. }
  76. $etime = $img->getmicrotime ();//End time
  77. echo "Spents". ($etime-$stime). " Seconds ";
  78. Exit
Copy Code
Multithreading, download images, 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.