Recipe 14.4. Debugging SOAP Requests


14.4.1. Problem

Your SOAP request is not working as expected, but you're not sure why.

14.4.2. Solution

Enable the trace option when you create the SOAPClient:

<?php $opts = array('trace' => true); $client = new SOAPClient($wsdl_url, $opts); ?>

Now you can access data sent across the wire:

<?php $response = $client->getQuote('EBAY'); // going... print $client->__getRequestHeaders() . "\n"; print $client->__getRequest() . "\n"; // and coming... print $client->__getReponseHeaders() . "\n"; print $client->__getRequest() . "\n"; ?>

14.4.3. Discussion

SOAP requests can be difficult to debug. When all else fails, it can be necessary to visually inspect the actual HTTP and XML data being sent back and across forth the wire.

This can be tricky when the data is secured over SSL or you don't control the server. In these cases, it's easiest to ask the ext/soap extension to give you a complete accounting of everything sent and received.

First, you must enable the TRace option. This tells the extension to store this information for later retrieval:

<?php $opts = array('trace' => true); $client = new SOAPClient($wsdl_url, $opts); ?>

Now whenever you make a request, the most recent request data is available through four functions: two handle the outgoing request from PHP, and another handling the incoming response from the server:

<?php $response = $client->getQuote('EBAY'); // going... print $client->__getRequestHeaders() . "\n"; print $client->__getRequest() . "\n"; // and coming... print $client->__getReponseHeaders() . "\n"; print $client->__getRequest() . "\n"; ?>

Now you can inspect the data to see what appears to be the problem. Usually, the SOAP envelope contains the wrong XML. It's well-formed; it's just not laid out how the SOAP server expects it.

At this level, it can be very helpful to have a few sample SOAP requests and replies that you know are valid. You can then attempt to reconstruct those requests using ext/soap one section at a time.

Debugging at this level can require an understanding of HTTP, XML, XML namespaces, XML Schema, SOAP, and WSDL. In particular, you may end up with two XML documents that are semantically equivalent, but appear different due to XML namespaces, prefixes, and default namespaces. If you are absolutely positive two documents are similar, but one is not working, you may want to use cURL to explicitly post the two different XML files to the server.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net