Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Created August 31, 2011 07:08
Show Gist options
  • Save fetus-hina/1182976 to your computer and use it in GitHub Desktop.
Save fetus-hina/1182976 to your computer and use it in GitHub Desktop.
Ameba blog post
<?php
date_default_timezone_set('Asia/Tokyo');
require_once('Zend/Uri.php');
require_once('Zend/Http/Client.php');
$wsse = new AmebaWsse('USER', 'PASS');
// API の URI を取得
$uris = getAmebaApiUris($wsse);
// 新しいエントリをポスト
if(!isset($uris['service.post'])) {
die('$uris["service.post"] not found');
}
postNewEntry(
$uris['service.post'],
$wsse,
'件名',
'<a href="http://twitter.com/#!/fetus_hina">本文</a>');
function getAmebaApiUris(AmebaWsse $wsse) {
$client = new Zend_Http_Client();
$resp =
$client
->setUri('http://atomblog.ameba.jp/servlet/_atom/blog')
->setHeaders('X-WSSE', $wsse->generate())
->request(Zend_Http_Client::GET);
if(!$resp->isSuccessful()) {
throw new Exception(__FUNCTION__ . ': Request failed');
}
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
if(!@$document->loadXML($resp->getBody())) {
throw new Exception(__FUNCTION__ . ': XML Parse failed');
}
$xpath = new DOMXpath($document);
$xpath->registerNamespace('n', 'http://purl.org/atom/ns#');
$links = $xpath->query('//n:link');
if($links->length < 1) {
throw new Exception(__FUNCTION__ . ': link node not found');
}
$result = array();
foreach($links as $link) {
if(!$link->hasAttribute('rel') || !$link->hasAttribute('href')) {
continue;
}
$rel = Normalizer::normalize($link->getAttribute('rel'));
$href = Normalizer::normalize($link->getAttribute('href'));
if(isset($result[$rel])) {
trigger_error(
__FUNCTION__ . ': rel "' . $rel . '" already defined',
E_USER_WARNING);
continue;
}
try {
$result[$rel] = Zend_Uri::factory($href);
} catch(Zend_Uri_Exception $e) {
trigger_error(
__FUNCTION__ . ': url "' . $href . '" looks invalid: ' . $e->getMessage(),
E_USER_WARNING);
continue;
}
}
return $result;
}
function postNewEntry(Zend_Uri $api_uri, AmebaWsse $wsse, $subject, $content) {
$client = new Zend_Http_Client();
$resp =
$client
->setUri($api_uri)
->setHeaders('X-WSSE', $wsse->generate())
->setRawData(createEntryXml($subject, $content), 'application/x.atom+xml')
->request(Zend_Http_Client::POST);
if(!$resp->isSuccessful()) {
throw new Exception(__FUNCTION__ . ': Request failed');
}
return true;
}
function createEntryXml($subject, $content) {
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$n_entry = $doc->appendChild($doc->createElementNS('http://purl.org/atom/ns#', 'entry'));
$n_title = $n_entry->appendChild($doc->createElement('title'));
$n_content = $n_entry->appendChild($doc->createElement('content'));
$n_title->appendChild($doc->createTextNode($subject));
$n_content->appendChild($doc->createCDATASection($content));
return $doc->saveXml();
}
class AmebaWsse {
private $user_name, $password;
public function __construct($user_name, $password) {
$this->user_name = $user_name;
$this->password = $password;
}
public function generate() {
$nonce = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 20));
$created = gmdate('Y-m-d\TH:i:s\Z', time());
$digest = hash('sha1', $nonce . $created . hash('md5', $this->password), true);
$wsse =
sprintf(
'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
$this->user_name,
base64_encode($digest),
base64_encode($nonce),
$created);
return $wsse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment