Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
publish article to a wordpress blog using XMLRPC interface. Uploading a picture and setting this as a featured image. Additionally add custom fields.
#!/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: $!";
}
@eimkasp

This comment has been minimized.

Copy link

commented Mar 14, 2013

That's a great example of XML RPC files upploading ,thanks, this heleped my a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.