How to dump SoapClient request for debug?
Solution 1:
These functions only works if the SoapClient object was created with the trace option set to TRUE.
Try:
$client = new SoapClient("http://www.webservicex.net/ConverPower.asmx?WSDL", array('trace' => 1));
Solution 2:
Debug a SOAP request
- Using a SOAP extension
The easiest and best* way to debug a SOAP request is indeed to create a SOAP extension that logs the raw SOAP request and the raw SOAP response from the Web service or Web service client using the following functions of the SoapClient class:
- SoapClient::__getLastRequestHeaders
- SoapClient::__getLastRequest
- SoapClient::__getLastResponseHeaders
- SoapClient::__getLastResponse
To make it work, you do have to create your SoapClient object with the trace option turned on, as mentioned by xdazz:
$client = new MySoapClient($wsdlUrl, array('trace' => 1));
and then run your SOAP calls wrapped in a try-catch block:
try{
$result = $client->__SoapCall('routeCase', $params);
}catch (\Exception $e){
throw new \Exception("Soup request failed! Response: ".$client->__getLastResponse());
}
When developing SOAP solutions in PHP it is also a good idea to clean the PHP tmp folder when your WSDL contract changes (see your tmp
folder path in phpinfo()
) to force the PHP SoapClient to download WSDL and XSD files again instead of using the cached files (until they expire).
Furthermore, it's useful to set options like exceptions
and cache_wsdl
and the soap_version
version whilst developing:
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>false,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
*The downside of debugging using a SOAP extension is that certificate-errors happen before the actual request or something. So it's not possible to use getLastRequest() or getLastResponse() when there is a connection failure of some kind.
- Using Xdebug
Another interesting option to debug a SoapClient is setting a debug session cookie for Xdebug and your favorite IDE
$client = new SoapClient(
'http://example.loc/index.php/api/v2_soap/?wsdl'
);
$client->__setCookie('XDEBUG_SESSION', 'NETBEANS');
- Using specialized SOAP Tracer & Debugger
Specialized SOAP Trace & Debug apps are very useful too: In addition to usual suspects like SoapUI, there are also intermediate tracing proxies like Charles as described here. The downside of this method is that more layers are added and therefore are likely to introduce new problems, e.g. handshake problems.
There are also some commercial SOAP Debuggers out that are worth the money, like the XML Spy SOAP Debugger or SOAPSonar which do validation and invocation. In any event, SoapUI is always a good companion.
If you suspect there is an issue on the network protocol level try Wireshark, a network protocol analyzer for Unix and Windows.
Logs, logs, logs
Basic error information can also be found in the PHP log and the web server log. Ensure you have turned on full error logging.
Solution 3:
I just wrap it up for action:
$client = new \SoapClient("http://127.0.0.1/services/wsdl2",array('trace' => 1,));
try{
$result = $client->__SoapCall('routeCase', $params);
}catch (\Exception $e){
throw new \Exception("Soup Request Failed! Response:\n".$client->__getLastResponse());
}