PHP SOAP Extension - Example 6 (Page 6 of 8 )
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.
Example 7 (client4.php)
<?php
$client = new SoapClient("stockquote2.wsdl");
try {
echo "<pre>n";
print($client->getQuote("ibm"));
echo "n";
print($client->getQuote("microsoft"));
echo "n</pre>n";
} catch (SoapFault $exception) {
echo $exception;
}
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().
Next: Example 8 (client5.php) >>
More Zend Articles
More By Zend