require_once("XML/RPC.php"); $GLOBALS['XML_RPC_defencoding'] = "UTF-8"; list($host, $xmlrpc_path) = array('example.com', '/xmlrpc.php'); list($username, $password) = array('admin', '****'); $client = new XML_RPC_client($xmlrpc_path, $host, 80); // クライアントインスタンス生成 //ポートは80 //ユーザ名とパスワード $appkey = new XML_RPC_Value('', 'string'); $username = new XML_RPC_Value($username, 'string'); $password = new XML_RPC_Value($password, 'string'); //カスタムフィールド $custom_fields = array_map( create_function( '$f', 'return new XML_RPC_Value(array( "key" => new XML_RPC_Value($f[0],"string"), "value" => new XML_RPC_Value($f[1],"string") ), "struct" );' ), array( array("カスタムフィールド名1", "値1"), array("カスタムフィールド名2", "値2") ) ); //カテゴリー $categories = array_map( create_function('$c', 'return new XML_RPC_Value($c, "string");'), array('カテゴリー名1', 'カテゴリー名2', '...') ); $content = new XML_RPC_Value( array( 'title' => new XML_RPC_Value('タイトル', 'string'), 'description' => new XML_RPC_Value('本文', 'string'), 'mt_text_more' => new XML_RPC_Value('more以降', 'string'), 'custom_fields' => new XML_RPC_Value($custom_fields, 'struct'), 'categories' => new XML_RPC_Value($categories, 'struct') ), 'struct' ); $publish = new XML_RPC_Value(0, 'boolean'); //とりあえず下書きにする //公開の場合は 1 (true) にする $message = new XML_RPC_Message( 'metaWeblog.newPost', array($appkey, $username, $password, $content, $publish) ); $r = $client->send($message); $post = $r->value(); $id = $post->me['string']; print_r($id . "\n"); exit();
XMLRPCでWordPressに投稿
XMLRPCでWordPressに投稿するスクリプトの基本構造。