?
Solved

How to trap error in PHP

Posted on 2009-02-19
7
Medium Priority
?
431 Views
Last Modified: 2012-05-06
Hi,

In PHP, how can I trap the following error so that the web page keeps displaying the rest of the page after the error:

«Warning: DOMDocument::load() [function.DOMDocument-load]: Input is not proper UTF-8, indicate encoding ! Bytes: 0xC3 0x2E 0x2E 0x2E in http://www.cyberpresse.ca/rss/257.xml, line: 99 in /home/forum/domains/quebec.com/public_html/michel/test_erreur1_ok.php on line 48»

Thanks
0
Comment
Question by:scml
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 23685896
One option would be to use the error_reporting function http://us.php.net/error_reporting to disable warnings and show only errors or another combination (see options):

// Display only errors
error_reporting(E_ERROR);

0
 
LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23685919
Where can we view the error ? Give us a link to it
0
 

Author Comment

by:scml
ID: 23686273
Here is the link:

http://www.forum-quebec.com/
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:LordOfPorts
ID: 23697417
If you run the W3C validator at http://validator.w3.org/ for the source XML document you are using above http://www.cyberpresse.ca/rss/257.xml it displays an error, see:

http://validator.w3.org/check?uri=http%3A%2F%2Fwww.cyberpresse.ca%2Frss%2F257.xml&charset=(detect+automatically)&doctype=Inline&group=0

Essentially you need to contact the owner, unless that is you, and ask if it would be possible to correct the error specified by the validator. Once the source XML document is valid you should no longer get the error.
0
 

Author Comment

by:scml
ID: 23700213
Hi LordOfPorts,

Problem is the xml file is a rss feed from a newspaper. It changes daily so today for example, there's no error. When there's an error I wanted to be able to put a string like «Not availlable» instead of the error message and allow the script to continue past the error.
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 1000 total points
ID: 23701553
Hello scml,

In this case you are actually receiving not an error but a warning so the steps you could try would be to disable warnings as per my first post:

// Place at the beginning of the code
// Display only errors and no warnings
error_reporting(E_ERROR);

Then, in addition, check the return value of the DOMDocument::load http://us2.php.net/manual/en/domdocument.load.php function, it will return FALSE of failure, e.g. something like:

<?php
 
$doc = new DOMDocument();
 
if($doc->load('http://www.cyberpresse.ca/rss/257.xml') == true) {
    // Loading the file worked, proceed
}
else {
    echo '<<Not availlable>>';
}
 
?> 

Open in new window

0
 

Author Closing Comment

by:scml
ID: 31548965
Good job !
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question