Created
January 26, 2015 13:05
-
-
Save lokielse/952a53ea181036587a5a to your computer and use it in GitHub Desktop.
Tower Rest API PHP SDK
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 | |
/** | |
* Tower Rest API v1.0 | |
* PHP SDK | |
*/ | |
namespace Tower; | |
class TowerRest | |
{ | |
/** | |
* 客户端ID | |
* | |
* @var | |
*/ | |
protected $clientId; | |
/** | |
* 客户端密钥 | |
* | |
* @var | |
*/ | |
protected $clientSecret; | |
/** | |
* 用户名 | |
* | |
* @var | |
*/ | |
protected $userName; | |
/** | |
* 用户密码 | |
* | |
* @var | |
*/ | |
protected $userPass; | |
/** | |
* 访问token | |
* | |
* @var | |
*/ | |
protected $accessToken; | |
/** | |
* 网关 | |
* | |
* @var string | |
*/ | |
public static $endPoint = 'https://beta1.tower.im/api/v2/'; | |
public function __construct($clientId, $clientSecret, $userName, $userPass) | |
{ | |
$this->clientId = $clientId; | |
$this->clientSecret = $clientSecret; | |
$this->userName = $userName; | |
$this->userPass = $userPass; | |
} | |
/** | |
* 授权 | |
* | |
* @return mixed | |
*/ | |
public function auth() | |
{ | |
$data = array( | |
'grant_type' => 'password', | |
'username' => $this->userName, | |
'password' => $this->userPass | |
); | |
$url = sprintf('%s%s', self::$endPoint, 'oauth/token'); | |
$userPass = sprintf('%s:%s', $this->clientId, $this->clientSecret); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_USERPWD, $userPass); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$content = curl_exec($ch); | |
return json_decode($content); | |
} | |
/** | |
* 使用token进行访问 | |
* | |
* @param $uri | |
* @param array $data | |
* @param bool $post | |
* | |
* @return mixed | |
*/ | |
protected function api($uri, $data = array(), $post = false) | |
{ | |
$url = sprintf('%s%s', self::$endPoint, $uri); | |
if ($data && !$post) { | |
$url .= '?=' . http_build_query($data); | |
} | |
$headers = array('Authorization: Bearer ' . $this->accessToken); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
if ($post) { | |
curl_setopt($ch, CURLOPT_POST, $post); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$content = curl_exec($ch); | |
return json_decode($content); | |
} | |
public function setAccessToken($accessToken) | |
{ | |
$this->accessToken = $accessToken; | |
} | |
/** | |
* 获取团队信息 | |
* | |
* @return mixed | |
*/ | |
public function getTeamInformation($teamGUID) | |
{ | |
return $this->api("teams/$teamGUID", array()); | |
} | |
/** | |
* 获取团队项目列表 | |
* | |
* @return mixed | |
*/ | |
public function getTeamProjects($teamGUID) | |
{ | |
return $this->api("teams/$teamGUID/projects", array()); | |
} | |
/** | |
* 获取项目成员列表 | |
* | |
* @return mixed | |
*/ | |
public function getProjectMembers($projectGUID) | |
{ | |
return $this->api("projects/$projectGUID/members", array()); | |
} | |
/** | |
* 获取项目任务清单列表 | |
* | |
* @return mixed | |
*/ | |
public function getProjectTodoLists($projectGUID) | |
{ | |
return $this->api("projects/$projectGUID/todolists", array()); | |
} | |
/** | |
* 获取指定任务清单下的任务列表 | |
* | |
* @return mixed | |
*/ | |
public function getTodoListTodos($todoListGUID, $completed = false, $count = 20, $since = '', $till = '') | |
{ | |
$data = array( | |
'since' => $since, | |
'till' => $till, | |
'count' => $count, | |
'completed' => $completed ? 1 : 0, | |
); | |
return $this->api("todolists/$todoListGUID/todos", $data); | |
} | |
/** | |
* 创建任务清单 | |
* | |
* @param $projectGUID | |
* @param $title | |
* | |
* @return mixed | |
*/ | |
public function createTodoList($projectGUID, $title) | |
{ | |
return $this->api("projects/$projectGUID/todolists", array('title' => $title), true); | |
} | |
/** | |
* 创建任务 | |
* | |
* @param $todoListGUID | |
* @param $content | |
* @param $assignee_guid | |
* @param $due_at | |
* | |
* @return mixed | |
*/ | |
public function createTodo($todoListGUID, $content, $assignee_guid, $due_at) | |
{ | |
$data = array( | |
'content' => $content, | |
'assignee_guid' => $assignee_guid, | |
'due_at' => $due_at, | |
); | |
return $this->api("todolists/$todoListGUID/todos", $data, true); | |
} | |
/** | |
* 创建任务清单评论 | |
* | |
* @param $todoListGUID | |
* @param $content | |
* | |
* @return mixed | |
*/ | |
public function createTodoListComment($todoListGUID, $content) | |
{ | |
return $this->api("todolists/$todoListGUID/comments ", array('content' => $content), true); | |
} | |
/** | |
* 创建任务评论 | |
* | |
* @param $todoGUID | |
* @param $content | |
* | |
* @return mixed | |
*/ | |
public function createTodoComment($todoGUID, $content) | |
{ | |
return $this->api("todos/$todoGUID/comments ", array('content' => $content), true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment