XML-RPCを使って、ブログを操作するためのPHPスクリプトをクラス化してみた。全部の機能が正しく動いているかどうかは確認していないけれど、いちおういくつかの正しく動いているので、公開してみることにする。
もし万一、業務用途で使いたい方という方がいらっしゃったら、コメント欄からでもご一報お願いできますか。
xmlrpc.incを使っているのでダウンロードしてパスの通っているところにおく必要あり(ダウンロードはここから)。
使い方:
最初にオブジェクトを生成して、prepareで接続を初期化してから使う。
<?php
  /*
  | Copyright (c) 2007 kaorun. All rights reserved.
  | http://php.tekmemo.com
  */
  
  require_once(‘xmlrpc.inc’);
  $GLOBALS[‘xmlrpc_internalencoding’]=’UTF-8′;
  class xmlrpc_weblog
  {
    /**
     * XML-RPC client object
     *
     * @var object
     * @access public
     */
    var $client;
    
    /**
     * Target path for XML-RPC connections
     *
     * @var string
     * @access public
     */
    var $apipath;
    
    /**
     * Target host for XML-RPC connections
     *
     * @var string
     * @access public
     */
    var $apihost;
    
    /**
     * Target port for XML-RPC connections
     *
     * @var integer
     * @access public
     */
    var $apiport;
    
    /**
     * Application key for XML-RPC connections (usually ignored)
     *
     * @var integer
     * @access public
     */
    var $appkey;
    
    /**
     * Blog ID for the target
     *
     * @var integer
     * @access public
     */
    var $blogid;
    
    /**
     * User ID for login
     *
     * @var string
     * @access public
     */
    var $userid;
    
    /**
     * Password for login
     *
     * @var string
     * @access public
     */
    var $password;
    
    /**
     * Prepare for the connection to XML-RPC client.
     *
     * @param string $path
     * @param string $host
     * @param integer $port
     * @param integer $appkey
     * @param integer $blogid
     * @param string $userid
     * @param string $password
     * @return boolean
     * @access public
     */
    function prepare($path,$host,$port,$appkey,$blogid,$userid,$password)
    {
      $this->client  =new xmlrpc_client($path,$host,$port);
      $this->appkey  =new xmlrpcval($appkey,’int’);
      $this->blogid  =new xmlrpcval($blogid,’int’);
      $this->userid  =new xmlrpcval($userid,’string’);
      $this->password  =new xmlrpcval($password,’string’);
      return true;
    }
    
    /**
     * Send XML-RPC message as "blogger.getUserInfo".
     *
     * @return mixed (successed:user information array, failed:false)
     * @access public
     */
    function blogger_getuserinfo()
    {
      $msg=new xmlrpcmsg(‘blogger.getUserInfo’,
          array(
            $this->appkey,
            $this->userid,
            $this->password
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "blogger.getUsersBlogs".
     *
     * @return mixed (successed:user blog array, failed:false)
     * @access public
     */
    function blogger_getusersblogs()
    {
      $msg=new xmlrpcmsg(‘blogger.getUsersBlogs’,
          array(
            $this->appkey,
            $this->userid,
            $this->password
          )
        );
      $result=$this->client->send($msg);
&nbs
p;     if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "blogger.newPost".
     *
     * @param string $content, blog body
     * @param boolean $publish, re-build blog instantly or not
     * @return mixed (successed:posted article number, failed:false)
     * @access public
     */
    function blogger_newpost($content,$publish)
    {
      $msg=new xmlrpcmsg(‘blogger.newPost’,
          array(
            $this->appkey,
            $this->blogid,
            $this->userid,
            $this->password,
            new xmlrpcval($content,’string’),
            new xmlrpcval($publish,’boolean’)
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "metaWeblog.newPost".
     *
     * @param array $article, blog title, body, and so on
     * @param boolean $publish, re-build blog instantly or not
     * @return mixed (successed:posted article number, failed:false)
     * @access public
     */
    function metaweblog_newpost($article,$publish)
    {
      $struct=array();
      foreach($article as $tmpKey => $tmpValue){
        $struct[$tmpKey]=new xmlrpcval($tmpValue,’string’);
      }
      $msg=new xmlrpcmsg(‘metaWeblog.newPost’,
          array(
            $this->blogid,
            $this->userid,
            $this->password,
            new xmlrpcval($struct,’struct’),
            new xmlrpcval($publish,’boolean’)
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "metaWeblog.editPost".
     *
     * @param integer $postid, target article id
     * @param array $article, blog title, body, and so on
     * @param boolean $publish, re-build blog instantly or not
     * @return boolean
     * @access public
     */
    function metaweblog_editpost($postid,$article,$publish)
    {
      $struct=array();
      foreach($article as $tmpKey => $tmpValue){
        $struct[$tmpKey]=new xmlrpcval($tmpValue,’string’);
      }
      $msg=new xmlrpcmsg(‘metaWeblog.editPost’,
          array(
            new xmlrpcval($postid,’int’),
            $this->userid,
            $this->password,
            new xmlrpcval($struct,’struct’),
            new xmlrpcval($publish,’boolean’)
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "metaWeblog.setPostCategories".
     *
     * @param integer $postid, target article id
   &n
bsp; * @param array $categories, array(array(int $category_id => bool $isPrimary),array(—),…)
     * @return boolean
     * @access public
     */
    function mt_setpostcategories($postid,$categories)
    {
      $myArray=array();
      foreach($categories as $tmpKey => $tmpValue){
        $tmpArray=array();
        $tmpArray[‘categoryId’]  =new xmlrpcval($tmpKey,’string’);
        $tmpArray[‘isPrimary’]  =new xmlrpcval($tmpValue,’boolean’);
        $myArray[]=new xmlrpcval($tmpArray,’struct’);
      }
      
      $msg=new xmlrpcmsg(‘mt.setPostCategories’,
          array(
            new xmlrpcval($postid,’int’),
            $this->userid,
            $this->password,
            new xmlrpcval($myArray,’array’)
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "mt.publishPost".
     *
     * @param integer $postid, target article id
     * @return boolean
     * @access public
     */
    function mt_publishpost($postid)
    {
      $msg=new xmlrpcmsg(‘mt.setPostCategories’,
          array(
            new xmlrpcval($postid,’int’),
            $this->userid,
            $this->password
          )
        );
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
    
    /**
     * Send XML-RPC message as "mt.supportedMethod".
     *
     * @return mixed (successed:method array, failed:false)
     * @access public
     */
    function mt_supportedmethods()
    {
      $struct=array();
      foreach($article as $tmpKey => $tmpValue){
        $struct[$tmpKey]=new xmlrpcval($tmpValue,’string’);
      }
      $msg=new xmlrpcmsg(‘mt.supportedMethods’);
      $result=$this->client->send($msg);
      if(!$result){
        return false;
      }elseif($result->faultCode()){
        return false;
      }else{
        $tmpObj=$result->value();
        return $tmpObj->scalarval();
      }
    }
  }
?>