Guest User

PHP Multi Curl - possible bug

a guest
Jun 25th, 2012
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $urls = array(
  2.     "http://www.google.com",
  3.     "http://www.yahoo.com",
  4. );
  5.  
  6. $mh = curl_multi_init();
  7. $activeDownloads = array();
  8.  
  9. foreach ($urls as $i => $url) {
  10.     $curlHandle = curl_init();
  11.     $key = (int) $curlHandle;
  12.  
  13.     curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
  14. //    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  15.     curl_setopt($curlHandle, CURLOPT_HEADER, 0);
  16.     curl_setopt($curlHandle, CURLOPT_URL, $url);
  17.  
  18.     $destinationFilepath = '/tmp/download_' . $key;
  19.     $file = fopen($destinationFilepath, "w");
  20.     curl_setopt($curlHandle, CURLOPT_FILE, $file);
  21.  
  22.     curl_multi_add_handle($mh, $curlHandle);
  23.  
  24.     $activeDownloads[$key] = array(
  25.         'url' => $url,
  26.         'filepath' => $destinationFilepath,
  27.     );
  28. }
  29.  
  30. while (true) {
  31.  
  32.     usleep(50000);
  33.  
  34.     curl_multi_exec($mh, $running);
  35.  
  36.     // Call select to see if anything is waiting for us
  37.     $select = curl_multi_select($mh);
  38.     if ($select < 1) {
  39. //        echo 'No download is ready yet' . PHP_EOL;
  40.         continue;
  41.     }
  42.  
  43.     // Since something's waiting, give curl a chance to process it
  44.     do {
  45.         $status = curl_multi_exec($mh, $running);
  46.         usleep(10000);
  47.     } while ($status == CURLM_CALL_MULTI_PERFORM || $running);
  48.  
  49.  
  50.     // Now grab the information about the completed requests
  51.     while ($info = curl_multi_info_read($mh)) {
  52.  
  53.         $ch = $info['handle'];
  54.         $key = (int) $ch;
  55.  
  56.         // This outputs all the content correctly when CURLOPT_RETURNTRANSFER is true and the CURLOPT_FILE flag is not set
  57. //        $content = curl_multi_getcontent($ch);
  58. //        var_dump($content);
  59. //        die;
  60.  
  61.         $download = $activeDownloads[$key];
  62.  
  63.         $info = curl_getinfo($ch);
  64.  
  65.         curl_close($ch);
  66.  
  67. //        print_r($info); die;
  68.  
  69.         $content = file_get_contents($download['filepath']);
  70.  
  71.         // This output is cut off, usually at 40960 characters, why?
  72.         echo $content . PHP_EOL;
  73.  
  74.         echo strlen($content) . PHP_EOL;
  75.  
  76.         die;
  77.     }
  78. }
RAW Paste Data