Recipe 14.2. Calling a SOAP Method with WSDL


14.2.1. Problem

You want to send a SOAP request. Creating a SOAP client allows you to gather information from SOAP servers, regardless of their operating system and middleware software.

14.2.2. Solution

Use the ext/soap extension. Here's client code that finds current stock quotes:

<?php $wsdl_url =     'http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl'; $client = new SOAPClient($wsdl_url); $quote = $client->getQuote('EBAY'); // eBay, Inc. print $quote; ?> 31.49

14.2.3. Discussion

There are a handful of SOAP implementations for PHP. If you're using PHP 5, it's highly recommended to use the bundled ext/soap extension. While this extension is not compatible with PHP 4, it has many advantages over PEAR::SOAP and NuSOAP, the two main PHP SOAP extensions that are PHP 4 compatible. In particular, ext/soap is:

  • Written in C, not PHP, so it's fast and efficient.

  • Bundled with PHP as of PHP 5, and enabled by default as of PHP 5.1.

  • Compatible with many parts of the SOAP specifications.

  • Written to take advantage of PHP 5 features, including exceptions.

To make a SOAP request, you instantiate a new SOAPClient object and pass the constructor the location of the web services' WSDL:

$client = new SOAPClient('http://api.example.com/service.wsdl');

WSDL (Web Services Description Language) is an XML vocabulary that lets the implementor create a file that defines what methods and arguments his web service supports. This file is then placed on the Web for others to read.

WSDL is not particularly friendly for humans, but it's great for machines. When you point the SOAP extension at a WSDL file, the extension automatically creates an object for the web service, and you can manipulate this object as you would a PHP class.

The object even knows what parameters each method takes and each parameter's type. This is important because, unlike PHP, SOAP is strictly typed. You cannot provide a string 1 when SOAP wants an integer 1. WSDL allows the SOAP extension to coerce PHP variables into the appropriate types without any action on your part.

Therefore, whenever possible, you want to know the location of the server's WSDL file. This makes it much easier to make SOAP requests. Example 14-1 shows how to make a query using WSDL.

SOAP client using WSDL

<?php $wsdl_url =     'http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl'; $client = new SOAPClient($wsdl_url); $quote = $client->getQuote('EBAY'); // eBay, Inc. print $quote; ?> 31.49

From XMethods's web site, you know that the WSDL file for this service is at http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl.

You now instantiate a new SOAPClient object by passing $wsdl_url, the location of the WSDL file, to the constructor. This returns a client object, $client, that you use to make SOAP requests.

The constructor creates a SOAP client, but you still need to make the actual query itself, which is called getQuote( ). It takes one argument, the stock ticker. Pass your stock ticker, in this case EBAY, directly to the method.

When you call $client->getQuote(10001), the SOAP extension converts the PHP string EBAY to a SOAP message written in XML and sends an HTTP request to the XMethods server. After XMethods receives and processes your query, it replies with a SOAP message of its own. The SOAP extension listens for this response and parses the XML into a PHP object, which is then returned by the method and stored in $quote.

The $quote variable now holds the current stock price of the EBAY stock. Right now it's trading at $31.49 a share.

14.2.4. See Also

Recipe 14.3 for making SOAP requests without 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

Similar book on Amazon

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