PHP/Zend:如何强制浏览器不在网页上显示特定情况的警告?
我正在尝试获取这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几个选项:
函数调用的所有错误
抑制此 与抑制错误相同,
这种方式通常不受欢迎,因为它使代码更难调试。正如 Shrapnel 指出的那样,无论如何,您都希望禁止在生产系统上公开显示所有错误消息。在开发系统上,建议您使用
error_reporting(-1) ;
,这将启用E_ALL
和E_STRICT
。如果你想使用
try/catch
,您还可以更改默认错误处理并将所有错误转换为但这是一个全局更改,会影响引发的所有内容。您还必须在代码中使用
catch(Exception $e)
而不是Zend_Exception
,但它会起作用。请注意,上面的内容会转换所有内容,甚至包括通知,因此如果您稍后尝试访问此内容,您还会收到有关$isOk
未定义的异常。请随意根据您的喜好调整处理程序并查看 用户set_error_handler
的评论以获得更完善的版本。另一个全局更改是更改应用程序文件夹中的 application.ini,例如让 Zend Framework 控制错误处理:
根据您的需要更改这些。它们与 PHP.ini 中的相同,例如
display_errors :
display_startup_errors:
Several options:
Suppress all errors for just this function call
which is the same as
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 enableE_ALL
andE_STRICT
.If you want to use
try/catch
, you can also change default error handling and convert all errors to exceptions by doingThis is a global change though and affects everything raised. You'd also have to use
catch(Exception $e)
instead ofZend_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 forset_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:
Change these to your needs. They are the same as in PHP.ini, e.g.
display_errors:
display_startup_errors:
它应该在任何生产站点上以这种方式设置。
根本不应该允许用户看到系统错误消息
it should be set this way on any production site.
users shouldn't be allowed to see system error messages at all