LoginSignup
1
0

More than 5 years have passed since last update.

Perl XML-RPC画像投稿の例

Last updated at Posted at 2018-06-25

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;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0