今日の役に立たない一言 - Today’s Trifle! -

古い記事ではさまざまなテーマを書いていますが、2007年以降はプログラミング関連の話がほとんどです。

XMLRPCでカテゴリとタグ付きでブログ記事を投稿する方法

ブログに記事を投稿するのに、カテゴリとタグを指定して投稿しようとしたんだけど、なかなかうまくいかなくて試行錯誤して、やっとうまくいったのでまとめとく。

カテゴリとタグをString配列にしてMapに格納するのがポイントだったらしい。

	XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
	String url = "http://memo.satoshis.jp/xmlrpc.php";
	config.setServerURL(new URL(url));

	XmlRpcClient client = new XmlRpcClient();
	client.setConfig(config);
	Hashtable<Object,Object> hash = new Hashtable<>();
	hash.put("post_title", title);// 記事のタイトル
	hash.put("post_content", contents);// 本文
	hash.put("post_author", "1"); //
	hash.put("post_status", "publish"); // 一般公開

	Map<String, Object> terms = new HashMap<>();
	terms.put("category", categories); // カテゴリをString配列で
	terms.put("post_tag", tags); // タグをString配列で
	hash.put("terms_names", terms);

	Object[] params = new Object[5];
	params[0] = "1"; // BlogID
	params[1] = "userid";
	params[2] = "password";
	params[3] = hash;
	params[4] = true;
	Object result = client.execute("wp.newPost", params);