How can I intercept the HTTP request of a PHP Soap Client?

与世无争的帅哥 提交于 2019-12-24 07:28:31

问题


I have implemented a SoapClient in PHP and I need to debug calls made to the Soap server. I would like to intercept the HTTP calls when I run my PHP code in order to retrieve the body content.

Is this possible? How can I achieve this? I am under Linux.


回答1:


Extend the SoapClient class and redefine the method __doRequest(). This is where the HTTP request is sent to the server. If you are happy with the default implementation, all you have to do is to log the values it receives as arguments, call the parent implementation, log the value it returns and also return it. Use the new class instead of SoapClient whenever you need to log the communication between the SOAP client and server.

Something along these lines:

class MySoapClient extends SoapClient
{
    public string __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        // Log the request here
        echo('$action='.$action."\n");
        echo('$request=[[['.$request."]]]\n");

        // Let the parent class do the job
        $response = parent::__doRequest($request, $location, $action, $version, $one_way);

        // Log the response received from the server
        echo('$response=[[['.$response."]]]\n");

        // Return the response to be parsed
        return $response;
    }
}

Use the log method of your choice instead of echo() in the code above.



来源:https://stackoverflow.com/questions/43050788/how-can-i-intercept-the-http-request-of-a-php-soap-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!