PHP Script to Display SOAP Requests and Responses

Posted on March 5, 2013

Some time back, while working out web services on another project, I put together a janky little snippet of PHP code that’s actually pretty handy when dealing with SOAP-based web services.  For me, the hardest part of working with web services is trying to figure out exactly what the request needs to look like to work correctly.  The other thing is figuring out exactly what I got back!  This little snippet actually shows you what the xml-formatted SOAP request and SOAP response each look like.  The REALLY nice thing is that it only takes 4 lines of code.

Here’s what it looks like:

[code]
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
[/code]

Basically, all you do is plug this in after you’ve created your new SoapClient() object and performed the soapCall().  A basic example would look like the code below.  Please note, this isn’t a working piece of code, just an illustration of how it would look.

[code]
$client = new SoapClient(
 some client specifics here
);
$SomeSoap = $client->__soapCall(
  do some SOAP function
);
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";

[code]

What this should get you, when you run the script, is two separate, preformatted lines of text.  The first line will be the XML-formatted request that PHP sent to the web service.  The second line should be the XML-formatted response returned.  Granted, this doesn’t necessarily give you the final format that you need, but it will get you names of all of the XML variables that have been returned.  With that information, you can proceed to find a way to extract the information that you need.

Stay tuned for more on this subject.

Jeff Eske

Leave a Comment

If you would like to make a comment, please fill out the form below.

You must be logged in to post a comment.

© Copyright Jeff's Blog • Powered by Wordpress • Using Detour theme created by Brian Gardner.