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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向