how to catch the execption of DOMDocument::load()
how to catch the execption of DOMDocument::load()
am 09.01.2008 11:00:10 von sunnyboyGuo
Deal all,
When I use DOMDocument::load function to load one xml file, if this
file is not found, I want to give the 500 status to my client program
(not internet explorer).
But the load function will give some warning message and then I can
not rewrite the header because headers already sent.
How can I catch this exception and use header function to write header
information?
The code is as follows:
$DOC = new DOMDocument();
if ($DOC->load($some_file))
{
do something.
} else {
Header("http/1.1 500 Internal Server Error");
return;
}
thank you very much.
Best wishes,
Tony
Re: how to catch the execption of DOMDocument::load()
am 09.01.2008 11:16:46 von Janwillem Borleffs
sunnyboyGuo@gmail.com schreef:
> How can I catch this exception and use header function to write header
> information?
>
Which PHP version are you using? With PHP5 you can use try/catch:
try {
// do what you want to do
} catch (Exception $ex) {
Header("http/1.1 500 Internal Server Error");
}
With PHP4, when the error is non-fatal, you can surpress the error
message by prepending an at sign (@) before the functional call.
JW
Re: how to catch the execption of DOMDocument::load()
am 09.01.2008 13:30:01 von sunnyboyGuo
On Jan 9, 6:16=A0pm, Janwillem Borleffs wrote:
> sunnyboy...@gmail.com schreef:
>
> > How can I catch this exception and use header function to write header
> > information?
>
> Which PHP version are you using? With PHP5 you can use try/catch:
>
> try {
> =A0 =A0// do what you want to do} catch (Exception $ex) {
>
> =A0 =A0Header("http/1.1 500 Internal Server Error");
>
> }
>
> With PHP4, when the error is non-fatal, you can surpress the error
> message by prepending an at sign (@) before the functional call.
>
> JW
sorry, my environment is php5.0.
when there occured some error, it will send headers automatically, so
i can not use header() to send expected header information.
it will prompt message like this:
Warning: Cannot modify header information - headers already
sent by (output started at /var/www/tpms.php:155) in /var/www/
tpms.php on line 25
can i control it not send automatically, and to send myself header
information?
best regards,
Tony
Re: how to catch the execption of DOMDocument::load()
am 09.01.2008 13:40:40 von Janwillem Borleffs
sunnyboyGuo@gmail.com schreef:
> when there occured some error, it will send headers automatically, so
> i can not use header() to send expected header information.
> it will prompt message like this:
>
> Warning: Cannot modify header information - headers already
> sent by (output started at /var/www/tpms.php:155) in /var/www/
> tpms.php on line 25
>
> can i control it not send automatically, and to send myself header
> information?
>
You can use the PHP4 method to surpress the error (with the @-sign) or
alter the error_reporting setting (ini file or through the
error_reporting function) to E_FATAL (or one of the other possible
values/combinations; see http://www.php.net/error_reporting for details).
You can also disable the display_errors directive in your php.ini file.
JW
Re: how to catch the execption of DOMDocument::load()
am 09.01.2008 14:11:30 von sunnyboyGuo
On Jan 9, 8:40=A0pm, Janwillem Borleffs wrote:
> sunnyboy...@gmail.com schreef:
>
> > when there occured some error, it will send headers automatically, so
> > i can not use header() to send expected header information.
> > it will prompt message like this:
> >
> > Warning: =A0Cannot modify header information - headers already
> > sent by (output started at /var/www/tpms.php:155) in /var/www/
> > tpms.php on line 25
>
> > can i control it not send automatically, and to send myself header
> > information?
>
> You can use the PHP4 method to surpress the error (with the @-sign) or
> alter the error_reporting setting (ini file or through the
> error_reporting function) to E_FATAL (or one of the other possible
> values/combinations; seehttp://www.php.net/error_reportingfor details).
>
> You can also disable the display_errors directive in your php.ini file.
>
> JW
Hi JW,
maybe i did not describe clearly. i want to surpress php to send
header automaticlly, but send
header by myself.
when i use error_reporting(0) to surpress errors, it can surpress
them, but the function call header(HTTP/1.1 400 Not FOUND) didn't
rewrite the header. i think php may send header before the call, but
its warning was surpressed.
Best regards,
Tony
Re: how to catch the execption of DOMDocument::load()
am 09.01.2008 14:14:52 von Janwillem Borleffs
sunnyboyGuo@gmail.com schreef:
> maybe i did not describe clearly. i want to surpress php to send
> header automaticlly, but send
> header by myself.
> when i use error_reporting(0) to surpress errors, it can surpress
> them, but the function call header(HTTP/1.1 400 Not FOUND) didn't
> rewrite the header. i think php may send header before the call, but
> its warning was surpressed.
>
If you surpress the error, PHP doesn't send anything, unless there is
some whitespace or other characters printed by the script.
Try sending the header prior to the call and see what it produces.
JW
Re: how to catch the execption of DOMDocument::load()
am 10.01.2008 04:10:26 von sunnyboyGuo
On Jan 9, 9:14=A0pm, Janwillem Borleffs wrote:
> sunnyboy...@gmail.com schreef:
>
> > maybe i did not describe clearly. i want to surpress php to send
> > header automaticlly, but send
> > header by myself.
> > when i use error_reporting(0) to surpress errors, it can surpress
> > them, but the function call header(HTTP/1.1 400 Not FOUND) didn't
> > rewrite the header. i think php may send header before the call, but
> > its warning was surpressed.
>
> If you surpress the error, PHP doesn't send anything, unless there is
> some whitespace or other characters printed by the script.
>
> Try sending the header prior to the call and see what it produces.
>
> JW
Hi JW,
thank you very much. i called echo() before the header(). now it
work well after removing it.
Best regards,
Tony