Article

Build your own Web Service with PHP and XML-RPC

Page: 1 2 3 4 5 6 7 Next

PHP XML-RPC Implementations

Just as your hands were starting to sweat, it's now time to review a number of Open Source projects that can save you having to write your own code to generate XML-RPC requests and responses. Those reviewed here all come from the implementations page at xmlrpc.com. The two main "stress tests" in reviewing them were:

  1. how long do they take to install and use?

  2. will they be widely supported by the type of PHP configuration you'll find on an average Web host?

XMLRPC-EPI

http://xmlrpc-epi.sourceforge.net/

XMLRPC-EPI was developed originally for internal use at epinions and has been successful to the point that it now provides PHP with its experimental XML-RPC Functions. XMLRPC-EPI itself is a base class written in C++ (the rest are written in PHP), which won't be something you can readily install on your Web server if you don't have root access -- you'll need to re-compile PHP.

Aside from that, this class only deals with the interpretation of XML-RPC requests and responses, and doesn't actually send or receive the requests themselves. You'll find an early tutorial on its use here.

eZ xmlrpc

http://developer.ez.no/article/static/53/

Developed by Bård Farstad (gotta love that name), this is the XML-RPC class used in ezPublish to receive requests from their desktop client. The design of the class is good, in that it's fairly intuitive to work with and is nicely introduced by this tutorial, plus it offers introspection.

But it fails on one major point -- it requires that the xml parser (--with-qtdom) be available and configured for use with PHP. This is going to be non-standard for the majority of PHP installations, which usually adopt the expat XML parser (--with-xml). So, sadly, we'll have to look elsewhere...

Fase 4 XML-RPC

http://www.fase4.com/xmlrpc/

Looking at the site, the documentation is good, but in installing and trying the "server.php" demonstration script results in a "class not found" error. With so many to choose from, that eliminates this class from the race.

phpRPC

http://sourceforge.net/projects/phprpc/

This class has potential. It goes beyond interpretation and request/response handling to provide functionality like connection to an "abstract" database. The current version is alpha 0.9, so for now we're going to pass on it, but it's worth keeping an eye on. If you have big projects planned, this class could save you a lot of development time.

phpxmlrpc

http://phpxmlrpc.sourceforge.net/

Developed by Useful Inc, the creators of the XML-RPC standard, this class obviously has a lot going for it. Basically it fully supports the XML-RPC standard and offers debugging as well, which, as we'll see later, can be one of your bigger problems when building a Web Service.

The documentation is good and there are plenty of examples, as well as this tutorial on how to build a client with it. But as a starting place for building your first Web Service, this class isn't ideal -- it can be recommended only to more experienced PHP developers, which is why we're going to look elsewhere...

Keith Devens' XML-RPC Client/Server

http://www.keithdevens.com/software/xmlrpc/

Visiting the project's home page, you'll quickly realise you're off to an easier start. The code is simply a set of user-defined PHP functions to include in your scripts, as opposed to a full class.

It takes away from your XML-RPC servers and clients the requirement that they work out what types of XML-RPC parameters they're dealing with. This does mean your XML-RPC client will need to be more careful in examining the data it receives, but you'll only be worried by PHP variables (which you should already be used to), not XML-RPC parameters. Also, the documentation is good. So this is where we're going to start building your first Web Service...

Your First Web Service

Here we go! Let's build a Web Service for publishing news from your Website, along the lines of an RSS feed. But rather than simply providing a list titles and links that will end up taking the user back to the original news source, our Web Service will make it look like the news lives on the site where the XML-RPC client is installed.

We'll define two XML-RPC methods for our use: "news.getNewsList", which will provide a list of news items, and "news.viewNewsItem", which will display a single news item in full. There will also be a default "method_not_found" method, in case of trouble. Each method corresponds to a function we'll define in PHP, such as a query which SELECTs news from your database.

We'll assume you have PHP 4.1.0 or later, and access to a MySQL database (version 3.23+). It's worth reading the code and examining the comments as well as checking the documentation for Keith's XML-RPC related functions.

Ok. Roll up those sleeves, and away we go...

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links