« PHPのセキュリティ対策 | Main | XML-RPCでWordPressのカテゴリ指定時のバグ »
phpとpearでXML-RPCを利用してWordPressへ投稿
By admin | 1 月 9, 2009
pearの「XML_RPC」モジュールをインストールしておきます。
pear install XML_RPC
ソースはこんな感じ。
-----
$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";
}
-----
一応これで投稿できますが、
カテゴリはデフォルトのものになってしまいます。
カテゴリも指定したい場合は、続けてこんな感じで書きます。
-----
$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です。
Topics: PHP, WebProgramming, WordPress |



4 月 3rd, 2009 at 11:00:31
[...] phpとpearでXML-RPCを利用してWordPressへ投稿 | ボクの覚え書き - CentOSでサーバ
4 月 29th, 2009 at 18:01:21
[...] phpとpearでXML-RPCを利用してWordPressへ投稿 | ボクの覚え書き - CentOSでサーバ
9 月 7th, 2009 at 17:37:15
[...] http://q.hatena.ne.jp/1144846005が解決のヒントとなった。ping送信についてなので直接は関係ないのだが問題があった時、PEARのXML-RPCを使ったという回答がある。今回、私もPEARのXML-RPCを導入した。リンクのページからXML_RPC-1.5.2をDLし実装する。そしてhttp://memo.funny-k.com/archives/108/comment-page-1のページにあるソースをほぼ利用してソフトを組んだ。 [...]