Article

Build your own Web Service with PHP and XML-RPC

Page: 1 2 3 4 5 6 7 Next

Introducing XML-RPC

In "Web Services Demystified", Kevin looks at a standard called SOAP, which is used to "package" data for exchange between two systems. SOAP is on its way to being the W3C standard for Web Services, and with backing from Microsoft and IBM will probably get the official stamp very soon.

But another standard exists for the same purpose: XML-RPC. It's been around since 1998, and although it's not an official W3C standard, is already widely used, and has impressive support in the form of Open Source projects. Developed by Useful Inc. in conjunction with Microsoft, it can be regarded in many ways as the forerunner to SOAP, which hasn't stopped running quite yet.

As this article will demonstrate, the biggest thing XML-RPC has going for it is simplicity, which is clear from the start when you compare the 1500 word specification with SOAP's 11,000 word (and growing) bible. The other good news is that XML-RPC is well supported in PHP, along with implementations in just about any other language you could desire.

For a detailed comparison of XML-RPC and SOAP, I'd recommend reading XML-RPC vs. SOAP. In brief, SOAP is intended to fill the gaps in XML-RPC, offering a truly enterprise-level mechanism for the exchange of data between systems. For example, it should be easier to deliver multi-dimensional arrays through SOAP than through XML-RPC. But there's nothing wrong with using XML-RPC, especially given that it's a stable standard, while SOAP may undergo further re-definition in the future.

You might wonder whether it's worth putting the effort into learning about XML-RPC if it's about to be superseded by another standard. Worry no more: you can easily transform a SOAP request to an XML-RPC request using XSLT, which will allow your XML-RPC to survive in a messy Internet where everyone's looking for the SOAP (apologies -- but one pun had to be made).

So how does XML-RPC work? As you'll see later in this article, you don't really need to know too much about it to be able to use it. But there's plenty of information out there that describes it, and a worthwhile read is XML-RPC for Newbies. We'll take a quick look at how XML-RPC works now, and then move on to how we can make use of it.

The two main components of an XML-RPC "message" are methods and parameters. Methods correspond loosely to the functions you define in PHP, while parameters correspond to the variables you pass to those functions. Parameters can be one of a number of different types, such as strings, integers and arrays -- very similar to the variable types you're already used to in PHP. In addition, XML-RPC defines other tags for things like error handling, but we'll leave that explanation to the spec as, for the most part, we'll never need to worry about them.

An XML-RPC "conversation", between two systems bgins with a request from the XML-RPC client, which the server answers with a response. The request contains a method and perhaps some parameters required by the method. The response replies with parameters that contain the requested data. The process is very much like using a function you've defined in a PHP script; you call a function and pass it some variables. The function then responds by returning some variables.

Here's what a client request might look like, as seen by the target XML-RPC server:

Request (from your XML-RPC client):

 POST /xmlrpcInterface HTTP/1.0  
 User-Agent: Sitepoint XML-RPC Client 1.0  
 Host: xmlrpc.sitepoint.com  
 Content-type: text/xml  
 Content-length: 195  
 <?xml version="1.0"?>  
 <methodCall>    <methodName>forums.getNumTodaysThreads</methodName>  
   <params>  
     <param><value><string>PHP Development</string></value></param>  
   </params>  
 </methodCall>

And here's the response from the server, as seen by the XML-RPC client.

Response (from the XML-RPC server):

 HTTP/1.1 200 OK  
 Connection: close  
 Content-Length: 148  
 content-Type: text/xml  
 Date: Wed, Jul 28 1999 15:59:04 GMT  
 Server: Sitepoint XML-RPC Server 1.0  
 <?xml version="1.0"?>  
 <methodResponse>  
   <params>  
     <param>  
       <value><int>42</int></value>  
     </param>  
   </params>  
 </methodResponse>

The example request might be to find out how many threads have been posted in the PHP Development forum today. The response tells the client the number it asked for. This is just a simple example, sending one parameter to a method and getting one parameter back. XML-RPC provides much more, allowing you to pass the server more complicated sets of data (such as arrays), and receive in return a detailed response that contains all sorts of data.

In case you were wondering, the "RPC" in XML-RPC stands for Remote Procedure Call. The client system "calls" a "procedure" (or method) on a remote server. The concept of a Remote Procedure Call is general to software development, and one which you'll come across in many places. XML-RPC is an XML standard for data exchange to be used in a Remote Procedure Call.

XML-RPC itself sits at the packaging layer of the technology stack described in Kevin's "Web Services Demystified" (and as mentioned above, it's an alternative to SOAP, which Kevin uses as an example of a packaging layer). What happens in the description and discovery layers is up to you. Some implementations of XML-RPC offer extensions that provide introspection, which would amount to the description layer providing details of the methods and parameters with which you can call an XML-RPC server. The discovery layer is missing with XML-RPC: there is no central registry of XML-RPC servers, but no doubt extensions conforming to the UDDI standard will arise when the need appears.

Enough detail of XML-RPC! Using the implementations that already exist for PHP, you don't need to get too involved with the standard itself. So let's see what you can get for free for XML-RPC and PHP...

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

Sponsored Links