WordPress XMLRCP API

Ich glaube die WordPress API ist einer der am wenigsten dokumentierten API die ich jemals gesehen habe. Deswegen gibt es jetzt ein Beispiel um in Perl ein Bild hochzuladen, einen Post zu erstellen mit Categories, Custom Fields und einem Featured Image.

#!/usr/bin/perl
# script: publish article to a wordpress blog using XMLRPC interface. Uploading a picture and setting this as a featured image. Additionally add custom fields.
# author: <bert2002>
use strict;
use utf8;
use XMLRPC::Lite;
use IO::Socket::SSL;
my ($buf,$contents);
my $pictureid;
my @categories;
# settings
my $username = “USERNAME”;
my $password = “PASWORT”;
my $server = “https://www.yourblog.com/xmlrpc.php”;
my $image = “PATHtoIMAGE”
my $htmlpost = “THErealCONTENT”;
# upload picture to wordpress
open(FILE, “$image”) or die “$!”;
while (read(FILE, $buf, 60*57)) {
        $contents .= $buf;
}
close(FILE);
my $res = XMLRPC::Lite
->proxy($server)
->call(‘metaWeblog.newMediaObject’, 1, $username, $password,
{ name => $image, type => ‘image/jpeg’, bits => $contents } )
->result;
if (defined ($res)) {
$pictureid = $res->{id};
print “. picture uploaded with id $pictureid\n”;
} else {
        print ” .. uploading picture failed: $!”;
}
# post article to wordpress
my $publishdate = “20100220T12:34:56”;
# prepare categories
my $multicategories = “cat1 cat2 cat3”;
@categories = split(/ /, $multicategories);
# finally create that fucking post
my $res = XMLRPC::Lite
->proxy($server)
->call(‘metaWeblog.newPost’, 1, $username, $password,
{
        categories => \@categories,
        custom_fields => [
                { “key” => “keyone”, “value” => “value one” },
                { “key” => “keytwo”, “value” => “value two” }
        ],
        description => $htmlpost,
        title => $productname,
# dateCreated => $publishdate,
        mt_allow_comments => 0,
        wp_post_thumbnail => $pictureid,
}, 1)->result;
if (defined ($res)) {
        print “. posted article id $res and picture id $pictureid\n”;
} else {
        print “.. posting article with id $res failed: $!”;
}
 oder auf gist.github.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.