Using php I want to load XML files from several websites and want to prevent errors if my one of the sites is down. I’m using this code to access the URL.
function get_xml($url){
$xmlDoc = new DOMDocument();
$xmlDoc->load($url); // This is the line causing errors
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
...
If there is no xml feed available at $url I get lots of error messages like:
Warning: DOMDocument::load(): Space required after the Public Identifier
Warning: DOMDocument::load(): SystemLiteral ” or ‘ expected
Warning: DOMDocument::load(): SYSTEM or PUBLIC, the URI is missing
Warning: DOMDocument::load(): Opening and ending tag mismatch: link line 5 and head
What can I do to prevent $url from attempting to load if the feed isn’t available?
I’ve tried:
if(file_exists($url)){
$xmlDoc->load('$url');
}
And things with fopen and @load but I could well have been using them incorrectly.
Try with:
Your function
get_xmlwill return immediatelyFALSEif the methodloadfails to load the XML data