XML-RPCでWordPressに投稿する
WordPressの管理画面にログインしないで投稿したかったので調べてみたら、
XML-RPCで投稿できるらしい。
XML-RPC を使用する方法 | Sun Limited Mt.
コメントアウトしてたりいらない処理があるのでもっときれいにしなきゃとは思うけど、
書いたとこまで載せておこう。
<?php require_once('XML/RPC.php'); class WP_submit extends DateTime{ private $info = array( 'user'=>'', 'pass'=>'', 'ip'=>'', 'path'=>'' ); private $username; private $passwd; private $result; private $client; private $xmlv = 'XML_RPC_Value'; function __construct($login_info){ foreach($login_info as $key=>$val){ $this->info[$key] = $val; } } function connect(){ $appkey = ''; $this->client = new XML_RPC_client($this->info['path'],$this->info['ip'],80); $appkey = new $this->xmlv($appkey,'string'); $this->username = new $this->xmlv($this->info['user'],'string'); $this->passwd = new $this->xmlv($this->info['pass'],'string'); $m = new XML_RPC_Message( 'blogger.getUsersBlogs', array($appkey,$this->username,$this->passwd) ); $this->result = $this->client->send($m); } function state(){ if(!$this->result){ throw new Exception('fail connect'); }else{ return true; } } function get_blogid(){ if($this->state()){ $blogs = XML_RPC_decode($this->result->value()); $id = new $this->xmlv($blogs[0]["blogid"], "string"); return $id; } } function get_entry($id,$num){ $article_num = new $this->xmlv($num,'int'); $m = new XML_RPC_Message( 'metaWeblog.getRecentPosts', array($id,$this->username,$this->passwd,$article_num) ); $r = $this->client->send($m); if($r){ $entry = array_map( array($this,'content'), XML_RPC_decode($r->value()) ); } //以下は最新一件のみを返す。$entryは配列でブログのデータが代入されてる return $entry[0]['description']; } function content($data){ if(is_array($data)){ return array_map(array($this,'content'),$data); } return mb_convert_encoding($data, 'UTF-8','UTF-8'); } function post_entry($id,$title,$fieldlist,$tag,$cat,$is_publish){ $category = $this->makecategory_data($cat); $fields = $this->makefields_data($fieldlist); $entry_data = new $this->xmlv( array( 'title' => new $this->xmlv($title,'string'), 'categories' => new $this->xmlv($category,'array'), //'description' => new $this->xmlv($txt,'string'), 'mt_keywords' => new $this->xmlv($tag,'string'), 'custom_fields' => new $this->xmlv($fields,'struct'), 'dateCreated' => new $this->xmlv(time()+(60*60*9),'dateTime.iso8601') ),'struct'); $publish = new $this->xmlv($is_publish,'boolean'); $m = new XML_RPC_Message( 'metaWeblog.newPost', array($id,$this->username,$this->passwd,$entry_data,$publish) ); $r = $this->client->send($m); if(count($r)){ header('Location: ./result.html'); } } function makecategory_data($cat){ $categorys = array(); for($i=0;$i<count($cat);$i++){ $categorys[] = new $this->xmlv($cat[$i],'string'); } return $categorys; } function makefields_data($fieldslist){ $fields = array(); foreach($fieldslist as $k => $val){ $fields[] = new $this->xmlv( array('key' => new $this->xmlv($k,'string'), 'value' => new $this->xmlv($val,'string') ),'struct'); } return $fields; } function htm($post){ foreach($post as $key => $val){ if(empty($val)){ header('Location: ./error.html'); exit(); } if($key === 'category'){ for($i=0;$i<count($post[$key]);$i++){ $post[$key][$i] = htmlspecialchars($post[$key][$i],ENT_QUOTES); } }else{ $post[$key] = htmlspecialchars($val,ENT_QUOTES); } } return $post; } } $login_info = array( 'user'=>'WordPressのID', 'pass'=>'WordPressのパスワード', 'ip'=>'サーバのIPアドレス', 'path'=>'./xmlrpc.php' ); $t = new WP_submit($login_info); $t->connect(); $id = $t->get_blogid(); //$entrys = $t->get_entry($id,5); $_POST = $t->htm($_POST,ENT_QUOTES); $cat = $_POST['category']; $tag = $_POST['post_tag']; $fieldlist = array( 'hoge1' => $tag, 'hoge2' => $_POST['field2'], 'hoge3 ' => $_POST['field3'], 'hoge4' => $_POST['field4'], 'hoge5' => $_POST['field5'] ); $post = $t->post_entry($id,$_POST['title'],$fieldlist,$tag,$cat,0);
ここには書かないけど実際は投稿画面のhtmlファイルなどもあるよ。
なのでこのままコピーしても動きません!
ちょっと特定の投稿用に特化しすぎてるので、
汎用化できたらいいな。