...

View Full Version : Problem with my while loop



jeddi
11-04-2009, 08:09 PM
Hello,

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) {
curl_close($ch);
$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);
$i++;
}

Thanks for advice. :)

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

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

http://us3.php.net/manual/en/control-structures.do.while.php

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

That fixed it :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum