Total Pageviews

Monday, June 13, 2011

fetch multiple contents with time out using curl with php mutitreading

Follow kvn_krishna on Twitter


this prgm used to fetch  the contents from other servers .... wil set the sleeping time and and will see the time taken and how to fetch ,,, by using curl with php mutitreading
<style type="text/css">
<!--
.style2 {color: #FF0000; font-style:oblique; font-size:24px;}
-->
</style>

<?php
function addHandle(&$curlHandle,$url)
{
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($curlHandle,$cURL);
return $cURL;
}
//execute the handle until the flag passed to function is greater then 0
function ExecHandle(&$curlHandle)
{
$flag=null;
do {
    curl_multi_exec($curlHandle,$flag);//fetch pages in parallel
} while ($flag > 0);
}


$list[1] = "http://www.google.co.in/";
$list[2] = "http://in.yahoo.com/?p=in";
$list[3] = "http://www.rediff.com/";
$list[4] = "http://www.bing.com/";
$curlHandle = curl_multi_init();

for ($i = 1;$i <= 4; $i++)
 $curl[$i] = addHandle($curlHandle,$list[$i]);
ExecHandle($curlHandle);
for ($i = 1;$i <= 4; $i++)
{
// set the sleeping time on 2 seconds
 usleep(2000000);
 {
 // get the content from other servers
 $text[$i] =  curl_multi_getcontent ($curl[$i]);
 echo $text[$i];
  }
 
 echo "<span class='style2'>$i time out".date("h:i:s")."</span>";
}
for ($i = 1;$i <= 4; $i++)//remove the handles
  curl_multi_remove_handle($curlHandle,$curl[$i]);
curl_multi_close($curlHandle);

?>

No comments:

Post a Comment