LoginSignup
3
4

More than 5 years have passed since last update.

RubyのXML-RPCを利用してFC2ブログに投稿するサンプル

Posted at

XML-RPCの利用には特にgemは必要ありません。
今回はsinatraを使って、webアプリからAPI経由でFC2に投稿するサンプルを作ってみました。

main.rb
# encoding: utf-8
require 'sinatra'
require 'xmlrpc/client'

get '/fc2/new' do
  erb :new
end

post '/fc2/create' do

  #FC2ブログのエンドポイント
  client = XMLRPC::Client.new2('http://blog.fc2.com/xmlrpc.php')
  #HTTPを圧縮しない
  client.http_header_extra = {'accept-encoding' => 'identity'}

  #ブログID,パスワード、コンテンツ
  blog_id = 'blog_id'
  password = 'password' 
  contents = {
    'title' => 'test',
    'description' => 'test post'
  }

  begin
    #APIリクエスト(新規投稿)
    client.call('metaWeblog.newPost', '', blog_id, password, contents, 1)
  rescue XMLRPC::FaultException => e
    p e.faultCode
    p e.faultString
  end

end

新規投稿時は、metaWeblog.newPostというパラメータでコールしてやるといいです。
PHPによるサンプルやその他の命令については以下に詳しく紹介されています。

また、XML-RPCクライアントのHTTP圧縮によるエラーの回避については下記を参考にさせていただきました。

3
4
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
3
4