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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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