php 打印soap日志,在PHP中记录所有Soap请求和响应
我是第二个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, $location, $action, $version);
$response = $this->soapClient->__doRequest($request, $location,
$action, $version,
$one_way);
$this->log($response, $location, $action, $version);
return $response;
}
public function log($request, $location, $action, $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);
}
}