Using Charles To Debug PHP SOAP
SoapClient
has the ability to enable tracing and give information about the request/response headers/body, which is useful, but I needed to store the outputs and also rewrite a particular request header during the course of debugging this. Enter: Charles Proxy.
I wrote about Charles at length on TechPortal recently, so if you’re new to Charles that would be a good place to learn more. I set up Charles to proxy requests on port 8888, and I was running the PHP scripts on the same machine. To set the proxy, I simply added some entries to the $options
array that gets passed to the SoapClient
constructor:
$options = array( "cache_wsdl" => WSDL_CACHE_NONE, "soap_version" => SOAP_1_1, "trace" => 1, "proxy_host" => "localhost", "proxy_port" => 8888, ); $client = new SoapClient($wsdl, $options);
The specific bits that are relevant to proxying through Charles here are the proxy_host
and proxy_port
settings, this sends these SOAP requests through port 8888 on my local machine, which is where Charles is listening for proxy activity. This means that I can easily keep one eye on the detail of the request without having large headers or SOAP responses all over my output or in the logs. As I mentioned earlier, there was also a moment where we thought that PHP was sending some incorrect headers (it wasn’t) and I was able to use Charles to rewrite just those headers, which helped us to see that this wasn’t the problem. You can also set a stream_context
option to set any specific HTTP-related settings you need to, but some headers such as Content-Type
will be ignored as the SOAP extension has clear ideas on which to send.
I hope that helps someone working with a remote SOAP service, they aren’t always easy to debug but this is one of many tools in my box so I thought I’d share.
Another little known tool I’m fond of (having written it ;-) ) for debugging webservices: http://projects.ez.no/ggwebservices/
It probably is a bit overkill to have to install a full-fledged CMS just to get it, but I thought some users might find it useful
I can’t tell you how thankful I am for Charles, it has saved my bacon more than once. Thank you for your work, great product!
I’ve used Charles for debugging data sent directly from/to the client, but never this way. Great thinking! I imagine it to be used for other sorts of server calls as well. Thanks for the tip!
Good Thinking and very creative use
Pingback: Debugging PHP SOAP over SSL using Charles | Rob Allen
[code]SoapClient::__getLastRequest[/code] and [code]SoapClient::__getLastResponse[/code] comes in handy too :)
In this case, SoapClient::__getLastRequestHeaders as well I suppose.