以前書いた「アメブロへ楽天APIなどの情報をPHPで自動で投稿する」の反響が良かったので、どうせならアメブロ以外の無料ブログの XML-RPC を使った自動投稿も紹介したいと思います。

ざっと調べたところ、現在、XML-RPC による投稿に対応している無料ブログサービスは「goo」「so-net」「FC2」「jugem」「SeeSaa」です。
まずは、FC2 ブログに自動投稿するプログラムを紹介したいと思います。
FC2の現状
FC2 のサービスは査察が入るなど色々と問題があったので、現在はブログの投稿内容がシビアになっています。
よって、非公開の NG ワードもありますので投稿内容には注意していきましょう。
幸い、NG でもブログ記事は下書き状態になり、それを公開で更新する際に NG なワードも教えてもらえるので利用者に対しては親切設計だと思います。
FC2BlogManager.phpの調整
FC2 に投稿する方法としてはいくつかあるのですが 、「FC2BlogManager.php」というライブラリを使うと便利です。
現在は、git 上に WordPress の過去記事をランダムに投稿するプラグインが公開されているようですが、以前のライブラリと同じものかどうか比較してみないとなんとも言えないところです。
FC2 の場合は、少し FC2BlogManager.php を調整する必要がありますが、postEntry() のメソッドを以下のようにすれば問題ありません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public function postEntry($title, $content, $blogid = 0) { if ($blogid === 0) { $blogid = new XML_RPC_Value( 0, 'string'); } else if ($blogid === '') { $blogid = new XML_RPC_Value( '', 'string'); } else { $blogid = new XML_RPC_Value( $blogid, 'string'); } $username = $this->createStringValue($this->user); $passwd = $this->createStringValue($this->password); $content = new XML_RPC_Value( [ 'title'=> $this->createStringValue($title), 'description'=> $this->createStringValue($content), 'dateCreated'=> new XML_RPC_Value(date("Ymd\TH:i:s", time()), 'dateTime.iso8601') ], 'struct' ); $publish = new XML_RPC_Value(1, 'boolean'); $message = new XML_RPC_Message( self::COMMAND_POSTENTRY, [$blogid, $username, $passwd, $content, $publish] ); $result = $this->sendMessage($message); return $result; } |
FC2以外の無料ブログへの自動投稿
また、SeeSaa だけは独自の「ブログID」の取得が必要なので、自分のブログにログインして ID を探して下さい。
ブログにログインすると、ブログ一覧に自分のブログタイトルがリンクで表示されるので、そのリンクの URL に付いている blog_id= のパラメータが「ブログID」になります。
ちょっと面倒ですけどね・・・。
Ameba
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | function ameba($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); $atomapi_url = 'http://atomblog.ameba.jp/servlet/_atom/blog'; $created = date('Y-m-d\TH:i:s\Z'); $nonce = sha1(md5(time())); $pass_digest = base64_encode(pack('H*', sha1($nonce.$created.strtolower(md5(USER_PASS))))); $wsse = 'UsernameToken Username="'.USER_ID.'", '. 'PasswordDigest="'.$pass_digest.'", '. 'Nonce="'.base64_encode($nonce).'", '. 'Created="'.$created.'"'; $ameHeader = "X-WSSE: " . $wsse; $rawdata = sprintf(' <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://purl.org/atom/ns#" xmlns:app="http://www.w3.org/2007/app#" xmlns:mt="http://www.movabletype.org/atom/ns#"> <title>%s</title> <content type="application/xhtml+xml"> <![CDATA[%s]]> </content> </entry>', a, b); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $atomapi_url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER,array($ameHeader)); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); curl_close($ch); preg_match('/rel="service.post" type="application\/x\.atom\+xml" href="(.*?)"/',$res,$postURl); $rawdata = sprintf(' <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://purl.org/atom/ns#" xmlns:app="http://www.w3.org/2007/app#" xmlns:mt="http://www.movabletype.org/atom/ns#"> <title>%s</title> <content type="application/xhtml+xml"> <![CDATA[%s]]> </content> </entry>', $title, $text); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $postURl[[1]]); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER,array($ameHeader)); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$rawdata); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); curl_close($ch); } |
FC2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function fc2($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); $fc2_host = "blog.fc2.com"; $fc2_xmlrpc_path = "/xmlrpc.php"; try { $bm = new FC2BlogManager($fc2_host, $fc2_xmlrpc_path); $bm->setUser(USER_ID); $bm->setPassword(USER_PASS); $bm->postEntry($title, $text); var_dump($bm->getBlogs()); } catch(Exception $e) { echo $e->getMessage(); } } |
goo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function goo($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); $goo_host = "blog.goo.ne.jp"; $goo_xmlrpc_path = "/xmlrpc.php"; try { $bm = new FC2BlogManager($goo_host, $goo_xmlrpc_path); $bm->setUser(USER_ID); $bm->setPassword(USER_PASS); $bm->postEntry($title, $text, USER_ID); var_dump($bm->getBlogs()); } catch(Exception $e) { echo $e->getMessage(); } } |
ジュゲム
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function jugem($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); $jugem_host = USER_ID . ".jugem.jp"; $jugem_xmlrpc_path = "/admin/xmlrpc.php"; try { $bm = new FC2BlogManager($jugem_host, $jugem_xmlrpc_path); $bm->setUser(USER_ID); $bm->setPassword(USER_PASS); $bm->postEntry($title, $text); var_dump($bm->getBlogs()); } catch(Exception $e) { echo $e->getMessage(); } } |
SeeSaa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function seesaa($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); define('BLOG_ID', '[BLOG_ID]'); $seesaa_host = "blog.seesaa.jp"; $seesaa_xmlrpc_path = "/rpc"; try { $bm = new FC2BlogManager($seesaa_host, $seesaa_xmlrpc_path); $bm->setUser(USER_ID); $bm->setPassword(USER_PASS); $bm->postEntry($title, $text, BLOG_ID); var_dump($bm->getBlogs()); } catch(Exception $e) { echo $e->getMessage(); } } |
Sonet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function so-net($title, $text) { define('USER_ID', '[ID]'); define('USER_PASS', '[PASSWORD]'); $so-net_host = "blog.so-net.ne.jp"; $so-net_xmlrpc_path = "/_rpc"; try { $bm = new FC2BlogManager($so-net_host, $so-net_xmlrpc_path); $bm->setUser(USER_ID); $bm->setPassword(USER_PASS); $bm->postEntry($title, $text, USER_ID); var_dump($bm->getBlogs()); } catch(Exception $e) { echo $e->getMessage(); } } |