Friday, 26 June 2015

Parallel Multiple API call Using PHP


Here I will explain how to call multiple apis in parallel process. So I am going to use PHP CURL function to achieve it   

#Default GET method

 $nodes =array(array('url'=>"http://rest.example1.com"),
                                   array('url'=>"http://rest.example2.com?id=2&param1=yourvalue"),
                                   array('url'=>"http://rest.example3.com?id=2&param1=yourvalue")
                                    );
   $results = multiThreading($nodes);

#POST Method And PUT Method

 $user_data  will be array of your post data
 like  $user_data['name']= 'dev'; 
        $user_data['email']= 'xyz@test.com';

        $nodes[0]['url'] ="http://rest.";
        $nodes[0]['post'] = $user_data;
        $results = multiThreading($nodes);

#POST Method And PUT Method  - XML Input

$user_data  will be array of your post data
like  $user_data['name']= 'dev'; 
        $user_data['email']= 'xyz@test.com';

        $nodes[0]['url'] ="http://rest.";
        $nodes[0]['post'] = $user_data;
        $results = multiThreading($nodes,array('Content-Type: application/xml'));


Output - var_dump( $results);


function multiThreading($nodes){
$node_count = count($nodes);
$RESP_TIMEOUT = 30;
$curl_arr = array();
$tmp_str_cookies = "";
/* pass your Headers */
$tmp_arr_httpheader = array("Connection: Close");

$master = curl_multi_init();

$options = array(
        CURLOPT_RETURNTRANSFER => true,         // return web page
        CURLOPT_HEADER         => false,        // don't return headers
        CURLOPT_FOLLOWLOCATION => true,         // follow redirects
        CURLOPT_AUTOREFERER    => true,         // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 10,          // timeout on connect
       // CURLOPT_TIMEOUT        => $RESP_TIMEOUT,          // timeout on response
        CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
        CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
        CURLOPT_SSL_VERIFYPEER => false,        //
        CURL_HTTP_VERSION_1_1  => 1,
CURLOPT_NOSIGNAL => true,
CURLOPT_HTTPHEADER =>$tmp_arr_httpheader,
CURLOPT_REFERER => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
    );



for($i = 0; $i < $node_count; $i++)
{
$url = (is_array($nodes[$i]) && !empty($nodes[$i]['url'])) ? $nodes[$i]['url'] : $nodes[$i];
$resp_timeout =  (is_array($nodes) && array_key_exists('resp_timeout',$nodes[$i])) ? $nodes[$i]['resp_timeout'] : $RESP_TIMEOUT;
// default 5 secs timeout
$curl_arr[$i] = curl_init();
curl_setopt($curl_arr[$i], CURLOPT_URL, $url);
if (is_array($nodes[$i])) {
if (!empty($nodes[$i]['post'])) {
if (is_array($nodes[$i]['post']))
{
$o="";
foreach ($nodes[$i]['post'] as $k=>$v)
{
$o.= "$k=".$v."&";
}
$post_data=substr($o,0,-1);
}
else
{
$post_data = $nodes[$i]['post'];

}
curl_setopt($curl_arr[$i], CURLOPT_POST,1);
curl_setopt($curl_arr[$i], CURLOPT_POSTFIELDS,$post_data);
}
elseif (!empty($nodes[$i]['put'])) 
{
if (is_array($nodes[$i]['put']))
{
$o="";
foreach ($nodes[$i]['put'] as $k=>$v)
{
$o.= "$k=".$v."&";
}
$post_data=substr($o,0,-1);
}
else
{
$post_data = $nodes[$i]['put'];

}
curl_setopt($curl_arr[$i], CURLOPT_CUSTOMREQUEST,'PUT');
curl_setopt($curl_arr[$i], CURLOPT_POSTFIELDS,$post_data);
}
elseif (!empty($nodes[$i]['delete'])) 
{
curl_setopt($curl_arr[$i], CURLOPT_CUSTOMREQUEST,'DELETE');
}

}
curl_setopt($curl_arr[$i], CURLOPT_TIMEOUT, $resp_timeout);
curl_setopt_array($curl_arr[$i],$options);
curl_multi_add_handle($master, $curl_arr[$i]);

}
do {
curl_multi_exec($master,$running);
usleep(10000);
} while($running > 0);
for($i = 0; $i < $node_count; $i++)
{
$results[$i] = curl_multi_getcontent  ( $curl_arr[$i]  );
$tmp_arr = (curl_getinfo($curl_arr[$i]));
curl_multi_remove_handle($master, $curl_arr[$i]);
curl_close($curl_arr[$i]);

        

}
$rest_latency = $res_latency_array;

curl_multi_close($master);

return $results;
}

No comments:

Post a Comment