当PHP超时时运行一些东西



我有一个问题。我有web请求另一个web服务。但有时该web服务响应较慢。

我使用SOAP请求web服务。

我的问题是如何运行的东西(保存到DB)时超时(约30秒)

谢谢…

我已经尝试过register_shutdown_function,但函数仍然运行,即使它不是超时,…帮我. .

您可以尝试注册一个关机函数:

更新:现在应该只工作,当脚本超时。

function shutdown()
{
    $timedout = false;
    $errors = error_get_last();
    if( isset( $errors["type"] ) && $errors["type"] === E_ERROR ) {// E_ERROR => Fatal run-time errors.
        // if the code reaches this, it means, that a fatal error occurued
        // if you need to do this only when the script TIMEDOUT, you can check if message is "Maximum execution time....."
        $errorMessages = array( "Maximum execution time" );// check if this message is always the same on different PHP versions
        // just the solution for more than one specifif error messages to check
        foreach( $errorMessages as $errorMessage ) {
            // you maybe want to use, mb_strlen with the additional '8-bit' param
            // to avoid the mb.functions.overload
            $message = substr( $errors["message"], 0, strlen( $errorMessage ) );
            if( in_array( $message, $errorMessages ) ) {
                // timeout message occurred
                $timedout = true;
            }
        }
    }
    // we got a fatal error, probably a timeout
    if( $timedout ) {
        echo 'Script executed with success';
        echo "<pre>";
        var_dump( $errors );
    }
}
register_shutdown_function('shutdown');
//note: this below is just my test
set_time_limit( 2 );
while( true ) {
    // do nothing
}
我输出:

Fatal error: Maximum execution time of 2 seconds exceeded in some.php on line 11
Script executed with success

相关内容

  • 没有找到相关文章