WordPressにXML-RPCで投稿@PHP

Posted by tech | PHP, WordPress | 2009/11/17(火) 22:28

pearのXML_RPCモジュールをインストールしておく

# pear install XML_RPC




↓ ソースここから。

require_once("XML/RPC.php");

$host = "hogehoge.com"; // WordPressのHost名
$user = "username"; // WordPressのユーザ名
$pass = "password"; // WordPressのパスワード
$xmlrpc = "/xmlrpc.php"; // XMLRPCのパス
$port = 80; // ポート番号
$blogid = 1; // blog ID

$title = "記事のタイトル";
$description = "記事の本文";
$publish = 1; // 0:下書き、1:公開

$category1 = 1; // カテゴリID
$category2 = 5; // カテゴリID
$category3 = 8; // カテゴリID


/* 投稿処理 */
// クライアント生成
$Client = new XML_RPC_client($xmlrpc, $host, $port);

// メッセージ作成
$Message = new XML_RPC_Message(
  "metaWeblog.newPost",
  array( new XML_RPC_Value($blogid, "string"),
         new XML_RPC_Value($user, "string"),
         new XML_RPC_Value($pass, "string"),
         new XML_RPC_Value( array( "title" => new XML_RPC_Value($title, 'string'),
                                   "description" => new XML_RPC_Value($description, 'string'),
                                   "dateCreated" => new XML_RPC_Value("", 'string') // 投稿日時
                                 ), "struct"
                          ),
         new XML_RPC_Value($publish, "boolean")
       )
);

// メッセージ送信
if(!$Response = $Client->send($Message)){
	echo "Post failed.";
}


/* カテゴリ変更 */
// Post ID取得
$Buf = $Response->value();
$PostId = $Buf->me["string"];

// カテゴリ指定
$CateList = array( new XML_RPC_Value($category1, "int"),
                   new XML_RPC_Value($category2, "int"),
                   new XML_RPC_Value($category3, "int")
                 );
$Category = new XML_RPC_Value($CateList, "struct");

// メッセージ作成
$Message = new XML_RPC_Message(
  "mt.setPostCategories",
  array( new XML_RPC_Value($PostId, "int"),
         new XML_RPC_Value($user, "string"),
         new XML_RPC_Value($pass, "string"),
         $Category
       )
);

// メッセージ送信
if(!$Response = $Client->send($Message)){
	echo "Category change failed.";
}