In this final part of a four-part series that explores PEAR, you will learn about the XML-RPC protocol, which is very useful when it comes to building web services. It is surprisingly simple, both to install and to use. You'll learn how to create a client and more.
About XML-RPC
XML-RPC is an XML-based protocol which allows various languages to inter-operate in a standards-based manner. The protocol encapsulates the data of method calls in a simple XML format which can then be easily transmitted over HTTP. Many popular sites provide a web service which uses this protocol. The ability to consume these services, or provide APIs of your own, will give you many options when seeking to have your site work with others.
XML-RPC is often considered the predecessor to SOAP and originated in 1998. It was originally created by Dave Winer of Userland, with the help of Microsoft. While many organizations have moved to provide SOAP or other interfaces, quite a few have maintained XML-RPC, as it is often seen as a nice, light weight, and simple solution.
The specification for XML-RPC is very minimal, detailing only a handful of data types and a simple syntax for calling methods. The included data types are explained in this table:
Data Type
XML-RPC Translation
PHP Equivalent
boolean
<boolean>1</boolean>
boolean: TRUE, FALSE
double
<double>3.14</double>
double: 3.14
integer
<int>42</int>
int: 42
string
<string>Simple String</string>
string: "Simple String"
nil
<nil />
NULL
datetime
<dateTime.iso8601>
19980717T14:08:55
</dateTime.iso8601>
date('c') (added in PHP5)
base64
<base64>
eW91IGNhbid0IHJlYWQgdGhpcyE=
</base64>
base64_encode($someString)
array
<array>
<value><int>42</int></value>
<value>
<string>test</string>
</value>
<value>
<double>1.23</double>
</value>
</array>
Non-associative array:
array(
42,
"test",
1.23
)
struct
<struct>
<member>
<name>pi</name>
<value>
<double>3.14</double>
</value>
</member>
<member>
<name>The Answer</name>
<value>
<int>42</int>
</value>
</member>
</struct>
associative array:
array(
'pi' => 3.14,
'The Answer' => 42
)
As you can see, the XML-RPC protocol allows a variety of simple and complex data structures to be converted into a simple XML syntax. This simplicity allows a variety of languages and platforms to easily inter-operate, sending simple XML snippets back and forth and translating them easily into native data structures.
Installing XML_RPC2
PEAR provides a convenient package for accessing and creating XML-RPC services, XML_RPC2. To use the following examples, you'll need to install the package.
> sudo pear install XML_RPC2
downloading XML_RPC2-1.0.2.tgz ...
Starting to download XML_RPC2-1.0.2.tgz (49,025 bytes)
.............done: 49,025 bytes
install ok: channel://pear.php.net/XML_RPC2-1.0.2
Assuming you get the "install ok" message, you're ready to go! The XML_RPC2 files have been installed in your directory, and can be included in your applications.