0

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

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

$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;

谢谢。

4

1 回答 1

0
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 异常。您可以使用catch(SoapFault $soapfault)by putuse SoapFault;catch(Exception $e)by put use Cake\Core\Exception\Exception;before try catchblock。

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

于 2016-02-17T11:22:14.070 回答