Prevent the error loading XML
I'm coding a little script that retrieves information stored on an xml on a remote server. The trouble is when I load the XML in this way:
Code:
$xml = new SimpleXMLElement("http://www.xxx.com/address/of/xml.xml", NULL, TRUE );
Well I happen to have the following error because of too many connected at the same time:
Quote:
Warning: SimpleXMLElement::__construct(
http://www.xxx.com/address/of/xml.xml) [simplexmlelement.--construct]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\Program Files\EasyPHP5.3.0\www\project\resources\xml.php on line 43
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "http://www.xxx.com/address/of/xml.xml" in C:\Program Files\EasyPHP5.3.0\www\project\resources\xml.php on line 43
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\Program Files\EasyPHP5.3.0\www\project\resources\xml.php:43 Stack trace: #0 C:\Program Files\EasyPHP5.3.0\www\project\resources\xmlr.php(43): SimpleXMLElement->__construct('http://www.xxx.com/address/of/xml.xml', 0, true) #1 {main} thrown in C:\Program Files\EasyPHP5.3.0\www\project\resources\xml.php on line 43
How can I ensure that the XML will be reloaded until there is no error and continue the execution of my script?
Re: Prevent the error loading XML
Are you sure it's because of too many online? Otherwise you can go through the function http://www.php.net/manual/en/functio...-load-file.php. It is generally the same as what you want, except that it returns either a SimpleXMLElement or false. So a loop should get what you want. But stinks the risk of infinite loop your thing.
Re: Prevent the error loading XML
Thank you for this response, its exactly what I needed and it works great. Yes even I feel that the infinite loop but not the right choice. Yes there are too many people who consult the xml at the same time.
Re: Prevent the error loading XML
It is not a good idea to put the xml in BD or make a counter of number of people who want to read this xml and spent a nb, copy the file (basically a mini load management)?
Re: Prevent the error loading XML
Macario in your case I assume that this XML, you'll find on a server other than yours, and this XML you load each time a visitor passes. It's just as disgusting approach. You could not store it on your site, and do update every 30 minutes for example. It will avoid overloading the remote server that hosts the XML