Created
October 4, 2012 20:51
-
-
Save mloberg/3836369 to your computer and use it in GitHub Desktop.
PHP Sifter API Class
This file contains hidden or 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 | |
/** | |
* A simple PHP class to interact with the Sifter API. | |
*/ | |
class Sifter | |
{ | |
/** | |
* Sifter account hostname (example.sifterapp.com) | |
* | |
* @var string | |
*/ | |
private $hostname; | |
/** | |
* Sifter token. | |
* | |
* @var string | |
*/ | |
private $token = ''; | |
/** | |
* Interact with the Sifter API | |
* | |
* @param string $host Sifter account hostname | |
* @param string $token Sifter API token | |
*/ | |
public function __construct($host, $token) | |
{ | |
$this->hostname = $host; | |
$this->token = $token; | |
} | |
/** | |
* Return Sifter projects. | |
* | |
* @return array Sifter projects | |
*/ | |
public function projects() | |
{ | |
$projects = $this->request('/api/projects'); | |
return $projects['projects']; | |
} | |
/** | |
* Get a project details. | |
* | |
* @param string|integer|array $id Project or project ID | |
* @return array Project details | |
*/ | |
public function project($id) | |
{ | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$project = $this->request('/api/projects/' . $id); | |
return $project['project']; | |
} | |
/** | |
* Get a project milestones. | |
* | |
* @param string|integer|array $id Project or project ID | |
* @return array Project milestones | |
*/ | |
public function milestones($id) | |
{ | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$milestones = $this->request('/api/projects/' . $id . '/milestones'); | |
return $milestones['milestones']; | |
} | |
/** | |
* Get a project categories. | |
* | |
* @param string|integer|array $id Project or project ID | |
* @return array Project categories | |
*/ | |
public function categories($id) | |
{ | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$categories = $this->request('/api/projects/' . $id . '/categories'); | |
return $categories['categories']; | |
} | |
/** | |
* Get a project people. | |
* | |
* @param string|integer|array $id Project or project ID | |
* @return array Project people | |
*/ | |
public function people($id) | |
{ | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$people = $this->request('/api/projects/' . $id . '/people'); | |
return $people['people']; | |
} | |
/** | |
* Get a project issues. | |
* | |
* @param string|integer|array $id Project or project ID | |
* @return array Project issues | |
*/ | |
public function issues($id) | |
{ | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$issues = $this->request('/api/projects/' . $id . '/issues'); | |
return $issues['issues']; | |
} | |
/** | |
* Get more detail about an issue. | |
* | |
* @param string|integer|array $project Project or project ID | |
* @param iteger $id Issue ID | |
* @return array Project issues | |
*/ | |
public function issue($project, $id) | |
{ | |
if (is_array($project)) { | |
$project = end(explode('/', $project['url'])); | |
} | |
if (is_array($id)) { | |
$id = end(explode('/', $id['url'])); | |
} | |
$issue = $this->request('/api/projects/' . $project . '/issues/' . $id); | |
return $issue['issue']; | |
} | |
/** | |
* Make a request to the Sifter API. | |
* | |
* @param string $endpoint API endpoint | |
* @throws Exception If error is returned | |
* @return array API response | |
*/ | |
private function request($endpoint) | |
{ | |
$headers = array( | |
'X-Sifter-Token: ' . $this->token, | |
'Accept: application/json' | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://' . $this->hostname . $endpoint); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$raw = curl_exec($ch); | |
curl_close($ch); | |
$response = json_decode($raw, true); | |
if (is_null($response)) { | |
throw new Exception(strip_tags($raw)); | |
} elseif ($response['error']) { | |
throw new Exception($response['error'] . ': ' . $response['detail']); | |
} | |
return $response; | |
} | |
} |
This file contains hidden or 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 | |
// require the class | |
require 'sifter.php'; | |
// create a new instance | |
$sifter = new Sifter('example.sifterapp.com', 'your-token'); | |
// Get all projects | |
$projects = $sifter->projects(); | |
// You can pass a project array (from projects) | |
// or a project id for the rest of these methods | |
// Get single project details | |
$project = $sifter->project($projects[0]); | |
// Get a project's milestones | |
$milestones = $sifter->milestones(1); | |
// Get a project's categories | |
$categories = $sifter->categories(1); | |
// Get all people assigned to a project | |
$people = $sifter->people(1); | |
// Get all issues for a project | |
$issues = $sifter->issues(1); | |
// Get a single issue | |
// First option is project id, second is issue id | |
$issue = $sifter->issue(1, $issues[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment