rhaco: はてな用ライブラリ

rhacoはてなのサービスを利用するためのライブラリです。arbo.network.services.HatenaAPIが使いにくかったから作った。

Hatena.phpをlibrary/mashiroに保存

class Hatena{
	
	function Hatena($user, $pass){
		$this->user = $user;
		$this->pass = $pass;
		$this->req = null;
		$this->image = null;
		$this->wsse_header = null;
	}
	function make_wsse(){
		$nonce       = pack('H*', sha1(md5(time().rand().posix_getpid()))); // for php4
		$created     = date('Y-m-d\TH:i:s\Z');
		$digest      = base64_encode(pack('H*', sha1($nonce . $created . $this->pass))); // for php4
		$wsse_text   = 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"';
		$this->wsse_header = sprintf($wsse_text, $this->user, $digest, base64_encode($nonce), $created);
	}
	function request($url, $post_data){
		
		$this->make_wsse();
		
		$header = array(
			'X-WSSE' => $this->wsse_header,
			'rawdata' => $post_data
		);
		list($this->res_headers, $this->res_body) = Http::Request($url, 'POST', $header);
		
		return 1;
		
	}
	function base64_image($image_path){
		$this->image = base64_encode(file_get_contents($image_path));
	}
}

index.php

require('__init__.php');
Rhaco::import('mashiro.Hatena');

$h_user = 'username';
$h_pass = 'password';
$hatena = new Hatena($h_user, $h_pass);

// ブックマークをAPIから追加する
$post_data  = '<entry xmlns="http://purl.org/atom/ns#">';
$post_data .= '<link rel="related" type="text/html" href="http://www.example.com/" />';
$post_data .= '<summary type="text/plain">サンプルコメントです</summary>';
$post_data .= '</entry>';

$hatena->request('http://b.hatena.ne.jp/atom/post', $post_data);