homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mask error - DOMDocument::load()
nil111




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

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

 

Matthew1980




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

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

nil111




msg:4139667
 4:51 am on May 26, 2010 (gmt 0)

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

nil111




msg:4139670
 4:57 am on May 26, 2010 (gmt 0)

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

Matthew1980




msg:4139776
 7:20 am on May 26, 2010 (gmt 0)

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

nil111




msg:4139826
 8:28 am on May 26, 2010 (gmt 0)

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

Matthew1980




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

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

nil111




msg:4139948
 11:03 am on May 26, 2010 (gmt 0)

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!

Little_G




msg:4143718
 10:14 pm on May 29, 2010 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved