在 PHP 中记录所有 Soap 请求和响应 [英] Logging all Soap request and responses in PHP

查看:25
本文介绍了在 PHP 中记录所有 Soap 请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用 PHP 中的内置 SoapClient 记录所有请求和响应?实际上,我可以使用 SoapClient::__getLastRequest()SoapClient::__getLastResponse() 手动记录所有内容,但是我们的系统中有很多我正在寻找的肥皂请求其他可能性.

Does anyone know how to log all request and responses with the builtin SoapClient in PHP? I could in fact manually log everything with SoapClient::__getLastRequest() and SoapClient::__getLastResponse() But we have that much soap requests in our system that i'm looking other possibilities.

注意:我使用的是 wsdl 模式,因此不能使用隧道到 SoapClient::__soapCall() 的方法

Note: i'm using wsdl mode so using a method that tunnels all through to SoapClient::__soapCall() isn't an option

推荐答案

我支持 Aleksanders 和 Stefans 的建议,但不会将 SoapClient 子类化.相反,我会将常规的 SoapClient 包装在一个装饰器中,因为日志记录不是 SoapClient 的直接关注点.此外,松散耦合让您可以轻松地用单元测试中的模拟替换 SoapClient,因此您可以专注于测试日志记录功能.如果您只想记录特定的调用,您可以添加一些逻辑,通过 $action 或您认为合适的任何内容过滤请求和响应.

I second Aleksanders and Stefans suggestion but would not subclass SoapClient. Instead I'd wrap the regular SoapClient in a decorator, because logging is not a direct concern of the SoapClient. In addition, the loose coupling lets you easily substitute the SoapClient with a mock in your UnitTests, so you can concentrate on testing the logging functionality. If you only want to log specific calls, you can add some logic that filters requests and responses by $action or anything you see fit.

编辑 由于 Stefan 建议添加一些代码,装饰器可能看起来像这样,尽管我不确定 __call() 方法(请参阅 Stefans 评论)

Edit since Stefan suggested to add some code, the decorator would probably look something like this, although I am not sure about the __call() method (see Stefans comments)

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

这篇关于在 PHP 中记录所有 Soap 请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆