Skip to content

Instantly share code, notes, and snippets.

@bert2002
Created August 6, 2012 12:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bert2002/3274182 to your computer and use it in GitHub Desktop.
Save bert2002/3274182 to your computer and use it in GitHub Desktop.
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
Copy link

eimkasp 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