Jump to content

if file_get_contents failed...loop?


Mike Solstice

Recommended Posts

Ok, I'm having some trouble with file_get_contents failing & am trying to make a loop that will sleep & then retry a maximum of 3 times before giving up & moving to the next item. I tried using a while loop but I'm getting unexpected T_WHILE, which I assume is because it's already in a while loop?

 

I found this:

 

$maxTries = "3";
for ($try=1; $try<=$maxTries; $try++) {

 

but could use some clarification. Will that only trigger if it failes or do I need to include another if statement like

 

$myfile = file_get_contents("http://example.com"); 
if ($myfile === FALSE) {
$maxTries = "3";
for ($try=1; $try<=$maxTries; $try++) {
sleep(3);
$myfile = file_get_contents("http://example.com"); 
}
else {
//continue with script

 

I hope that makes sense...trying to explain it as best I can without having to post my actual source code as I've had it stolen off forums before & now I have a clone to compete with. ;) I'm not great at putting this into words so if I need to clarify, please feel free to ask. I appreciate any & all help given. Thanks!

 

Link to comment
Share on other sites

So something like this?

 

$myfile = file_get_contents("http://example.com");
$maxTries = "3";
for ($try=1; $try<=$maxTries; $try++) {
if ($myfile === FALSE) {
sleep(3);
$myfile = file_get_contents("http://example.com"); 
}
else {
break;
//close for loop
}
//continue with script & close else
}
//close else

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.