This article describes the new SOAP extension for PHP. It is intended for PHP developers who want to write their own Web Services servers, or use SOAP to access existing ones. It assumes some familiarity with Web Services, SOAP, and WSDL (Web Services Description Language).
A modified version of our SOAP server and client follows:
Example 6 (server2.php)
<?php class QuoteService { private $quotes = array("ibm" => 98.42); function getQuote($symbol) { if (isset($this->quotes[$symbol])) { return $this->quotes[$symbol]; } else { throw new SoapFault("Server","Unknown Symbol '$symbol'."); } } } $server = new SoapServer("stockquote2.wsdl"); $server->setClass("QuoteService"); $server->handle(); ? > As you can see, I have used the SoapServer::setClass() method to connect the SoapServer object with the QuoteService class.
What’s inside? Are you curious about the SOAP message format, or hoping to debug a SOAP client of your own? If so, this section is for you. The SoapClient() constructor accepts an associative array as its second parameter, as you already saw in the first example. Various options can be passed through this associative array. Here are just two:
trace – allows the client to store SOAP requests and responses (turned off by default)
exceptions – allows the client to control the exception mechanism (turned on by default)
Take a look at the following SOAP client example. It is derived from example 5, and shows precisely what is transmitted between the client and the server. In order to retrieve this information we use the SoapClient methods __getLastRequest() and __getLastResponse().