有谁知道如何使用
PHP内置的SoapClient记录所有请求和响应?事实上,我可以使用SoapClient :: __ getLastRequest()和SoapClient :: __ getLastResponse()手动记录所有内容但是我们的系统中有很多肥皂请求,我正在寻找其他可能性.
注意:我正在使用wsdl模式,所以使用隧道全部通过SoapClient :: __ soapCall()的方法不是一个选项
我是第二个Aleksanders和Stefans的建议,但不会将SoapClient子类化.相反,我会将常规SoapClient包装在装饰器中,因为日志记录不是SoapClient的直接关注点.此外,松散耦合使您可以在UnitTests中使用模拟轻松替换SoapClient,因此您可以专注于测试日志记录功能.如果您只想记录特定的调用,可以添加一些逻辑,通过$action或您认为合适的任何内容来过滤请求和响应.
编辑自Stefan建议添加一些代码后,装饰器可能看起来像这样,虽然我不确定__call()方法(参见Stefans注释)
class SoapClientLogger
{
protected $soapClient;
// wrapping the SoapClient instance with the decorator
public function __construct(SoapClient $client)
{
$this->soapClient = $client;
}
// Overloading __doRequest with your logging code
function __doRequest($request,$location,$action,$version,$one_way = 0)
{
$this->log($request,$version);
$response = $this->soapClient->__doRequest($request,$one_way);
$this->log($response,$version);
return $response;
}
public function log($request,$version)
{
// here you Could add filterings to log only items,e.g.
if($action === 'foo') {
// code to log item
}
}
// route all other method calls directly to soapClient
public function __call($method,$args)
{
// you Could also add method_exists check here
return call_user_func_array(array($this->soapClient,$method),$args);
}
}