TaskoのAPI扱うクラス書いた
使い方
<?php $userid = 'Setting画面で得られるid'; $token = 'Setting画面で得られるtoken'; $tasko = new Service_Tasko($userid, $token); // 全てのペーパーの名前を得る $tasko->listAllPapers(); // 指定した名前のペーパーの本文を得る $tasko->getPaper('memo'); // 指定した名前のペーパーの名前を変える $tasko->renamePaper('memo', 'todo'); // 指定した名前のペーパーの本文を書き換える $tasko->editPaper('todo', 'hogepiyo'); // 新しいペーパーを作成する $tasko->newPaper('new_paper', 'contents'); // ペーパーを削除する $tasko->deletePaper('todo');
ソース
XMLRPC扱うものを書いたのは初めてなので変なところもあるかと思います。
突っ込み大歓迎です。
追記:
リクエストヘッダを思いっきり間違えていたので修正(2/22 22:51)
改めてXML-RPCの仕様書見てたら足りないリクエストヘッダがあったのでさらに修正(2/22 23:36)
<?php class Service_Tasko { protected $userid; protected $token; private static $debugging = false; const REQUEST_URI = 'http://taskodone.com/api'; public function __construct($userid, $token) { $this->setUserID($userid); $this->setToken($token); } public function setUserID($userid) { $this->userid = $userid; } public function setToken($token) { $this->token = $token; } /** * 全てのペーパー名を返す * * @return array */ public function listAllPapers() { $xmlElement = $this->request('papers'); $this->debug($xmlElement); $buf = array(); foreach ($xmlElement->params->param->value->array->data->value as $value) $buf[] = (string) $value->string; return $buf; } /** * 指定した名前のペーパーの本文を返す * * @param string $paperName * @return string */ public function getPaper($paperName) { $xmlElement = $this->request('paper', array($paperName)); $this->debug($xmlElement); $result = (string) $xmlElement->params->param->value->string; return $result; } /** * ペーパーの名前を変える * * @param string $oldName * @param string $newName * @return bool */ public function renamePaper($oldName, $newName) { $xmlElement = $this->request('rename', array($oldName, $newName)); $this->debug($xmlElement); if (isset($xmlElement->params->param->value->nil)) return false; return true; } /** * ペーパーを編集する * * @param string $paperName * @param string $contents * @return bool */ public function editPaper($paperName, $contents) { $xmlElement = $this->request('edit', array($paperName, $contents)); $this->debug($xmlElement); if (isset($xmlElement->params->param->value->nil)) return false; return true; } /** * 新しいペーパーを作成する * * @param string $paperName * @param string $contents * @return bool */ public function newPaper($paperName, $contents= '') { $xmlElement = $this->request('new', array($paperName, $contents)); $this->debug($xmlElement); if (isset($xmlElement->params->param->value->nil)) return false; return true; } /** * ペーパーを削除する * * @param string $paperName * @return bool */ public function deletePaper($paperName) { $xmlElement = $this->request('delete', array($paperName)); $this->debug($xmlElement); if (isset($xmlElement->params->param->value->nil)) return false; return true; } /** * リクエストの結果を返す * * @param string $methodName * @param array $params * @return SimpleXMLElement */ protected function request($methodName, $params = array()) { $content = $this->buildRequestContent($methodName, $params); $context = stream_context_create( array('http' => array( 'method' => 'POST', 'header' => 'Content-type: text/xml' . "\r\n" . 'Content-length: ' . strlen($content), 'content' => $content ) ) ); $result = @file_get_contents(self::REQUEST_URI, null, $context); if ($result === false) throw new Exception('file_get_contents() is fail'); $result = new SimpleXMLElement($result); if (isset($result->fault)) { $errorMsgs = array(); foreach ($result->fault->value->struct->member as $member) { $errorMsgs[] = (string) $member->name . ':' . (string) $member->value->string . PHP_EOL; } throw new Exception(implode('', $errorMsgs)); } return $result; } /** * リクエストの中身を作って返す * requestメソッド内で使われる * * @param string $methodName * @param array $params * @return string */ protected function buildRequestContent($methodName, $params) { if (!is_array($params)) throw new Exception('invalid argument'); $buf = array(); $buf[] = '<methodCall>'; $buf[] = '<methodName>' . $methodName . '</methodName>'; $buf[] = '<params>'; $buf[] = '<param><value><int>' . $this->userid . '</int></value></param>'; $buf[] = '<param><value><string>' . $this->token . '</string></value></param>'; foreach ($params as $param) { $buf[] = '<param><value><string>'; $buf[] = htmlspecialchars($param, ENT_QUOTES, 'UTF-8'); $buf[] = '</string></value></param>'; } $buf[] = '</params>'; $buf[] = '</methodCall>'; return implode('', $buf); } private static function debug() { if (self::$debugging) { $args = func_get_args(); $trace = debug_backtrace(); println('##' . $trace[1]['class'] . '::' . $trace[1]['function']); call_user_func_array('var_dump', $args); } } }