In this conclusion to a five-part article series on Web Services, you'll learn how to create a SOAP server, add server functions, and more. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
You can make a function available to clients by exporting it using the addFunction() method. In the WSDL file, there is only one function to implement, getQuote(). It takes $boxer as a lone parameter and returns a string. Let’s create this function and expose it to connecting clients:
<?php function getQuote($boxer) { if ($boxer == "Tyson") { $quote = "My main objective is to be professional but to kill him. (2002)"; } elseif ($boxer == "Ali") { $quote = "I am the greatest. (1962)"; } elseif ($boxer == "Foreman") { $quote = "Generally when there's a lot of smoke, there's just a whole lot more smoke. (1995)"; } else { $quote = "Sorry, $boxer was not found."; } return $quote; }
$soapserver = new SoapServer("boxing.wsdl");
$soapserver->addFunction("getQuote"); ?>
When two or more functions are defined in the WSDL file, you can choose which ones are to be exported by passing them in as an array, like so:
Alternatively, if you would like to export all functions defined in the scope of the SOAP server, you can pass in the constantSOAP_FUNCTIONS_ALL, like so:
It’s important to understand that exporting the functions is not all that you need to do to produce a valid SOAP server. You also need to properly process incoming SOAP requests, a task handled for you via the methodhandle(). This method is introduced next.