View Full Version : Problem with my while loop

11-04-2009, 08:09 PM

I have a small problem with my while :

The problem is that I want to test that the data is recieved by
the curl function. If the url was invalid, then the next url in
the $urls array is selected and tried.

Problem is that the string $data will not exist until I recive it
so the following code doesn't work.

Should set $data to false, would that help ?

$i = 0;

while(!$data) {
$target = $urls[$i];
echo "<br>Re-starting<br>Target_url: $target";

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);

Thanks for advice. :)

PS the first curl_close($ch); will act on a previously opened
handle. Then it has to close and failed ones.

11-04-2009, 08:11 PM
Change the "while" loop to a "do while" loop.


11-04-2009, 09:36 PM
Thanks :thumbsup:

That fixed it :)