Created
August 6, 2012 12:43
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: $!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's a great example of XML RPC files upploading ,thanks, this heleped my a lot