- Cod:
< ?php
//functia curl de procesare in paralel
function curl($nodes, $referer) {
// atentie la functia asta daca aveti safe mode on nu functioneaza
set_time_limit(0);
if(!$referer)
$referer = $nodes[0];
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init(); //initiem multi curl
for($i = 0; $i < $node_count; $i++) { //pentru fiecare nod (link)
//cream linkul.
$curl_arr[$i] = curl_init($nodes[$i]);
curl_setopt($curl_arr[$i],CURLOPT_FRESH_CONNECT,true);
curl_setopt($curl_arr[$i],CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_arr[$i],CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_arr[$i],CURLOPT_REFERER,$referer);
curl_setopt($curl_arr[$i],CURLOPT_TIMEOUT,30);
//il adaugam la masterul multi-curl
curl_multi_add_handle($master, $curl_arr[$i]);
}
$finalresult = array();
$returnedOrder = array();
do{
//extragem contentul
curl_multi_exec($master, $running);
$info = curl_multi_info_read($master);
if($info['handle']) {
$finalresult[] = curl_multi_getcontent($info['handle']);
$returnedOrder[] = array_search($info['handle'], $curl_arr, true);
curl_multi_remove_handle($master, $info['handle']);
curl_close($curl_arr[end($returnedOrder)]);
}
$previousActive = $running;
}
while($running > 0);
//returnam
return array_combine($returnedOrder, $finalresult);
curl_multi_close($master);
}
// specificati adresele paginilor in array
$urls = array( 'link1', 'link2' );
curl($urls, null);
?>
4en3rgY Romania