XML-RPCを使って画像付きの記事をブログに投稿する例
シーサーブログやfc2ブログで運用実績あり
#!c:\perl\bin\perl
#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;
use Data::Dumper;
use XMLRPC::Lite;
use File::Basename;
{
my $blogid = "ブログID";
my $username = "ユーザー名";
my $password = "パスワード";
my $proxyurl = "ブログのXMLRPC先";
# 画像パス
my $filename = "C:/images/xxxx.jpg";
my $bits = read_bin_file ($filename);
#
# ここで画像を投稿する
# いまのところ1枚単位
#
my $picresult = XMLRPC::Lite
-> proxy($proxyurl)
-> call('metaWeblog.newMediaObject', $blogid, $username, $password,
{
'bits' => XMLRPC::Data->type('base64', $bits),
'type' => "image/jpeg",
'name' => basename($filename)
}
)
-> result;
if (!defined ($picresult))
{
die "failed $!";
}
else
{
#
# 本文の投稿
#
my $msgresult = XMLRPC::Lite
-> proxy($proxyurl)
-> call('metaWeblog.newPost', $blogid, $username, $password,
{
'title' => "this is the title",
'description' => "and this is\n\n" .
"the body and a link to the picture\n\n" .
"{'url'} . "\">\n\nthe end :-)\n",
'mt_allow_comments' => 1, # must be int, not string
'mt_allow_pings' => 1 # must be int, not string
# 'mt_convert_breaks' => "", # string, see mt.supportedTextFilters for a valid value
# 'mt_text_more' => "", # the extended entry
# 'mt_excerpt' => "",
# 'mt_keywords' => "",
# 'mt_tb_ping_urls' => ??, #array of strings, ping URLs
},
1 # 1 = publish
)
-> result;
if (!defined ($msgresult))
{
die "failed $!";
}
else
{
print Dumper ($msgresult);
}
}
}
#
# 画像ファイル読み込み
#
sub read_bin_file
{
my ($filename) = @_;
# read in the picture
my $fh = IO::Handle->new();
open($fh, $filename) or die "$!";
local($/) = undef; # slurp
binmode($fh);
my $bits = <$fh>;
close($fh);
return $bits;
}