What is XML-RPC? Stripping all the technological detail away, it is merely a specific way of requesting data and getting a response back. It uses xml to make the request and sends and receives the data over http. (A more technical discussion can be found here.)

I’m currently working on a project that needs to get data from a WordPress installation. Fortunately, WordPress provides an XML-RPC interface which includes several stock APIs as well as a WordPress specific API. This is far more efficient than using custom templates or parsing actual html pages for both sides of the transaction.

With all that said, how do you use it? Depending on what you are trying to do and the platform you are using, there should (hopefully) be a nice library already coded so you can worry about requesting and receiving data without fretting about the details of xml-rpc. However, sometimes a q&d (quick and dirty) script is useful to test a request and look at what is actually coming back. I do that using a simple php script:

<?php
  $request = xmlrpc_encode_request("blogger.getUserInfo",
    array(1, "username", "password"));

  $context = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml",
    'content' => $request
  )));

  $file = file_get_contents("http://wordpressblog.com/xmlrpc.php",
    false, $context);

  $response = xmlrpc_decode($file);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    print_r($response);
  }
?>

The key to this script is the first line where $request is defined. The first parameter is the name of the request, followed by an array of parameters for that specific request. From the API:

blogger.getUserInfo takes the following parameters. All are required:

  • appkey: (Ignored in WordPress)
  • username: User login
  • password: User Password

If you do it right, you get a result something like:

Array
(
    [nickname] => Major Burns
    [userid] => 27
    [url] => http://mash.4077.gov
    [lastname] => Burns
    [firstname] => Frank
)

If your user & password is not correct, then you will get something like:

Notice: xmlrpc: Bad login/pass combination. (403) in /var/www/virtual/test.myserver.com/htdocs/xmlrpc_test.php on line 15

And if you are using an invalid request, you will get:

Notice: xmlrpc: server error. requested method pretend.badRequest does not exist. (-32601) in /var/www/virtual/test.myserver.com/htdocs/xmlrpc_test.php on line 15 on line 15

This should get you started using the standard requests, although keep in mind that they do come with some limitations. First, the exact request you want may not be defined (yet). Also, most of the requests require a WordPress user, with many of them requiring a user with author credentials.

One trick I find useful is, if I do not understand the parameters for a request or what a request actually does (the API documentation is definitely lacking in some ways), that information is available by just pulling up the WordPress xmlrpc.php in an editor.

The next step, and probably my next post, is how to add your own custom requests to WordPress.