0

I am using a file_get_contents along with a new SimplexmlElement($xml) in which sometimes the XML I'm pulling has a Error sign like below. I would like to loop the request until the error message is no more and store the XML. I have the parse already setup. But don't know how to do the loop of file_get_contents with the SimplexmlElements. The error I get is < error > < h2 > ERROR < / error >

Is there a way to loop a get_file_contents until condition is met in XML then us the current XML of the one where there isn't an error?

hakre
  • 193,403
  • 52
  • 435
  • 836
Dan
  • 183
  • 3
  • 14

1 Answers1

0

You could do a loop:

while( !is_object($xml) || $tryAgain){
    $xml = simplexml_load_file(rawurlencode('http://www.example.com/yourxml.xml'));
    if(is_object($xml->error)){
        $tryAgain = true;
        sleep(10);
    }else{
        $tryAgain = false;
    }
}

See this answer regarding sleep: Does sleep time count for execution time limit?

(simplexml_load_file can also load URLs, see http://php.net/simplexml-load-file , so you don't need the file_get_contents)

Community
  • 1
  • 1
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36