pearの「XML_RPC」モジュールをインストール。
1 |
$ sudo pear install XML_RPC |
ソースはこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
$host = "xxx.yyyyy.zzz"; // WordPressのホスト名 $user = "userid"; // WordPressのユーザーID $pass = "password"; // WordPressのパスワード $xmlrpc = "/xmlrpc.php"; // XML-RPCのパス $title = "記事のタイトル"; $description = "記事の本文"; require_once("XML/RPC.php"); // クライアント作成 $client = new XML_RPC_client($xmlrpc, $host, 80); // メッセージ作成 $message = new XML_RPC_Message( "metaWeblog.newPost", array( new XML_RPC_Value(1, "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(1, "boolean") //「0」だと下書き ) ); // メッセージ送信 $response = $client->send($message); if(!$response){ echo "Post Failed.\n"; |
}
一応これで投稿可能だが、カテゴリはデフォルトのものになってしまう。カテゴリも指定したい場合は、さらに
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$categoryid1 = 5; // 登録したいカテゴリID $categoryid2 = 7; // 登録したいカテゴリID // PostID取得 $ret = $response->value(); $postid = $ret->me['string']; // クライアント作成 $client = new XML_RPC_client($xmlrpc, $host, 80); // カテゴリ $catelist = array( new XML_RPC_Value($categoryid1, "int"), new XML_RPC_Value($categoryid2, "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 ) ); // メッセージ送信 $response = $client->send($message); if(!$response){ echo "Category Change Failed.\n"; } |
これでカテゴリの指定もOK。