Enterprise Integration Zone is brought to you in partnership with:

Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with phpwomen.org and her local user group PHP North West, and writes for a variety of outlets, including her own blog at lornajane.net. She is an active member of the PHP and open source communities and contributes to the joind.in event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 79 posts at DZone. You can read more from them at their website. View Full User Profile

Using 'Charles Proxy' To Debug PHP SOAP

07.22.2013
| 2719 views |
  • submit to reddit

I used this trick to solve a particularly sticky problem last week, so I thought I'd share. I had a 3rd party SOAP service (except it was actually a .NET WCF service, which is almost SOAP but not quite) which didn't behave in accordance with the way its documentation said it would. PHP's 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.

Published at DZone with permission of Lorna Mitchell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)