Welcome to WebmasterWorld Guest from 184.73.126.70

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

mask error - DOMDocument::load()

   
11:56 am on May 25, 2010 (gmt 0)

5+ Year Member



I use DOMDocument::load() to load a xml file.

$doc = new DOMDocument();
$doc->load("http://url_of_some_xml_file");

When the xml file indicated by the url fails to load( this I cannot control ), load() throws the following errors and stops the execution of all php code after that-

DOMDocument::load(http://url_of_some_xml_file) [domdocument.load]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://url_of_some_xml_file"


@ cannot be used to comment errors thrown by DOMDocument::load()

Can someone please let me know how I can stop DOMDocument::load() from throwing errors or stop the errors from showing?


Thanks in advance,


Nil111
12:34 pm on May 25, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there Nil111,

Here is the url for the error_reporting from php dot net: [uk3.php.net ]

so if you want the error's turning off (not the best Idea) try: error_reporting(0);

@ - this just supresses error messages, doesn't turn them off ;)

Cheers,
MRb
4:51 am on May 26, 2010 (gmt 0)

5+ Year Member



Thanks for the reply Mathew. tried but @ cannot be used to supress errors thrown by DOMDocument::load(). Will have to consider error_reporting(0);


got this from uk3.php.net -
...................................................
Only display php errors to the developer...

<?php
if($_SERVER['REMOTE_ADDR']=="00.00.00.00")
{
ini_set('display_errors','On');
}
else
{
ini_set('display_errors','Off');
}
?>

Just replace 00.00.00.00 with your ip address.
..............................................
4:57 am on May 26, 2010 (gmt 0)

5+ Year Member



I thought of using ini_set() as follows -

ini_set('display_errors','Off');
$doc->load("http://url_of_some_xml_file");
ini_set('display_errors','On');

just around the load() function. Thanks 4 the help
7:20 am on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there Nil111,

Why dont you just put the class function call into an if clause instead of encasing the function in ini_set() ?

if(!$doc->load("http://url_of_some_xml_file")){
echo "Error opening xml file";
}

or

if($doc->load("http://url_of_some_xml_file")){
//process xml file here
}else{
//throw error, as file couldn't be opened
}

or similar... I'm sure you see the logic to that though ;)

Just seems a bit of a convoluted way of 'deflecting' error messages, error messages are there for a reason, surely you would rather have the script run with no errors?

Cheers,
MRb
8:28 am on May 26, 2010 (gmt 0)

5+ Year Member



Hey I tried that but although load() should return a value of true or false, it throws the same errors. I'm working on Apache httpd server 2.2.11 and php 5.3.0 env. Hope that has nothing to do with it. So I'm stuck with ini_set()

Thanks!

Nil111
10:48 am on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there Nil111,

Well if it works for you, that's Ok - personally though I would try to get rid of the error rather than turning the errors off whilst you call the function.

Cheers,
MRb
11:03 am on May 26, 2010 (gmt 0)

5+ Year Member



Yep you'v got a very good point there. will try a workaround.


The thing is I cant stop the error coz here in case an xml file being called is not available( in this case rss feeds) the error is thrown anyway.

I will post if I come across a better way. Thanks!
10:14 pm on May 29, 2010 (gmt 0)

5+ Year Member



Hi,

tried but @ cannot be used to supress errors thrown by DOMDocument::load()

My tests show it can (using PHP 5.3.2), this version of Matthew1980's example works for me:

$doc = new DOMDocument();
if(![b]@[/b]$doc->load("http://url_of_some_xml_file")){
echo "Error opening xml file";
}


Not necessarily the best way of doing it though, you might be better of downloading first using something like cURL [php.net] and then checking the response code and then using the loadXML [php.net] method instead.

Andrew
 

Featured Threads

Hot Threads This Week

Hot Threads This Month