views:

44

answers:

1

Hi there,

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

I'm trying to create a php script which retries another php script up to 3 times until an error message is displayed. I'm thinking perhaps this could be done using a php loop? If the code works successfully the first time, then there is no need for it to retry 3 times, however, if it doesn't work the first time, then it should retry the php script up to 3 times untill an error message is displayed.

Using php coding, I've managed to make a script which grabs/fetches content from another location using "file_get_contents" and thereafter gives each word/data a php variable. All this was done by getting help from other members on stackoverflow (which I extremely appreciate). The code below is what does it all:

$searchdata = file_get_contents('http://www.example.com');

list($no1, $no2, $no3, $no4, $no5, 
     $no6, $no7, $no8, $no9) = explode(" ", $searchdata);

So, I'd like to add some sort of loop which retries this script up to 3 times; if it doesn't work the first time.

To determine whether the script works the first/second/third time or not, the text "#endofscript" or "failure" should be found when using "file_get_contents". The text "#endofscript" should be on the variable "$no9" and the text "failure" should be on the variable "$no2". If anything else is found other than "#endofscript" or "failure" that should be counted as an error and should be looped till found. If it still isn't found after the third try, could an error message be displayed? Such as "Error - Please try again".

Thank you for all your assistance and I will appreciate each and every single reply. If you need more details, please feel free to ask. :) And again, I'm really grateful for this. :)

+1  A: 
$retries = 0;
$searchdata = null;

while(!$searchdata && $retries < 3) {
    $retries++;
    $searchdata = file_get_contents('http://www.example.com');
    list($no1, $no2, $no3, $no4, $no5, $no6, $no7, $no8, $no9) = explode(" ", $searchdata);
    if($no9 != '#endofscript' || $no2 != 'failure') {
        $searchdata = null;
    }
}

if($searchdata) {
    list($no1, $no2, $no3, $no4, $no5, $no6, $no7, $no8, $no9) = explode(" ", $searchdata);
    // DO STUFF
} else {
    echo 'Please try again.';
}
Matt Williamson
Hi Matt Williamson, I really appreciate the response and it seems like you've posted some amazing coding. However, there seems to be a slight problem, when I run the code, php gives me the error "Parse error: syntax error, unexpected T_INC" and it's on line 5. Which is the "retries++;" part. Sorry for the bother and again, I do appreciate your reply. :)
AUllah1
It should be `$retries++`, Matt forgot the `$` sign, so PHP's trying to increment a constant which doesn't exist.
Marc B
Yep, corrected it. Thanks Marc
Matt Williamson
Again, thank you for the responses Marc B and Matt Williamson. I extremely appreciate them. :) Regarding the script, I don't think it works. :( Either "#endofscript" or "failure" should be found (and not both). If these are found, then it should work as normally, if not they are not found it should show the error message. Looking at the coding, it should work however, using the code, it still seems to display other things even when either "#endofscript" or "failure" are not found. Using file_get_contents, the source may time out and show random text. To avoid this, the retry script is needed.
AUllah1
P.S. The version of php I'm running is 4.4.9, which shouldn't cause a compatibility problem right? Sorry for the bother and as I keep repeatedly say, I appreciate your responses. :)
AUllah1
Can't really help too much, as I don't have the file that you are getting, but if you need to switch from throwing an error if it IS '#endofscript' then change the `==` to `!=` (not equals)
Matt Williamson
Again, I appreciate your response. ;) The file I'm downloading using "file_get_contents" is simply a dynamic page titled "dynamic.html". Although it's a HTML page, there are no HTML tags involved and contains the following data: "92 AULLAH1 325 6523 37 1 12 5 #endofscript"The numbers are always different depending on certain factors. Some times this data doesn't show up correctly due to time out/loss issues. Therefore I need to make sure the text "#endofscript" or "failure" is found, this indicates that the script didn't time out, if none of these are found, then it did indeed time out.
AUllah1
If it did time out, then the best thing to do is retry the retry script up to 3 times until an error message is displayed (the script which you've posted; I appreciate it. ;) At the moment, the retry script still seems to display other things other than the file_get_contents data, therefore that shows that the script isn't working. :( Again, as stated the coding seems fine but I don't know what the problem is. I appreciate your response. ;)
AUllah1