3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = microtime(true); rolling_curl($urls, $thr , "cb"); $end = microtime(true); $time = $end - $start; echo '<b>Total time '.round($time , 2).' seconds</b><br />'."\n"; function rolling_curl($urls, $rolling_window = 2 , $callback, $custom_options = null) { // make sure the rolling window isn't greater than the # of urls $rolling_window = (count($urls) < $rolling_window) ? count($urls) : $rolling_window; //$running = null; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => true, //CURLOPT_FAILONERROR=> true, CURLOPT_CONNECTTIMEOUT_MS=> 2000, CURLOPT_TIMEOUT_MS=> 2000, CURLOPT_FRESH_CONNECT=> true, CURLOPT_FORBID_REUSE=> true ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; ++$i) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch,$options); curl_multi_add_handle($master, $ch); } do { while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM ); if($execrun != CURLM_OK) break; // a request was just completed -- find out which one while($done = curl_multi_info_read($master)) { $info = curl_getinfo($done['handle']); if ($info['http_code'] == 200) { $output = curl_multi_getcontent($done['handle']); // request successful. process output using the callback function. $callback($output , $info['url'] , $start); // start a new request (it's important to do this before removing the old one) if( isset($urls[$i+1]) ) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i+1]; ++$i; // increment i curl_setopt_array($ch,$options); curl_multi_add_handle($master, $ch); // remove the curl handle that just completed curl_multi_remove_handle($master, $done['handle']); } else curl_multi_remove_handle($master, $done['handle']); } else { echo curl_error($done['handle'])." - ".$info['url']." <br/>\n\r"; //echo $info['url']." -> error<br>\n\r"; //print_r($info); // request failed. add error handling. if( isset($urls[$i+1]) ) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i+1]; ++$i; // increment i curl_setopt_array($ch,$options); curl_multi_add_handle($master, $ch); curl_multi_remove_handle($master, $done['handle']); } // remove the curl handle that just completed else curl_multi_remove_handle($master, $done['handle']); } } } while ($running); curl_multi_close($master); return true; } ?>

preferences:
29.09 ms | 402 KiB | 5 Q