Programming Web Services with XML-RPC
Chapter 5 (Excerpt)
Editors' note: This excerpt from O'Reilly & Associates' recently published
Programming Web Services with XML-RPC discusses using PHP to integrate two web applications into a single interface. In the given example, the authors show how a technology news service based on RSS (RDF Site Summary) feeds was created.
Integrating Web Applications: XML-RPC in PHP
Connecting Web Applications
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.
Creating a 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:
- This method adds a comment to the item specified by the item ID. It also records the name of the commenter against that comment. All three arguments are strings.
- This method returns an array of structs; each struct corresponds to an individual comment and has two members: "person" and "comment."
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. 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.