Link to home
Start Free TrialLog in
Avatar of lovewithu
lovewithu

asked on

Need help on multi_curl


I got a php script that is using multi_curl where I would need a little help on. It´s nothing big but I don´t find the solution

So here is the part of the script that is making problems:
$ch = array();
$master = curl_multi_init();

for($i = 0; $i < 5; $i++)
{
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $url[$i]);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch[$i], CURLOPT_HEADER, 1);
curl_setopt($ch[$i], CURLOPT_TIMEOUT, cTimeout);

curl_multi_add_handle($master, $ch[$i]);
}

do
{
unset ($curlInfo);

curl_multi_exec($master,$running);

$curlInfo = curl_multi_info_read ($master);

// I guess if curlInfo[msg] == 1 that means that one of the handles finished
if ($curlInfo[msg] == 1)
{

/////////////////////////////////////////////////////////////////////////////// HERE IS THE PROBLEM
}
} while($running > 0);

Now if you take a look at "HERE IS THE PROBLEM". I need to know instantly when the handle´s transfer has finished to know which of the 5 handles has finished.

So I tried looping all handles using curl_getinfo but the problem there is that until the loop has finished already other handles have finished so that I only can find out e.g. handle 1,2 and 5 have finished but I don´t get the information instantly as soon as a handle has finished.

But enough of that - I guess more explanation is more confusing to my bad English.

All that I need is the exact Syntax how I would find out in a curl_multi_exec IF and WHICH handle has finished. So probably 2 lines of code.
So here is the part of the script that is making problems:
$ch = array();
$master = curl_multi_init();
 
for($i = 0; $i < 5; $i++)
{
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $url[$i]);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch[$i], CURLOPT_HEADER, 1);
curl_setopt($ch[$i], CURLOPT_TIMEOUT, cTimeout);
 
curl_multi_add_handle($master, $ch[$i]);
}
 
do
{
unset ($curlInfo);
 
curl_multi_exec($master,$running);
 
$curlInfo = curl_multi_info_read ($master);
 
// I guess if curlInfo[msg] == 1 that means that one of the handles finished
if ($curlInfo[msg] == 1)
{
 
/////////////////////////////////////////////////////////////////////////////// HERE IS THE PROBLEM
}
} while($running > 0);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lovewithu
lovewithu

ASKER

So where i need to change ?

<pre>
<?php
$url = array("http://www.yahoo.com/","http://www.google.com/","http://www.msn.com/","http://www.experts-exchange.com/","http://www.php.net/");
$ch = array();
$master = curl_multi_init();
define("cTimeout", 10);
for($i = 0; $i<5; $i++){
	$ch[$i] = curl_init();
	curl_setopt($ch[$i], CURLOPT_URL, $url[$i]);
	curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch[$i], CURLOPT_HEADER, 1);
	curl_setopt($ch[$i], CURLOPT_TIMEOUT, cTimeout);
	curl_multi_add_handle($master, $ch[$i]);
}
 
$running=null;
do {
    curl_multi_exec($master,$running);
} while ($running > 0);
 
$data = array();
for($i = 0; $i<5; $i++){
	$data[$i] = curl_multi_getcontent($ch[$i]);
	curl_multi_remove_handle($master,$ch[$i]);
}
 
curl_multi_close($master);
 
echo htmlentities(print_r($data,true));
?>

Open in new window

hi,

please explain this....What u trying to say...
The script part you posted would  handles are finished getinfo on the handles...
Let´s say I got 10 urls to check by multicurl:
they would be initiated at the same time by multicurl as 10 different handles.

I need to somehow find out instantly wether any of those 10 urls transfers is finished already and if yes, which one..

I even tried already curl_getinfo within the do-while-loop, but that wouldn´t work since sometimes it´s simply too slow - in the meaning of other handles having finished while looping the curl_getinfo....
so somehow there needs to be somehow any other curl_multi-function that would allow that, but I don´t know which one or the syntax for it

I was unable to find an example as you would like it to work.
Can u please get it from someone else...ur friends...
You've asked this question twice, if the many experts that read the two identical posts do not provide a solution then quite possibly one doesn't exist.