Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

Using Perl to post to a WordPress blog

Thursday, July 6, 2006 | 5 comments

Today I was looking for a simple Perl example of posting to a WordPress blog. I learned that WordPress uses XML-RPC, a remote procedure call protocol using XML to encode its calls, but the documentation was not that good.

WordPress seems to support several blogging APIs that work over XML-RPC though for the specifics I decided to just look at the PHP source.

If you are looking for a simple example to get you started, stop looking. The following Perl module provides two simple methods, one that returns a reference to an array containing all available categories, and one that lets you post in one or more categories.

package WordPress;


use strict;
use warnings;

use Carp;
use XMLRPC::Lite;


sub new {

    my ( $class, $proxy, $username, $password ) = @_;

    my $self = {

        server   => XMLRPC::Lite->proxy( $proxy ),
        username => $username,
        password => $password
    };

    bless $self, $class;

    return $self;
}


sub get_categories {

    my $self = shift;

    my $call = $self->{ server }->call(

        'metaWeblog.getCategories',
        1,                              # blogid, ignored
        $self->{ username },
        $self->{ password }
    );
    $call->fault and croak 'get_categories: ', $call->faultstring;

    my @categories;
    push @categories, $_->{ categoryName } for @{ $call->result };

    return \@categories;
}


sub post {

    my ( $self, $title, $description, $categories ) = @_;

    defined $categories or $categories = [];

    my $call = $self->{ server }->call(

        'metaWeblog.newPost',
        1,                              # blogid, ignored
        $self->{ username },
        $self->{ password }, {

            title       => $title,
            description => $description,
            categories  => $categories,
        },
        1                               # publish
    );
    $call->fault and croak 'get_categories: ', $call->faultstring;
}

1;

The module uses the XMLRPC::Lite module which comes with SOAP::Lite. If you are using ActivePerl this module is already installed.

use strict;
use warnings;

use WordPress;

my $wp = WordPress->new(

    'http://example.com/xmlrpc.php',
    'username',
    'password'
);

# show the available categories
my @categories = @{ $wp->get_categories };
print map { "$_\n" } @categories;

# post in a random category
$wp->post(

    "Hello, Perl World!",
    "Posted via Perl",
    [ $categories[ rand @categories ] ]
);

The above Perl program first gets all available blog categories, and posts a message to a single randomly selected blog category.

The first parameter to the new class method of the WordPress module is the location of xmlrpc.php of your blog. If your blog is located at:

http://example.com/

use:

http://example.com/xmlrpc.php

And if your blog is located at:

http://example.com/wordpress/

use:

http://example.com/wordpress/xmlrpc.php

An easy check is to enter the URL in your browser, e.g. http://example.com/xmlrpc.php. You should see the following message:

XML-RPC server accepts POST requests only.

WordPress XML-RPC related

Also today

Please post a comment | read 5 comments, latest by ecoenrg | RSS feed