Jump to content
paxnWo

php curl multithread

Recommended Posts

Posted (edited)


<?php

function curl($nodes, $referer)
{

set_time_limit(0);
if(!$referer){
$referer = $nodes[0];
}
$node_count = count($nodes);

$curl_arr = array();
$master = curl_multi_init();

for($i = 0; $i < $node_count; $i++)
{
$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);

curl_multi_add_handle($master, $curl_arr[$i]);
}
$finalresult = array();
$returnedOrder = array();
do{
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);

return array_combine($returnedOrder, $finalresult);
curl_multi_close($master);

}

$urls = array( 'link1', 'link2' );
curl($urls, null);

?>

argumentul $nodes al functiei trebuie sa fie un array.

functia am gasit-o pe net si am rescris-o.

cu curl fara mulithread am prelucrat 100 de pagini in 32 de secunde.

cu curl cu multithread am facut-o in doar 2.

preluata de aici : Rusty Razor Blade Blog Archive Executing multiple curl requests in parallel with PHP and curl_multi_exec

am de prelucrat 2.400.000 pagini, caut dedicat pe care sa il omor. am nevoie doar o saptamana. cumpar.

Edited by paxnWo

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...