本サイト/記事は移転しました。

約10秒後にリダイレクトします。

自宅サーバのプログラム書き換えた

 おうちサーバで「あるメールアドレスにメールを送るとはてなハイクmixiに書き込む」というのをやっているけど、そのプログラムのはてなダイアリー書きこみ部分を変更。今まではcurlを使ってごりごり書き込んでいたけど、はてなダイアリーAtomPubを使って少しスマートに。

<?php
#美乳
error_reporting(E_ALL);
require_once 'HTTP/Request.php';

function write_hatenadiary($account , $password , $title, $body){
    $body = htmlspecialchars(mb_convert_encoding ( $body, "UTF-8","auto"));
    $title = htmlspecialchars(mb_convert_encoding ( $title, "UTF-8","auto"));

// WSSE Authentication
    $nonce       = pack('H*', sha1(md5(time().rand())));
    $created     = date('Y-m-d\TH:i:s\Z');
    $digest      = base64_encode(pack('H*', sha1($nonce . $created . $password)));
    $wsse_text   = 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"';
    $wsse_header = sprintf($wsse_text, $account, $digest, base64_encode($nonce), $created);

    $url = "http://d.hatena.ne.jp/$account/atom/blog";

//post
    $post_data =
'<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://purl.org/atom/ns#">
<title>'.$title.'</title>
<content type="text/plain">
'.$body.'
</content>
<updated></updated>
</entry>';

    $request = new HTTP_Request($url);
    $request->setMethod(HTTP_REQUEST_METHOD_POST);
    $request->addHeader('X-WSSE', $wsse_header);
    $request->setBody($post_data);

    if (PEAR::isError($request->sendRequest())) {
        die('request failed');
    }

    $res_code = $request->getResponseCode();
    $res_body = $request->getResponseBody();
    file_put_contents(uniquetime(),$res_code."\n".$res_body);
    sleep(1);

}

function uniquetime(){
    return "log\\".date('YmdHis').".log";
    
}

?>

 以前試したときの記録。
[PC][hatena]はてなダイアリーAtomPubを使ってダイアリー書きこみ
 snoopy使って書き換えようかとも思ったけど、さほど行数は変わらないようなのでそのままPEAR::HTTPで。