PHPでlivedoorBlogのAtomPubをそこそこ手懐けた【改】
過去に書いた以下の記事
PHPでlivedoorBlogのAtomAPIをそこそこ手懐けた - アンバランスな日々に
が古くなってもう使えなくなっていたので新しいコードを置いておきますね。
<?php function livedoor_api($title,$text){ require_once 'HTTP/Request2.php'; $id = ""; /* livedoorID */ $key = ""; /* API Key */ $url = "http://livedoor.blogcms.jp/atom/blog/".$id.'/article'; $created = date('Y-m-d\TH:i:s\Z'); $nonce = pack('H*', sha1(md5(time()))); $pass_digest = base64_encode(pack('H*', sha1($nonce.$created.$key))); $wsse = 'UsernameToken Username="'.$id.'", '. 'PasswordDigest="'.$pass_digest.'", '. 'Nonce="'.base64_encode($nonce).'", '. 'Created="'.$created.'"'; $text64= base64_encode($text); $rawdata = '<?xml version="1.0"?>'. '<entry xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">'. '<title type="text/html" mode="escaped">'.$title.'</title>'. '<content type="application/xhtml+xml" mode="base64">'.$text64.'</content>'. '</entry>'; $headers = array( 'X-WSSE: ' . $wsse, 'Expect:' ); try{ $req = new HTTP_Request2(); $req->setUrl($url); $req->setMethod(HTTP_Request2::METHOD_POST); $req->setHeader($headers); $req->setBody($rawdata); $response = $req->send(); } catch (HTTP_Request2_Exception $e) { die($e->getMessage()); } catch (Exception $e) { die($e->getMessage()); } }
HTTP_Request2を使えるようにしておいて下さいね。