【问题标题】:Is it possible to keep PHP script running even after HTTP 500 Internal Server Error?即使在 HTTP 500 内部服务器错误之后,是否可以保持 PHP 脚本运行?
【发布时间】:2016-02-17 10:22:00
【问题描述】:

在我的 PHP Web 应用程序中处理“HTTP 500 内部服务器错误”时遇到问题。
我的网络应用程序正在循环访问多个外部网络服务,如果其中任何一个出现错误,我的脚本将终止而不执行其他 URL。

以下是我正在尝试处理的代码 sn-p,我们将不胜感激。
如果有人想了解更多信息,请告诉我。

$arrURLs        = array('https://example1.com/abc1', 'https://example1.com/pqr2', 'https://example2.com/abc1', 'https://example2.com/pqr2');
$arrResponse    = array();
foreach( $arrURLs as $strURL ) {
    $soap_options = array('soap_version' => SOAP_1_1,
                            'debug'=>1,
                            'trace'=>1,
                            'exceptions'=>TRUE,
                            'cache_wsdl'=>WSDL_CACHE_NONE,
                            'connection_timeout'=> 500,
                            'stream_context' => stream_context_create(array('ssl' => array('verify_peer'=> false,'verify_peer_name'=>false,) ) ) );
    try{
        $client = new SoapClient( $strURL . '?wsdl', $soap_options );
        $client -> __setLocation( $strURL );
        $response = $client->method1();
        array_push( $arrResponse, $response );
    } catch(\Exception $e) {
        $strError = 'Exception while connecting to URL: ' . $strURL . 'Error Message: ' . $e->getMessage();
        echo $strError;
    }
}
// parse $arrResponse after getting from all web services;

谢谢。

【问题讨论】:

  • 为什么不捕获异常并重试请求?
  • “500”是脚本死机造成的。脚本死后无法继续。首先,您需要防止它死亡。检查错误日志以了解其终止的特定原因。
  • 我可以澄清一下吗?是您的服务器给出 500 还是您正在访问的其他服务器给出了 500 错误?
  • @JimWright:我正试图抓住它,但捕获打击没有被执行。 RiggsFolly:其他服务器。 deceze:他们有什么方法可以防止脚本死亡,因为我想为给定数组中的所有 URL 执行脚本。
  • 远程 API 中的错误应触发 SoapClient 中的异常。如果您无法捕获异常,您确定代码中没有发生错误吗?你有任何错误的示例堆栈跟踪吗?

标签: php web-services error-handling http-error httpexception


【解决方案1】:
try{
    $client = new SoapClient( $strURL . '?wsdl', $soap_options );
    $client -> __setLocation( $strURL );
    $response = $client->method1();
    array_push( $arrResponse, $response );
} catch(\SoapFault $soapfault) {
    $strError = 'SoapFault exception is caught for URL: ' . $strURL . 'Error Message: ' . $soapfault->getMessage();
    echo $strError;
    continue;
} catch(\Exception $e) {
    $strError = 'Exception while connecting to URL: ' . $strURL . 'Error Message: ' . $e->getMessage();
    echo $strError;
    continue;
} 

在 cakephp 中,您需要显式处理 SoapFault 异常。您可以通过将use SoapFault;catch(Exception $e) 放在use Cake\Core\Exception\Exception; 之前将use Cake\Core\Exception\Exception; 放在try catch 块之前使用catch(SoapFault $soapfault)

确保 $soap_options 包含 'exceptions'=>TRUE, 如果异常设置为 FALSE,您的异常将不会被捕获并且 脚本将在不捕获异常的情况下终止

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多