dpxo13079 2014-04-11 13:32
浏览 288
已采纳

主机无法访问时fsockopen导致致命错误 - “无法连接到”

When I call fsockopen to an unreachable IP address, PHP stops execution with FATAL ERROR, as it is intended to do. But it's normal situation for me, that a host is unreachable. Is there a way, how to prevent stopping the php even in case of the FATAL ERROR, if it is the intended behavior?

I don't use any framework, I handle all errors myself. I registers the handlers like this:

set_error_handler("errorHandler");
register_shutdown_function("fatalErrorHandler");

and the handlers are defined like this:

function fatalErrorHandler() 
{
    $error = error_get_last();

    if( $error !== NULL) {
      $errno   = $error["type"];
      $errfile = $error["file"];
      $errline = $error["line"];
      $errstr  = $error["message"];

      errorHandler($errno, "FATAL: " . $errstr, $errfile, $errline,get_defined_vars(),debug_backtrace());
    }
} 

function errorHandler($errno, $errstr, $errfile, $errline,$vars,$trace="")
{

   // some formatting and checking

   file_put_contents(dirname(__FILE__) . "/error/" . gmdate("YmdHis") . str_replace(".","",microtime(true)) . ".err"
  ,"<error_log_date>" . gmdate("YmdHis") . "</error_log_date><error_log_uid>{$uid}</error_log_uid>
   <error_log_str>{$errstr}</error_log_str>
   <error_log_file>{$errfile}</error_log_file>
   <error_log_line>{$errline}</error_log_line>
   <error_log_vars>{$vars}</error_log_vars>
   <error_log_trace>{$trace}</error_log_trace>");


 return true;
}
  • 写回答

1条回答 默认 最新

  • douchu4048 2014-04-11 13:37
    关注

    Final answer after the question was edited

    You are not aware of the fact that shutdown function is called at the end of the script regardless of whether errors happened or not. And even if it was just a warning which did not stop the script immediately error_get_last() will return it. Further you are just putting FATAL:, hardcoded in front of the message. You should handle different types of errors, warnings and notices here.

    The problem is the shutdown function, not fsockopen(). The shutdown function should look like this:

    register_shutdown_function(function() {
        $error = error_get_last();
        if($error && $error['type'] === E_ERROR) {
    
            $errno   = $error["type"];
            $errfile = $error["file"];
            $errline = $error["line"];
            $errstr  = $error["message"];
    
            errorHandler($errno, "FATAL: " . $errstr, $errfile, $errline /* , ... */);
        }   
    });
    

    Original answer

    You told that PHP stops execution with FATAL ERROR.

    This is not true. fsockopen will return false and throw a warning(!) in case of error:

    // sorry example.com ;)
    var_dump(fsockopen("www.example.com", 1000, $errno, $errstr, 3));
    

    PHP Warning: fsockopen(): unable to connect to www.example.com:1000 (Connection timed out) in /home/thorsten/src/checkout-plugin/a.php on line 3 PHP Stack trace: PHP 1. {main}() /home/thorsten/src/checkout-plugin/a.php:0 PHP 2. fsockopen() /home/thorsten/src/checkout-plugin/a.php:3 bool(false)

    If you get a fatal error, this can be caused by a global error handler which has been registered using set_error_handler() and turns warnings into exceptions. Some frameworks are doing so. If this is true, you can use the "silence" operator @ to suppress the warning:

    var_dump(@fsockopen("www.example.com", 1000, $errno, $errstr, 3));
    // bool(false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料