RElated Topics

Wednesday, January 21, 2009

Curl Concurrent Multiple Requests


<?php
    
    
function curTime()
    {
        
$mtime microtime();
        
$mtime explode(" ",$mtime);
        return 
$mtime[1] + $mtime[0];
    }

    
//
    
if( !empty($_GET['get_code']) )
    {
        
system('php -s '__FILE__);    
        exit();
    }    

    
//
    
if( !empty($_GET['req']) )
    {
        
$t1=curTime();

        
sleep($_GET['t']);

        
$t2=curTime();

        
$tdiff $t2 $t1;

        echo 
"<hr>
        {$_SERVER['REQUEST_URI']}
        {$_GET['l']} Start {$t1}
        {$_GET['l']} Sleep for {$_GET['t']} Seconds
        {$_GET['l']} End   {$t2}        
        {$_GET['l']} Total Processing Time {$tdiff}"
;

        exit();
    }    
    

    
//    
    
$self_url 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    
    
$total_req 20;
    
    
$arr_url = array();

    
$arr_url[] = $self_url."?req=Req_First&t=5";
    for(
$i=0$i<$total_req$i++)
    {
        
$arr_url[] = $self_url."?req=Req_{$i}&t=".rand(1,2);
    }
    

    
$t1=curTime();
    
    
$output fetch_urls($arr_url);
    
    
$t2=curTime();

    
$tdiff $t2 $t1;

    echo 
"<pre>
    Main Start {$t1}
    Main End {$t1}
    Main Total Processing Time {$tdiff} </pre>
    <a href='{$self_url}?get_code=1' target='_blank'>View Code</a>
    Sending Total "
.count($output)." Requests Parallel";

    foreach(
$output as $i => $str)
    {
        if( empty(
$str) )
        {
            
$str "<hr><b>Time Out for [{$i}]: {$arr_url[$i]}</b>";
        }
        echo 
"<pre>{$str}</pre>";
    }

    exit(); 

    
    
//
    
function fetch_urls($arr_url)
    {
        if( empty(
$arr_url) ) return false;
        if( !
is_array($arr_url) ) $arr_url = array( $arr_url );
        
        
$count count($arr_url);
        
$ach = array(); 
        
        foreach(
$arr_url as $i => $url )
        {
            
$ach[$i] = curl_init();
            
curl_setopt($ach[$i], CURLOPT_URL$url);
            
curl_setopt($ach[$i], CURLOPT_RETURNTRANSFERtrue);//if false then curl_exec() will echo its result        
            
curl_setopt($ach[$i], CURLOPT_TIMEOUT3);
        }

        
//create the multiple cURL handle
        
$mh curl_multi_init();
        
        
//add the multipel handler 
        
foreach($arr_url as $i => $url )
        {            
            
curl_multi_add_handle($mh,$ach[$i]);
        }

        
//execute the handles
        
$running=null;          
        do
        {
            
curl_multi_exec($mh,$running);      
        } while(
$running 0);

        
$output=array();
        
        
//Collect all Request Output
        
foreach($arr_url as $i => $url )
        {
            
$output[$i] = curl_multi_getcontent($ach[$i]);
            
curl_multi_remove_handle($mh$ach[$i]);
        }

        
curl_multi_close($mh);
        return 
$output;
    }

    
//
    
function test1()
    {
        
$ch1 curl_init();
        
$ch2 curl_init();

        
// set URL and other appropriate options
        
curl_setopt($ch1CURLOPT_URL"http://localhost/hemant/curl/time.php?l=Req1&t=3");
        
#curl_setopt($ch1, CURLOPT_HEADER, 0);
        
curl_setopt($ch1CURLOPT_RETURNTRANSFERtrue);//if false then curl_exec() will echo its result        

        #$output = curl_exec($ch1);
        #$info = curl_getinfo($ch1);
        #var_dump($output , $info ); die();



        
curl_setopt($ch2CURLOPT_URL"http://localhost/hemant/curl/time.php?l=Req2&t=2");
        
#curl_setopt($ch2, CURLOPT_HEADER, 0);
        
curl_setopt($ch2CURLOPT_RETURNTRANSFERtrue);//if false then curl_exec() will echo its result

        //create the multiple cURL handle
        
$mh curl_multi_init();

        
//add the two handles
        
curl_multi_add_handle($mh,$ch1);
        
curl_multi_add_handle($mh,$ch2);

        
$running=null;  
        
//execute the handles
        
do {
            
curl_multi_exec($mh,$running);      
        } while(
$running 0);

        
$output=array();
        
$output[1] = curl_multi_getcontent($ch1);
        
$output[2] = curl_multi_getcontent($ch2);

        
//close all the handles
        
curl_multi_remove_handle($mh$ch1);
        
curl_multi_remove_handle($mh$ch2);
        
curl_multi_close($mh);
        return  
$output;
    }



?>