Recipe 14.3. Calling a SOAP Method Without WSDL


14.3.1. Problem

You want to send a SOAP request to a service that does not expose a WSDL file, so you must specify the information usually provided there yourself.

14.3.2. Solution

Pass a null value for the location of the WSDL, and the main service settings, such as location and namespace URI, in the options array:

<?php $opts = array('location' => 'http://64.124.140.30:9090/soap',               'uri'      => 'urn:xmethods-delayed-quotes', $client = new SOAPClient(null, $opts); ?>

Make requests using the __soapCall( ) method, passing the method name as the first parameter, and an array of method arguments as the second:

$quote = $client->__soapCall('getQuote', array('EBAY')); // eBay, Inc. print $quote; 31.49

14.3.3. Discussion

Since you're not using WSDL, pass null as the first argument to SOAPClient. This tells the SOAP extension that you're passing the details about the web service in the second parameter of options.

This information is stored as an array. At a minimum, you must provide two entries: the URL where the SOAP server is located and the namespace URI that identifies the service. For example:

<?php $opts = array('location' => 'http://64.124.140.30:9090/soap',               'uri'      => 'urn:xmethods-delayed-quotes', $client = new SOAPClient(null, $opts); ?>

The server's URL is the location element; here, the server is at http://64.124.140.30:9090/soap. The server's namespace is set using the uri element. This is urn:xmethods-delayed-quotes.

Now you have a SOAP client, but with a non-WSDL-based client, you can't directly invoke SOAP methods on the $client object. Instead, you reference the __soapCall( ) method, passing the method name as your first argument and an array of parameters as the second:

<?php $quote = $client->__soapCall('getQuote', array('EBAY')); // eBay, Inc. print $quote; ?> 31.49

Since the SOAP client no longer knows how many parameters to expect, you must bundle your parameters to __soapCall( ) inside of an array. Therefore, the stock quote is now passed as array('EBAY') instead of 'EBAY'.

This code is more complex than the WSDL solution, and it even takes advantage of some default SOAP settings assumed by SOAPClient. This interface for calling SOAP methods is also less elegant.

However, this is the only way to pass or read additional information, such as SOAP headers.

14.3.4. See Also

14.2 for making SOAP requests with WSDL; Recipe 15.2 for more on SOAP servers; the ext/soap documentation at http://www.php.net/soap; Programming Web Services with SOAP, by Doug Tidwell, James Snell, and Pavel Kulchenko (O'Reilly).




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

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