NAME

WordPress::XMLRPC - api to wordpress xml rpc calls

SYNOPSIS

use WordPress::XMLRPC;

my $o = WordPress:::XMLRPC->new({
  username => 'author1',
  password => 'superpass',
  proxy => 'http://mysite.com/xmlrpc.php',
});

my $post = $o->getPost(5); # id 5

# let's change the title
$post->{title} = 'I did not like the old title.';

# let's save the changes back to the server..
$o->editPost(5, $post, 1); # 1 is publish

DESCRIPTION

I wanted to interact via the command line to a wordpress blog's xmlrpc.php file. Bascially this is interaction with xmlrpc.php as client. This module is not meant for speed, it is meant for convenience.

This is really useful to automate new postings, uploading media, etc.

CONSTRUCTOR

new()

Optional arg is hash ref.

Before we open a connection with xmlrpc, we need to have username, password, and proxy in the object's data. You can provide this in the following ways..

my $o = WordPress:::XMLRPC->new({
  username => 'author1',
  password => 'superpass',
  proxy => 'http://mysite.com/xmlrpc.php',
});

Or..

  my $o = WordPress:::XMLRPC->new;  
  
  $o->username('author1');
  $o->password('superpass');
  $o->proxy('http://mysite.com/xmlrpc.php');

  $o->server 
     or die( 
        sprintf 'could not connect with %s:%s to %s',
           $self->username,
           $self->password,
           $self->proxy,
        );

METHODS

xmlrpc_methods()

Returns array of methods in this package that make calls via xmlrpc.

server_methods()

Returns array of server methods accessible via xmlrpc.

username()

Perl set/get method. Argument is string. If you pass 'username' to constructor, it is prepopulated.

my $username = $o->username;
$o->username('bill');

password()

Perl set/get method. Argument is string. If you pass 'password' to constructor, it is prepopulated.

my $pw = $o->password;
$o->password('jim');

proxy()

Perl set/get method. Argument is string. If you pass 'proxy' to constructor, it is prepopulated.

server()

Returns XMLRPC::Lite object. proxy() must be set.

blog_id()

Setget method, set to '1' by default. This seems unused by wordpress. They have some documentation on this.

publish()

Many methods use 'publish' boolean value, by default we set to 1. You can still pass a value for publish such as;

$o->newPost( $content_hashref, 1 );

But you can also call;

$o->newPost( $content_hashref );

As we said, by default it is set to 1, if you want to set the default to 0,

$o->publish(0);

errstr()

Returns error string if a call fails.

$o->newPost(@args) or die($o->errstr);

XML RPC METHODS

These methods specifically mirror the xmlrpc.php file provided by WordPress installations. This file sits on your website.

getPage()

Takes 1 args: page_id (number).

Returns page hashref struct(ure).

Example return:

$val: {
        categories => [
                        'Uncategorized'
                      ],
        dateCreated => '20080121T12:38:30',
        date_created_gmt => '20080121T20:38:30',
        description => 'These are some interesting resources online.',
        excerpt => '',
        link => 'http://leocharre.com/perl-resources/',
        mt_allow_comments => '0',
        mt_allow_pings => '0',
        page_id => '87',
        page_status => 'publish',
        permaLink => 'http://leocharre.com/perl-resources/',
        text_more => '',
        title => 'Resources',
        userid => '2',
        wp_author => 'leocharre',
        wp_author_display_name => 'leocharre',
        wp_author_id => '2',
        wp_page_order => '0',
        wp_page_parent_id => '0',
        wp_page_parent_title => '',
        wp_password => '',
        wp_slug => 'perl-resources'
      }

This is the same struct hashref you would send to newPage().

getPages()

Returns array ref. Each element is a hash ref same as getPage() returns. If you want less info, just basic info on each page, use getPageList().

newPage()

Takes 2 args: page (hashref), publish (boolean). You can leave out publish, as discussed further in this documentation. The hashref must have at least a title and description. Returns page id (number, assigned by server).

deletePage()

Takes 1 args: page_id (number). Returns boolean (true or false).

editPage()

Takes 2 args: page (hashref), publish(boolean). The page hashref is just as discussed in getPage().

You could use getPage(), edit the returned hashref, and resubmit with editPage().

my $page_hashref = $o->getPage(5);
$page_hashref->{title} = 'This is the New Title';

$o->editPage($page_hashref) or die( $o->errstr );

Obviously the page id is in the page data (hashref), this is there inherently when you call getPage().

The same would be done with the posts.

getPageList()

Returns arrayref. Each element is a hashref. This is sort of a short version of getPages(), which returns all info for each.

Example return:

$return_value: [
                 {
                   dateCreated => '20061113T11:08:22',
                   date_created_gmt => '20061113T19:08:22',
                   page_id => '2',
                   page_parent_id => '0',
                   page_title => 'About Moi'
                 },
                 {
                   dateCreated => '20080105T18:57:24',
                   date_created_gmt => '20080106T02:57:24',
                   page_id => '43',
                   page_parent_id => '74',
                   page_title => 'tree'
                 },
               ]

getAuthors()

Takes no argument. Returns array ref, each element is a hashref.

$return_value: [
                 {
                   display_name => 'leo',
                   user_id => '2',
                   user_login => 'leo'
                 },
                 {
                   display_name => 'chamon',
                   user_id => '3',
                   user_login => 'chamon'
                 }
               ]

getCategories()

Takes no argument.

$return_value: [
                 {
                   categoryId => '4',
                   categoryName => 'art',
                   description => 'art',
                   htmlUrl => 'http://leocharre.com/articles/category/art/',
                   parentId => '0',
                   rssUrl => 'http://leocharre.com/articles/category/art/feed/'
                 },
                 {
                   categoryId => '1',
                   categoryName => 'Uncategorized',
                   description => 'Uncategorized',
                   htmlUrl => 'http://leocharre.com/articles/category/uncategorized/',
                   parentId => '0',
                   rssUrl => 'http://leocharre.com/articles/category/uncategorized/feed/'
                 }
               ]

newCategory()

Takes 1 args: category struct. Returns category id (number).

The category struct is a hash ref alike..

{
   name => 'Ugly houses',
   parent_id => 34, # (if this is a sub category )
   description => 'this is a great category',
}

The key 'name' must be present or croaks.

getCategory()

Argument is category id, will return struct (hash ref).

### $got: {
####         categoryId => 99,
####         categoryName => 'category772',
####         description => 'category772',
####         htmlUrl => 'http://leocharre.com/articles/category/category772/',
####         parentId => '0',
####         rssUrl => 'http://leocharre.com/articles/category/category772/feed/'
####       }

CAVEAT

There seems to be a bug in xmlrpc.php (wordpress v 2.3.2) , that does not fill out the categories properly. You can use newCategory() to insert a description, bu upon getCategory(), the struct description is replaced by the categoryName field.

suggestCategories()

Takes 2 args: category, max_results.

Returns array ref, each element is a hashref (not sure what this is for).

uploadFile()

Takes 1 args: data. Data is a hash ref, see WordPress::MediaObject.

newPost()

Takes 2 args: content_struct, publish. Returns id number of new post.

editPost()

Takes 3 args: post_ID, content_struct, publish. Returns boolean, true or false.

getPost()

Takes 1 args: post_ID Returns post struct, hashref.

$example_return_value: {
                         categories => [
                                         'Uncategorized'
                                       ],
                         dateCreated => '20080130T14:19:05',
                         date_created_gmt => '20080130T22:19:05',
                         description => 'test description here',
                         link => 'http://leocharre.com/articles/test_1201731544/',
                         mt_allow_comments => '1',
                         mt_allow_pings => '1',
                         mt_excerpt => '',
                         mt_keywords => '',
                         mt_text_more => '',
                         permaLink => 'http://leocharre.com/articles/test_1201731544/',
                         postid => '119',
                         title => 'test_1201731544',
                         userid => '2',
                         wp_author_display_name => 'leocharre',
                         wp_author_id => '2',
                         wp_password => '',
                         wp_slug => 'test_1201731544'
                       }

getRecentPosts()

Takes 1 args: num_posts (number, optional).

Returns arrayref. Each element is hash ref same as getPost() would return.

getCategories()

Example return value:

$return_value: [
                 {
                   categoryId => '4',
                   categoryName => 'art',
                   description => 'art',
                   htmlUrl => 'http://leocharre.com/articles/category/art/',
                   parentId => '0',
                   rssUrl => 'http://leocharre.com/articles/category/art/feed/'
                 },
                 {
                   categoryId => '6',
                   categoryName => 'cool linux commands',
                   description => 'cool linux commands',
                   htmlUrl => 'http://leocharre.com/articles/category/cool-linux-commands/',
                   parentId => '0',
                   rssUrl => 'http://leocharre.com/articles/category/cool-linux-commands/feed/'
                 }
               ]

newMediaObject()

Takes 1 args: data (hashref). The hashref keys and values are bits (Mime::Base64), type (mime type), and name (filename).

getTemplate()

Takes 1 args: template.

setTemplate()

Takes 2 args: content, template.

getUsersBlogs()

No argument, returns users blogs. Example return :

$r: [
      {
        blogName => 'leo charre',
        blogid => '1',
        isAdmin => '1',
        url => 'http://leocharre.com/'
      }
    ]

deletePost()

Argument is post id(number). Returns boolean.

WISHLIST

It'd be nice to manage links via xmlrpc.

BUGS

Please submit to AUTHOR

CAVEATS

This distro is alpha. Included are the metaWeblog and wp method calls.

REQUIREMENTS

XMLRPC::Lite

SEE ALSO

XMLRPC::Lite SOAP::Lite WordPress http://wordpress.org

AUTHOR

Leo Charre leocharre at cpan dot org

COPYRIGHT

Copyright (c) 2009 Leo Charre. All rights reserved.

LICENSE

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".

DISCLAIMER

This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the "GNU General Public License" for more details.