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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器