douxi4114 2010-04-16 07:26 采纳率: 100%
浏览 43
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dsfvsdfv23599 2010-04-16 08:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里