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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值