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).
Now that you’re familiar with the basic premises of using this extension to create both SOAP clients and servers, this section presents an example that simultaneously demonstrates both concepts. This SOAP service retrieves a famous quote from a particular boxer, and that boxer’s last name is requested using the exposedgetQuote()method. It’s based on theboxing.wsdlfile shown earlier in Listing 20-5. Let’s start with the server.
Creating the Boxing Server
The boxing server is simple but practical. Extending this to connect to a database server would be a trivial affair. Let’s consider the code:
<?php class boxingQuotes { 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; } }
The client, introduced next, will consume this service.
Executing the Boxing Client
The boxing client consists of just two lines, the first instantiating the WSDL-enabled SoapClient() class, and the second executing the exposed method getQuote(), passing in the parameter "Ali":
<?php $client = new SoapClient("boxing.wsdl"); echo $client->getQuote("Ali"); ?>
Executing the client produces the following output:
-------------------------------------------- I am the greatest. (1962) --------------------------------------------
Summary
The promise of Web Services and other XML-based technologies has generated an incredible amount of work in this area, with progress regarding specifications and the announcement of new products and projects happening all the time. No doubt such efforts will continue, given the incredible potential that this concentration of technologies has to offer.
In the next chapter, you’ll turn your attention to the security-minded strategies that developers should always keep at the forefront of their development processes.