Programming Web Services with XML-RPC
![]() Programming Web Services with XML-RPC By Simon St.Laurent, Joe Johnston & Edd Dumbill
Foreword by Dave Winer |
|||||
The following sections explore using PHP to integrate two web applications into one interface. The first section demonstrates how to create a complete PHP XML-RPC server application, in this case a discussion server. The web application to which this server will be connected is a database called Meerkat, the brainchild of Rael Dornfest and O'Reilly & Associates, Inc. (who also happen to be the publishers of this book). Meerkat is a storehouse of news about technological developments. After a subsequent section that gives an overview of Meerkat, the chapter demonstrates how to integrate the database with the custom XML-RPC discussion server.
A feature of many successful web sites and online applications is the ability to annotate, or provide comments regarding, on-site content. Unfortunately, it can be hard to obtain software supporting this feature that will integrate in the way you wish, often due to rigid user-interface constraints.
In this section, we attempt to solve this problem by creating an XML-RPC discussion/comment server. Because the interface is XML-RPC, you will be able to integrate it into your own applications without sacrificing the feel of your user interface.
The first task is to design the API. In the spirit of doing the simplest thing that works, this example application uses a straightforward interface. All items that can be commented on are identified by a string ID--this might be a URL, for example, or just a number corresponding to a database record identifier. Here is the API:
discuss.addComment(item_id, person_name, comment)
discuss.getComments(item_id)
This minimal interface allows a basic form of annotation to be added to web applications. Example 5-2 shows the listing of the server script that implements the interface. A simple file-based database is used to store the comments.
Note that the server uses Berkeley DB2 databases, so your installation of PHP must be compiled with support for this.[4] The web server must have write access to the directory where the database is stored. Windows users also have to change this directory to something like C:\TEMP.
4. Berkeley DB is now known as SleepyCat DB; see http://www.sleepycat.com/ for more information.
|
The Meerkat web application (http://meerkat.oreillynet.com/) is O'Reilly & Associates' database of headlines from technology sites around the Web. (O'Reilly & Associates is known, among other things, for the animal-themed cover designs of the books they publish. Meerkat's name carries on this tradition among O'Reilly's online offerings.) The Meerkat database aggregates the title, description, and URL of technology news stories and other resources from XML files, using the RDF Site Summary (RSS) format.[5] Each site creates an RSS file, which Meerkat retrieves and adds to its database. Meerkat organizes the web sites ("channels") into categories, according to the subject matter they cover. Figure 5-1 shows a screenshot of Meerkat, displaying stories in its "XML" category
In addition to its web interface, Meerkat provides an XML-RPC interface that gives access to its content. A well-structured PHP program normally abstracts its functionality into classes, which contain the program logic and separate activation of those classes in conjunction with HTML markup. XML-RPC for PHP can take advantage of that structuring, enabling you to add a web application interface easily by writing simple wrappers for your existing program logic. Meerkat is an example of an application that has done just that.
|
|
Meerkat's interface offers the following methods, three of which are used in Example 5-3:
meerkat.getCategories( )
meerkat.getChannels( )
meerkat.getChannelsByCategory(category_id)
meerkat.getItems(recipe)
5. Resources and tutorials for RSS can be found on the O'Reilly Network Syndication DevCenter at http://www.oreillynet.com/rss/.
|
Now we have two XML-RPC applications at our disposal: Meerkat, to explore stories on web sites, and our discussion server, which can annotate arbitrary items as long as they can be given a string identifier.
PHP and XML-RPC can be used to synthesize these two applications into a new one, allowing spontaneous discussions to take place around news stories. Figures 5-2 and 5-3 show the user interface for such an application. Users can browser Meerkat's database, and whenever stories that have comments are found, the comments are displayed against them. A link gives the option to add a comment to a story.
|
|
|
|
Example 5-3 gives a complete listing of the code required to build this application.
![]() Programming Web Services with XML-RPC By Simon St.Laurent, Joe Johnston & Edd Dumbill
Foreword by Dave Winer |
|||||
After reading this chapter, there should hopefully be no surprises for you in the Meerkat discussion application. It is a purely client-based application, creating two xmlrpc_client objects: one for Meerkat and one for our discussion server. However, there are some features worth noting:
dispatch( ) function wraps all the common error handling required in processing XML-RPC method calls. It may prove useful in your own programs.
discuss.getComments( ) accepts an array of IDs as its argument and returns an array of arrays holding the comments for each ID.
In general, it is not wise to include many XML-RPC calls in applications that require rapid user feedback--although many factors, such as server load and network conditions, obviously affect this decision.
|
Related Links |
This chapter demonstrates the use of PHP in creating XML-RPC client and server applications. PHP is ideal for creating XML-RPC web services in an environment that already has a running web server, and thus for adding XML-RPC interfaces to existing web applications. This chapter also shows the strengths of PHP used as a web integration language, bringing together disparate applications under one user interface.
Although normally thought of as a frontend application tool, PHP can now be a viable choice for creating backend systems by using XML-RPC as a middleware language. Consider the advantages of hiding access to a database behind an XML-RPC service. You could use PHP's native database routines to tightly control access to the underlying data store. CGI applications written in any language can then retrieve information from the database without actually knowing anything about the particular Relational Database Management System (RDBMS) used.
The possibilities for web services are only now being explored. PHP makes that exploration both easy and fun.
Return to XML.com.
XML.com Copyright © 1998-2006 O'Reilly Media, Inc.