各サイトを参考に試してみた。PEAR::XML_RPCとPEAR::Services_Bloggingでの投稿は成功したけど、PEAR::XML_RPC2は投稿に失敗した。cURL拡張モジュールが有効ではないのかも(未確認)。
エラーメッセージ
Warning: require_once(PEAR.php) [function.require-once]: failed to open stream: No such file or directory in /public_html/XML/RPC2/Backend.php on line 44 Fatal error: require_once() [function.require]: Failed opening required 'PEAR.php' (include_path='.:/usr/share/pear:/usr/share/php') in /public_html/XML/RPC2/Backend.php on line 44
PEAR::XML_RPCのコード
require_once("XML/RPC.php");
$host = "PATH_TO_SERVER";
$xmlrpc_path = "/wordpress/xmlrpc.php";
$appkey = '';
$user = 'USERNAME';
$passwd ='PASSWORD';
$c = new XML_RPC_client($xmlrpc_path, $host, 80);
$appkey = new XML_RPC_Value($appkey, 'string');
$username = new XML_RPC_Value($user, 'string');
$passwd = new XML_RPC_Value($passwd, 'string');
$message = new XML_RPC_Message(
'blogger.getUsersBlogs', array($appkey, $username, $passwd)
);
$result = $c->send($message);
if(!$result){
exit('Could not connect to the server.');
} else if( $result->faultCode() ){
exit($result->faultString());
}
$blogs = XML_RPC_decode($result->value());
$blog_id = new XML_RPC_Value($blogs[0]["blogid"], "string");
//--------ここまでがBlogIDの取得----------------------------
$title = "xml-rpc test";
$categories = array(
new XML_RPC_Value("###カテゴリー###", "string"),
);
$description = "###本文### HTMLもOK<p>段落</p>";
$content = new XML_RPC_Value(
array(
'title' => new XML_RPC_Value($title, 'string'),
'categories' => new XML_RPC_Value($categories, 'array'),
'description' => new XML_RPC_Value($description, 'string'),
'dateCreated' => new XML_RPC_Value(time(), 'dateTime.iso8601')
),
'struct');
$publish = new XML_RPC_Value(1, "boolean");
$message = new XML_RPC_Message(
'metaWeblog.newPost',
array($blog_id, $username, $passwd, $content, $publish)
);
$result = $c->send($message);
if(!$result){
exit('Could not connect to the server.');
} else if( $result->faultCode() ){
exit($result->faultString());
}
PEAR::Services_Bloggingのコード
require_once 'Services/Blogging.php';
$bl = Services_Blogging::factory(
'MetaWeblog',
'USERNAME', 'PASSWORD',
'http://PATH_TO_SERVER', '/wordpress/xmlrpc.php'
);
$post = $bl->createNewPost();
$post->title = '###タイトル###';
$post->content = "###本文###";
$post->categories = array('###カテゴリー###');
$bl->savePost($post);
PEAR::XML_RPC2のコード
require_once("./XML/RPC2/Client.php");
require_once("./XML/RPC2/Value.php");
$prefix = 'metaWeblog.'; // プレフィックス
$encoding = 'utf-8'; // 文字エンコーディング
$uri = 'http://PATH_TO_SERVER/wordpress/xmlrpc.php';
$options = array('prefix' => $prefix, 'encoding' => $encoding);
$client = XML_RPC2_Client::create($uri, $options); // XML-RPXクライアントの生成
$blog_id = XML_RPC2_VALUE('1', 'string');
$username = XML_RPC2_VALUE('USERNAME', 'string'); // ウェブサービスのユーザー名
$passwd = XML_RPC2_VALUE('PASSWORD', 'string'); // ウェブサービスのパスワード
$title = "###タイトル###";
$categories = array(
new XML_RPC2_Value("###カテゴリー###", "string"),
);
$description = "###本文### HTMLもOK<p>段落</p>";
$content = new XML_RPC2_VALUE(
array(
'title' => new XML_RPC2_VALUE($title, 'string'),
'categories' => new XML_RPC2_VALUE($categories, 'array'),
'description' => new XML_RPC2_VALUE($description, 'string'),
'dateCreated' => new XML_RPC2_VALUE(time(), 'dateTime.iso8601')
),
'struct');
$publish = new XML_RPC2_VALUE(1, "boolean");
try {
$result = $client->newPost($blog_id, $username, $passwd, $content, $publish);
} catch (XML_RPC2_FaultException $e) {
var_dump($e);
} catch (Exception $e) {
var_dump($e);
}
参考サイト
- [WordPress] XML-RPC を使用する方法 | Sun Limited Mt.
- 小粋空間: PHPでXML-RPCを利用する方法
- PHP、XML-RPCでブログ投稿MetaWeblog編 | バレで昼寝
- XML-RPC Support – WordPress Codex 日本語版
- MovableType で使える XML-RPC API
関連エントリー