问题
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