What is XML-RPC?

南宫阳焱
2023-12-01

XML-RPC is a simple, portable way to make remote procedure calls over HTTP. It can be used with Perl, Java, Python, C, C++, PHP and many other programming languages. Implementations are available for Unix, Windows and the Macintosh.

Here's a short XML-RPC client written in Perl. (We use Ken MacLeod's Frontier::Client module.)

use Frontier::Client;
$server = Frontier::Client->new(url => 'http://betty.userland.com/RPC2');
$name = $server->call('examples.getStateName', 41);
print "$name\n";

When run, this program will connect to the remote server, get the state name, and print it. (State #41 should be South Dakota in this example.)

Here's the same program in Python. (This time, we use Fredrik Lundh's xmlrpclib.)

python> import xmlrpclib
python> server = xmlrpclib.Server("http://betty.userland.com/RPC2")
python> server.examples.getStateName(41)
'South Dakota'

In the following chapters, you'll learn how to write XML-RPC clients and servers in a variety of programming languages.

2.1. How it Works

XML-RPC is described fully in Dave Winer's official specification. If you're curious, go ahead and take a look—it's a quick and straight-forward read.

On the wire, XML-RPC values are encoded as XML:

<methodCall>
  <methodName>sample.sumAndDifference</methodName>
  <params>
    <param><value><int>5</int></value></param>
    <param><value><int>3</int></value></param>
  </params>
</methodCall>

This is verbose, but compresses readily. It's also faster than you might expect—according to measurements by Rick Blair, a round-trip XML-RPC call takes 3 milliseconds using Hannes Wallnöfer's Java implementation.

2.2. Supported Data Types

XML-RPC supports the following data types:

int

A signed, 32-bit integer.

string

An ASCII string, which may contain NULL bytes. (Actually, several XML-RPC implementations support Unicode, thanks to the underlying features of XML.)

boolean

Either true or false.

double

A double-precision floating point number. (Accuracy may be limited in some implementations.)

dateTime.iso8601

A date and time. Unfortunately, since XML-RPC forbids the use of timezones, this is very nearly useless.

base64

Raw binary data of any length; encoded using Base64 on the wire. Very useful. (Some implementations don't like to receive zero bytes of data, though.)

array

An one-dimensional array of values. Individual values may be of any type.

struct

A collection of key-value pairs. The keys are strings; the values may be of any type.


 类似资料: