在 PHP 中记录所有 Soap 请求和响应

Logging all Soap request and responses in PHP(在 PHP 中记录所有 Soap 请求和响应)
本文介绍了在 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 请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)