PHP/Zend:如何强制浏览器不在网页上显示特定情况的警告?

发布于 2024-08-29 06:44:35 字数 711 浏览 4 评论 0原文

我正在尝试获取这样的 Twitter 更新:

try {

  $doc = new DOMDocument();
  $doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
  $isOK = true;

} catch( Zend_Exception $e ) {
  $isOK = false;
}

如果互联网连接没有问题,则 $isOK = true;已设置。但如果加载 Twitter 页面时出现问题,则会显示以下警告并且不会设置 $isOK = false;

警告:DOMDocument::load(http://twitter.com/statuses/user_timeline/ 1234567890.rss) [domdocument.load]: 无法打开流: HTTP 请求失败! HTTP/1.1 404 Not Found in /var/www/vcred/application/controllers/IndexController.php on line 120

我无论如何都不想在我的网页上看到上述警告。有什么想法吗?

谢谢

I am trying to get twitter updates like this:

try {

  $doc = new DOMDocument();
  $doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
  $isOK = true;

} catch( Zend_Exception $e ) {
  $isOK = false;
}

If there is not problem with internet connection then $isOK = true; is set. But if there is a problem in loading twitter page then it shows following warnings and does not set $isOK = false;

Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/1234567890.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/vcred/application/controllers/IndexController.php on line 120

I don't want to see above warning on my webpage in any case. Any idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-09-05 06:44:35

几个选项:

函数调用的所有错误

@$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');

抑制此 与抑制错误相同,

$oldLevel = error_reporting(0);
$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
error_reporting($oldLevel);

这种方式通常不受欢迎,因为它使代码更难调试。正如 Shrapnel 指出的那样,无论如何,您都希望禁止在生产系统上公开显示所有错误消息。在开发系统上,建议您使用 error_reporting(-1) ;,这将启用 E_ALLE_STRICT

如果你想使用 try/catch,您还可以更改默认错误处理并将所有错误转换为但这

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    throw new Exception($errstr, $errno);
}
set_error_handler("myErrorHandler");

是一个全局更改,会影响引发的所有内容。您还必须在代码中使用 catch(Exception $e) 而不是 Zend_Exception,但它会起作用。请注意,上面的内容会转换所有内容,甚至包括通知,因此如果您稍后尝试访问此内容,您还会收到有关 $isOk 未定义的异常。请随意根据您的喜好调整处理程序并查看 用户set_error_handler 的评论以获得更完善的版本

另一个全局更改是更改应用程序文件夹中的 application.ini,例如让 Zend Framework 控制错误处理:

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

根据您的需要更改这些。它们与 PHP.ini 中的相同,例如

display_errors

这决定了错误是否应该作为输出的一部分打印到屏幕上,或者是否应该对用户隐藏。

display_startup_errors

即使打开了 display_errors,也不会显示 PHP 启动序列期间发生的错误。强烈建议保持 display_startup_errors 关闭,调试除外。

Several options:

Suppress all errors for just this function call

@$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');

which is the same as

$oldLevel = error_reporting(0);
$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
error_reporting($oldLevel);

Suppressing errors this way is generally frowned upon, as it makes code harder to debug. Like Shrapnel pointed out, you want to disable public display of all error messages on a production system anyway. On Dev systems you are encouraged to use error_reporting(-1);, which would enable E_ALL and E_STRICT.

If you want to use try/catch, you can also change default error handling and convert all errors to exceptions by doing

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    throw new Exception($errstr, $errno);
}
set_error_handler("myErrorHandler");

This is a global change though and affects everything raised. You'd also have to use catch(Exception $e) instead of Zend_Exception then in your code, but it would work. Note that the above would convert everything, even Notices, so you will also get an Exception about $isOk being undefined if you are trying to access this later. Feel free to adapt the handler to your liking and check out the user comments for set_error_handler for more refined versions.

Another global change would be to change the application.ini in your application folder, e.g. letting Zend Framework control error handling:

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

Change these to your needs. They are the same as in PHP.ini, e.g.

display_errors:

This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

display_startup_errors:

Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed. It's strongly recommended to keep display_startup_errors off, except for debugging.

小鸟爱天空丶 2024-09-05 06:44:35
ini_set('display_errors',0);

它应该在任何生产站点上以这种方式设置。
根本不应该允许用户看到系统错误消息

ini_set('display_errors',0);

it should be set this way on any production site.
users shouldn't be allowed to see system error messages at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文