Created
December 5, 2013 00:46
-
-
Save monry/7798299 to your computer and use it in GitHub Desktop.
色々イケてないけど、ChatWork API と会話するためのクラス書いてみた。
まだ全部のメソッドを補完したワケじゃ無いけどねw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ChatWork.php | |
* | |
* @author Tetsuya MORI | |
*/ | |
/** | |
* ChatWork とキャッキャウフフする | |
* | |
* @author Tetsuya MORI | |
*/ | |
class ChatWork { | |
/** @const string ChatWork の API URL フォーマット */ | |
const URL_FORMAT = 'https://api.chatwork.com/v1%s?%s'; | |
/** @const string HTTPメソッド: GET */ | |
const HTTP_METHOD_GET = 'GET'; | |
/** @const string HTTPメソッド: POST */ | |
const HTTP_METHOD_POST = 'POST'; | |
/** @const string HTTPメソッド: PUT */ | |
const HTTP_METHOD_PUT = 'PUT'; | |
/** @const string HTTPメソッド: DELETE */ | |
const HTTP_METHOD_DELETE = 'DELETE'; | |
/** @var array インスタンスメソッド名と API メソッドの対応表 */ | |
private static $api_method_list = array( | |
'getMe' => '/me', | |
'getRoomList' => '/rooms', | |
'getRoomDetail' => '/rooms/%d', | |
'getContactList' => '/contacts', | |
'postMessage' => '/rooms/%d/messages', | |
); | |
/** @var array インスタンスメソッド名と HTTP メソッドの対応表 */ | |
private static $http_method_list = array( | |
'getMe' => self::HTTP_METHOD_GET, | |
'getRoomList' => self::HTTP_METHOD_GET, | |
'getRoomDetail' => self::HTTP_METHOD_GET, | |
'getContactList' => self::HTTP_METHOD_GET, | |
'postMessage' => self::HTTP_METHOD_POST, | |
); | |
/** @var \ChatWork 自身のインスタンス */ | |
private static $instance = null; | |
/** @var string API のトークン */ | |
private $api_token = ''; | |
/** | |
* コンストラクタ | |
* Singleton 実装にするために private | |
* | |
* @param string $api_token API トークン | |
*/ | |
private function __construct($api_token) { | |
$this->api_token = $api_token; | |
} | |
/** | |
* 初期化処理 | |
* API トークンを設定して Singleton インスタンスを構築 | |
* | |
* @param string $api_token APIトークン | |
*/ | |
public static function initialize($api_token) { | |
if (null === self::$instance) { | |
self::$instance = new self($api_token); | |
} | |
} | |
/** | |
* チャットルームリストを取得 | |
* | |
* @return array 部屋リスト | |
*/ | |
public static function getRoomList() { | |
list($http_status_list, $response_header_list, $response_body) = self::$instance->_run( | |
__FUNCTION__ | |
); | |
$room_list = json_decode($response_body, true); | |
foreach ($room_list as $room) { | |
echo sprintf('%10d: [%6s][%6s] %s', $room['room_id'], $room['type'], $room['role'], $room['name']); | |
} | |
return $room_list; | |
} | |
/** | |
* コンタクトリストを取得 | |
* | |
* @return array コンタクトリスト | |
*/ | |
public static function getContactList() { | |
list($http_status_list, $response_header_list, $response_body) = self::$instance->_run( | |
__FUNCTION__ | |
); | |
$contact_list = json_decode($response_body, true); | |
foreach ($contact_list as $contact) { | |
echo sprintf('%10d: %s', $contact['account_id'], $contact['name']); | |
} | |
return $contact_list; | |
} | |
/** | |
* 自身の情報を取得 | |
* | |
* @return array 自身の情報 | |
*/ | |
public static function getMe() { | |
list($http_status_list, $response_header_list, $response_body) = self::$instance->_run( | |
__FUNCTION__ | |
); | |
$me = json_decode($response_body, true); | |
echo sprintf('%10d: %s', $me['account_id'], $me['name']); | |
return $me; | |
} | |
/** | |
* メッセージを送信する | |
* | |
* @param integer $room_id チャットルームID | |
* @param string $message 送信するメッセージ | |
* @param array $parameter_list 追加パラメータ | |
*/ | |
public static function postMessage($room_id, $message, $parameter_list = array()) { | |
$parameter_list['body'] = $message; | |
list($http_status_list, $response_header_list, $response_body) = self::$instance->_run( | |
__FUNCTION__ | |
, $room_id | |
, $parameter_list | |
); | |
} | |
/** | |
* パラメータを分解して cURL をキック | |
* | |
* @return array 実行結果 | |
*/ | |
private function _run() { | |
$argument_list = func_get_args(); | |
if (!is_array($argument_list) || !count($argument_list)) { | |
return false; | |
} | |
// 最初の引数をメソッド名とみなす | |
$method_name = array_shift($argument_list); | |
// 最後の引数をリクエストパラメータとみなす | |
$parameter_list = (count($argument_list) > 1 ? array_pop($argument_list) : array()); | |
// 残りの引数をAPIメソッドのパス用パラメータとみなす | |
$id_list = (count($argument_list) ? $argument_list : array()); | |
return $this->_execute( | |
self::$http_method_list[$method_name] | |
, $this->_createAPIMethodPath($method_name, $id_list) | |
, $parameter_list | |
); | |
} | |
/** | |
* API メソッドのパスを構築 | |
* | |
* @param string $method_name メソッド名 | |
* @param array $id_list API メソッドを置換するためのIDリスト | |
* @return string APIメソッド名 | |
*/ | |
private function _createAPIMethodPath($method_name, $id_list = array()) { | |
if (is_array($id_list) && count($id_list)) { | |
return vsprintf(self::$api_method_list[$method_name], $id_list); | |
} | |
return self::$api_method_list[$method_name]; | |
} | |
/** | |
* cURL を実行する | |
* | |
* @param string $http_method HTTP メソッド | |
* @param string $api_method API メソッドパス | |
* @param array $parameter_list パラメータリスト | |
* @return array 実行結果 | |
*/ | |
private function _execute($http_method, $api_method, $parameter_list = array()) { | |
$parameter_list['method'] = $http_method; | |
$query_string = http_build_query($parameter_list); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, sprintf(self::URL_FORMAT, $api_method, $query_string)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($curl, CURLOPT_HEADER, true); | |
curl_setopt($curl, CURLOPT_MAXREDIRS, 3); | |
curl_setopt( | |
$curl | |
, CURLOPT_HTTPHEADER | |
, array( | |
"X-ChatWorkToken: {$this->api_token}", | |
) | |
); | |
$response = curl_exec($curl); | |
return self::_parseResponseData($response); | |
} | |
/** | |
* HTTPレスポンスの情報をパースする | |
* | |
* @param string $response レスポンス | |
* @return array パースした情報 [HTTPステータスの情報, レスポンスヘッダの配列, レスポンスボディ] | |
*/ | |
private static function _parseResponseData($response) { | |
$response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response); | |
if (false === strpos($response, "\r\n")) { | |
$response_header = $response; | |
$response_body = ''; | |
} else { | |
list($response_header, $response_body) = preg_split('/(\r\n){2}/', $response, 2); | |
} | |
$response_header_string_list = preg_split('/\r\n/', $response_header); | |
$http_status_line = array_shift($response_header_string_list); | |
$http_status_list = array( | |
'protocol' => 'HTTP', | |
'version' => '', | |
'status_code' => 0, | |
'status_reason' => '', | |
); | |
if (preg_match('/^HTTP\/(\d\.\d) (\d+) (.*)$/', $http_status_line, $matches)) { | |
$http_status_list['version'] = $matches[1]; | |
$http_status_list['status_code'] = intval($matches[2]); | |
$http_status_list['status_reason'] = $matches[3]; | |
} | |
$response_header_list = array(); | |
$response_header_name = ''; | |
$last_key = ''; | |
foreach ($response_header_string_list as $response_header_string) { | |
if (false === strpos($response_header_string, ':')) { | |
$response_header_list[$last_key] .= "\n{$response_header_string}"; | |
} else { | |
list($response_header_name, $response_header_value) = preg_split('/: ?/', $response_header_string, 2); | |
$response_header_list[$response_header_name] = $response_header_value; | |
} | |
$last_key = $response_header_name; | |
} | |
return array($http_status_list, $response_header_list, $response_body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment