Guest User

Untitled

a guest
Sep 14th, 2013
138
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $urls = array(
  3.     'http://static.php.net/www.php.net/images/php.gif',
  4.     'http://p.ebaystatic.com/aw/pics/globalheader/spr9.png'
  5. );
  6.  
  7. $save_to = '/tmp/';
  8.  
  9. function add_file_to_curl($save_to, $mh, $url, $i){
  10.         global $conn, $fp;
  11.         $g=$save_to.basename($url);
  12.         if(is_file($g)){
  13.                 unlink($g);
  14.         }
  15.         $conn[$i]=curl_init($url);
  16.         $fp[$i]=fopen ($g, "w");
  17.         curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
  18.         curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
  19.         curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
  20.         curl_multi_add_handle ($mh,$conn[$i]);
  21. }
  22.  
  23. $conn="";
  24. $fp="";
  25. $mh = curl_multi_init();
  26. foreach ($urls as $i => $url) {
  27.         add_file_to_curl($save_to, $mh, $url, $i);
  28.         echo "URL IS $url, I is $i\n";
  29. }
  30. do {
  31.         $n = curl_multi_exec($mh,$running);
  32.         $ready = curl_multi_select($mh); // Waiting for one of the files to finish
  33.         if(0 < $ready){
  34.                 while($info = curl_multi_info_read($mh)){
  35.                         $status = curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
  36.                         echo "STATUS $status\n";
  37.                         if(200 == $status){
  38.                                 $successUrl = curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
  39.                                 echo "$successUrl\n";
  40.                         }
  41.                         break 1;
  42.                 }
  43.         }
  44. }
  45. while (0 < $running && -1 != $ready);
  46. $info = curl_multi_info_read($mh);
  47. $status = curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
  48. echo "STATUS $status\n";
  49. if(200 == $status){
  50.         $successUrl = curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
  51.         echo "$successUrl\n";
  52. }
  53.  
  54. foreach ($urls as $i => $url) {
  55.         echo "Running on $url and $i\n";
  56.         curl_multi_remove_handle($mh,$conn[$i]);
  57.         curl_close($conn[$i]);
  58.         fclose ($fp[$i]);
  59. }
  60. curl_multi_close($mh);
  61. ?>
RAW Paste Data