These examples utilize the simple Representational State Transfer (REST) style of web services.
addTwo.php
Adds two numbers and returns the results in plain text.
The following file resides on the faculty.washington.edu server and comprises the web service
- addTwo.php – a simple PHP script that takes two parameters, adds them together, and echos the result in a string of text.
The client script resides on the linux.ischool.washington.edu server. It calls the remote function addTwo.php and passes it two parameters.
Click here to connect to a demo version of this code.
addTwoXML.php
Adds two numbers and returns the results as XML.
The following files reside on the faculty.washington.edu server and comprise the web service. Because this example is so simple, it is written as a limited MVC design pattern (the object methods and view class are conflated into one method).
- Response.php - The model class for the response; includes a method for converting the response to XML.
- addTwoXML.php – A controller of sorts; invokes the Response class and passes the object to the "toXML" method.
The client script resides on the linux.ischool.washington.edu server. It calls the remote class addTwoXML.php, passes it two parameters, and parses the XML result.
Click here to connect to a demo version of this code.
getTunesXML.php
This more complex example demonstrates how a web service can execute a database query on the remote server and return a structured set of results to the client in XML. The client can parse and display the XML results as an HTML table. This example is written using a full MVC design pattern so you can see how that style translates to a web service. Essentially, the web services functionality is implemented via a controller which is remotely called by the client.
The following files reside on the faculty.washington.edu server. In addition, the database that's being searched is also on faculty.washington.edu.
- TunesWebService.php – The models, methods, and XML view classes are all implemented in this file. Normally, you'd break out these functions into separate files for greater separation of function.
- getTunesXML.php - This is the Controller for our MVC-style web service. The remote client calls this page and passes it search parameters.
The client resides on the linux.ischool.washington.edu server.
- searchTunesClient.php – This file calls a controller function in the "getTunesXML.php" file and parses the XML results. The search term is hard-coded into this file for demonstration purposes; in practice, this would likely be a web form that allowed a user to enter whatever search queries were defined by the web service.