php curl multi interface

// create the multi curl handle

$mh = curl_multi_init();

$handles = array();

for($i=0;$i<5;$i++)

{

// create a new single curl handle

$ch = curl_init();

// setting several options like url, timeout, returntransfer

// simulate multithreading by calling the wait.php script and sleeping for $rand seconds

curl_setopt($ch, CURLOPT_URL, "http://put your url here/wait.php?seconds=".($i+1));

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

// add this handle to the multi handle

curl_multi_add_handle($mh,$ch);

// put the handles in an array to loop this later on

$handles[] = $ch;

}

// execute the multi handle

$running=null;

do

{

curl_multi_exec($mh,$running);

// added a usleep for 0.25 seconds to reduce load

usleep (250000);

} while (
$running > 0);

// get the content of the urls (if there is any)

for($i=0;$i<count($handles);$i++)

{

// get the content of the handle

$output.= curl_multi_getcontent($handles[$i]);

// remove the handle from the multi handle

curl_multi_remove_handle($mh,$handles[$i]);

}

// echo the output to the screen

echo $output;

// close the multi curl handle to free system resources

curl_multi_close($mh);

?>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *