Making Programs Talk to each other using XML-RPC

The Internet has changed the way applications are built today. In the last few years, we have seen a sudden burst in Internet software – Instant Messengers, Online Gaming, etc; all based on the client-server architecture. With all this client server technology, we also have to ensure compatibility between languages, and operating systems. XML-RPC is one way to do this.

WHAT IS XML-RPC?

RPC stands for “Remote Procedure Call” – a function call from one program to another. XML-RPC is a simple, standard specification by which we can use remote procedure calls in applications. The calls are made in XML format, over the HTTP layer. There are implementations of XML-RPC available for many languages, like Perl, Java, PHP, C++ and many operating systems, like Unix, Windows and Macintosh. Thus, XML-RPC is a great way to make programs in different languages/operating systems, talk to each other; and the ideal solution to creating APIs – Application Program Interfaces so that developers can write programs for your software.

XML-RPC was a specification to standardize the concept of making function calls in XML format over HTTP. It was put forward by UserLand Inc., who, along with Microsoft, went on to create the SOAP protocol – a successor of XML-RPC.

XML-RPC vs SOAP

There are many different protocols/architectures that have (more or less) the same goals as XML-RPC. SOAP, CORBA, COM, Java RMI, all deal with interoperability, but each has its own drawbacks.

XML-RPC evolved into the more popular SOAP, but the immense size and large number of strange features of SOAP make it difficult to implement, learn and use. And the constant corporate tug of war over SOAP standards has made it very hard to follow. On the other hand, XML-RPC is simple, easy to implement and already widely used.

HOW XML-RPC WORKS

A remote procedure call has a simple structure. One program passes a few parameters to the function of another program. The function executes, and the result of the function is returned. All this happens through an interface, and here we’re using XML-RPC as our interface.

Let us take a very simple example for this. Suppose we had a “client” program that would send three integers to a remote “server” program. The remote server program would then send the maximum and minimum of the three numbers back to the client.

In XML-RPC, the function call maxandmin(5,3,7) from the client would be sending this to the server:


<methodCall>
<methodName>example.maxandmin</methodName>
<params>
<param><value><int>5</int></value></param>
<param><value><int>7</int></value></param>
<param><value><int>3</int></value></param>
</params>
</methodCall>

This XML formatted list of parameters is sent over HTTP to the “server” program. The server would then process the parameters and return the result, which would be like this:

<methodResponse>
<params>
<param><value><int>7</int></value></param>
<param><value><int>3</int></value></param>
</params>
</methodResponse>

If an error occured in the function, it could be sent back in place of the result:

<fault>
<value>
<struct>
<member>
<name>faultCode</name> <value><int>4</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Something wierd.</string></value>
</member>
</struct>
</value>
</fault>

This result can then be parsed easily by the client to retrieve the data.

SUPPORTED DATA TYPES

In our example, we passed three integers to the server, under the typename “int”. There are more data types, that you can use – here’s a list of them, along with an example:

int: four-byte signed integer. eg. 9
boolean: 0 (false) or 1 (true). eg. 1
string: ASCII string. eg. hello world
double: double-precision signed float. eg. 3.1416
dateTime.iso8601: date/time. eg. 20020511W11:23:11
base64: base64-encoded binary. eg. eW91IGNhbid0IHJlYWQgdGhpcyE

You can use these data types individually, or you can combine them into an array using the tag or make them into a structure, using the tag.

IMPLEMENTATION AND AVAILABILITY

XML-RPC implementations are available in almost every language now. So if you are using Perl, PHP, Java, Ruby, C, C++, Python, or just about any other language to write your internet applications, you’re sure to find a client/server module that you can use in your code.

The implementations are all indexed at the XML-RPC website, http://www.xml-rpc.com. The site contains an exhaustive list to all resources relating to XML-RPCs and lists of imlpementations in various languages.

CONCLUSION

XML-RPC is a simple, clean, effective way to interface between programs. Though it might seem that all this language independence and XML/HTTP interfacing slows down the function call process, it’s actually quite fast. One Java implementation has been clocked at 3 milliseconds for a roundtrip XML-RPC.
XML-RPC is a good standard for writing APIs for applications, especially web-based services. It’s wide-spread availablity and acceptance in the developer community makes it a great specification to follow.