Jump to content

error handling simplexml_load_file()


edzillion

Recommended Posts

I am making a wordpress widget and I want it to deal with a site being down.

 

The code:

$xml = simplexml_load_file('scripts/pricefeed.php');
if($xml)
{
   //display widget etc.

 

Pricefeed checks if the URL exists and returns false if it does not:

<?php
$fileUrl = "http://dgcsc.org/goldprices2.xml";
$AgetHeaders = @get_headers($fileUrl);

if (preg_match("|200|", $AgetHeaders[0])) {
	// file exists
	readfile($fileUrl);
} 
else 
{
	// file doesn't exist
	return false;
}
?>

 

it works fine if the URL is there but if it isnt I get the error 'parser error : Document is empty' on the simple_xml_load line.

Do I need a try catch statement or something? Not v. experienced at that....

 

 

Link to comment
https://forums.phpfreaks.com/topic/94693-error-handling-simplexml_load_file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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