Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $urls = array(
- 'http://static.php.net/www.php.net/images/php.gif',
- 'http://p.ebaystatic.com/aw/pics/globalheader/spr9.png'
- );
- $save_to = '/tmp/';
- function add_file_to_curl($save_to, $mh, $url, $i){
- global $conn, $fp;
- $g=$save_to.basename($url);
- if(is_file($g)){
- unlink($g);
- }
- $conn[$i]=curl_init($url);
- $fp[$i]=fopen ($g, "w");
- curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
- curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
- curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
- curl_multi_add_handle ($mh,$conn[$i]);
- }
- $conn="";
- $fp="";
- $mh = curl_multi_init();
- foreach ($urls as $i => $url) {
- add_file_to_curl($save_to, $mh, $url, $i);
- echo "URL IS $url, I is $i\n";
- }
- do {
- $n = curl_multi_exec($mh,$running);
- $ready = curl_multi_select($mh); // Waiting for one of the files to finish
- if(0 < $ready){
- while($info = curl_multi_info_read($mh)){
- $status = curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
- echo "STATUS $status\n";
- if(200 == $status){
- $successUrl = curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
- echo "$successUrl\n";
- }
- break 1;
- }
- }
- }
- while (0 < $running && -1 != $ready);
- $info = curl_multi_info_read($mh);
- $status = curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
- echo "STATUS $status\n";
- if(200 == $status){
- $successUrl = curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
- echo "$successUrl\n";
- }
- foreach ($urls as $i => $url) {
- echo "Running on $url and $i\n";
- curl_multi_remove_handle($mh,$conn[$i]);
- curl_close($conn[$i]);
- fclose ($fp[$i]);
- }
- curl_multi_close($mh);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement