WordPress and XML-RPC

2

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.

,
2

Comments

  • Old Friend

    Hail Ed,
    Hope you are doing well. Nice to see you writing again.

    A few comments for you.

    1) This looks like a good way to get some additional consulting business, but how do people contact you? I looked for your email address, but if you have it posted, I just overlooked it.

    2) You have a typo – under your Services tab you say “contact me at a list of references, current pricing or an instant quote.” I assume your email address and “for” should be there, but it is not showing up (at least under Firefox).

    3) What are you recommending for your new clients? WordPress, MT, or something else?

    4) Do you have a rates page?

    That’s it for now – I’ll be contacting you soon even if you don’t guess who this is.

    An old friend

  • Ed

    I’ve got a few ideas but nothing for certain. Thanks for providing a little quality control for me – I’ve been so busy lately, throwing this new blog up did not get my full attention, so your points are all excellent and I will be updating my Services page when I get a few free moments.

    For new clients, I would have to say WordPress. If someone really wants something different, I’m willing to work with other CMS systems or even learn a new one, but as mature as WordPress is now, it has become the de facto standard.

    Look forward to hearing from you.