Post to WordPress in Perl (xmlrpc)

Filed under: Perl tips — vincent @ 23:29

Today, I had to enable a custom program to inject posts in a WordPress blog. Since WordPress implements not less than 3 differents APIs to this effect, through XMLRPC, the choice of the technology was easy: Perl and xmlrpc.

The task was complicated by the fact that the page was only accessible by passing through an HTTP Basic Authentication.

A few packages enable you to make XML RPC calls in Perl:

I first tried Frontier, just because I had it already on my developpement system. I couldn’t find anywhere in the documentation how to make an HTTP Basic Authentication with it, to access a password protected page. So rejected it.

The RPC::XML library was also already packaged for my Ubuntu 6.06 system (package librpc-xml-perl). It found it well documented and could get up to speed in no time. I here provide two code extracts, to show basic usage. As usual, they are short, with some hard-coded values, and should not be taken as an example of programmation style!

Inject a new post in WordPress

Here comes the code:

#!/usr/bin/perl -w
use strict;
use RPC::XML::Client;

my $cli=RPC::XML::Client->new('http://www.cipherbrain.be/wordpress_base/xmlrpc.php');
$cli->credentials('TheHttpRealm','TheHttpLogin','TheHttpPassword');

my $appkey="perl"; # doesn't matter
my $blogid=1; # doesn't matter (except blogfarm)

my $username="TheWordPressUser";
my $passwd='TheWordPressPassword';

my $text=<<'END';
<title>ThePostTitle</title>ยต
<category>234</category>

This is the post content...

You can also include html tags...

See you!

END

my $publish=0; # set to 1 to publish, 0 to put post in drafts
my $resp=$cli->send_request('blogger.newPost',$appkey,$blogid,$username,$passwd,$text,$publish);

Here are the placeholders that I used for some special values:

TheHttpRealm
If you have restricted the access to the page (for example, with a AuthType directive in your .htaccess file under Apache), this is what you put in the line AuthName of the same .htaccess). If defines the realm of your authentication.
TheHttpLogin
This is the user that you will generally have defined, under Apache, in your .htpasswd file.
TheHttpPassword
You guess it: your password (in clear text), generally encrypted in your .htpasswd file.
TheWordPressUser
This a user defined in WordPress which has the necessary rights to post in the category that you choose.
TheWordPressPassword
And this is the password associated with the user TheWordPressUser
ThePostTitle
The title to use for your post
234
This is the ID number (inside WordPress) of the category in which to post your article

All of this is easily usable from an external program, except for the category ID. Indeed, how would you know what ID correspond to a given category name? Of course, you can manually look in WordPress, and it is probably the easiest way to do if your categories don’t change often.

But if you want in your external program to propose the list of categories, you’ll need the next call.

Get a list of categories from WordPress

... # same as above
my $resp=$cli->send_request('metaWeblog.getCategories',$blogid, $username, $passwd);
foreach my $struct ( @$resp) {
	my $h=$struct->value; # convert to perl regular hash
	print "  ".join(',',map { "$_=".$h->{$_} } keys %$h )."n"; # display the information in hash
}

Of course, you’ll need to do a better usage of the information gained here, but I hope that you’ve understood how to get to it.

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.