【发布时间】:2018-08-17 04:36:12
【问题描述】:
我在处理我的 php soap 服务器上的错误和异常时遇到了困难。有时错误/异常会被我的错误/异常处理程序捕获,有时不会。它一直让我发疯,但现在我已经能够创建一个说明这个问题的小例子。希望有人能够理解这种行为并给出一些建议。
考虑以下 php 文件,运行一个肥皂服务器:
<?php
set_exception_handler(function(Throwable $Exception) {
file_put_contents(__DIR__.'/exception.txt', 'Uncaught exception: '.$Exception->getMessage());
});
//throw new Exception('qwer'); // (1) Invokes the exception handler
//throw new Error('asdf'); // (2) Invokes the exception handler
function SoapFunction() {
// throw new Exception('bar'); // (3) Invokes the exception handler
// throw new Error('foo'); // (4) Does not invoke the exception handler!
}
$Server = new SoapServer(null, ['uri' => 'MyNamespace']);
$Server->addFunction('SoapFunction');
ob_start();
$Server->handle();
file_put_contents(__DIR__.'/response.txt', ob_get_flush());
有四行抛出异常被注释掉。
通过向soap服务器发送请求,将创建文件response.txt,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="MyNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:SoapFunctionResponse><return xsi:nil="true"/></ns1:SoapFunctionResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
通过取消注释第一个异常(1)并向soap服务器发送请求,将创建文件exception.txt,其内容如下:
Uncaught exception: qwer
通过取消注释第二个异常(2)并向soap服务器发送请求,将创建文件exception.txt,其内容如下:
Uncaught exception: asdf
通过取消注释第三个异常(3)并向soap服务器发送请求,将创建文件exception.txt,其内容如下:
Uncaught exception: bar
通过取消注释第四个异常(4) 并向soap 服务器发送请求,文件exception.txt 将不会被创建!但是文件response.txt会被创建,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>foo</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
奇怪的是第四个异常从未到达异常处理程序。相反,异常消息作为soap 故障发送给客户端。谁能明白为什么?我正在使用 PHP 7.1.11。
对于那些想尝试的人,这里有一个简单的 php soap 客户端。只需更改http://www.example.com/server.php 使其指向上述soap 服务器php 文件。
<?php
$Client = new SoapClient(null, ['uri' => 'MyNamespace', 'location' => 'http://www.example.com/server.php']);
$Client->__soapCall('SoapFunction', []);
【问题讨论】:
-
我会看看使用 Zend-SOAP,因为它有更好的异常处理
标签: php web-services soap error-handling exception-handling