public
Last active

A Toggl API client class for PHP

  • Download Gist
toggl.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
<?php
/**
* Toggl API Client
*
* A somewhat unfinished client for the Toggl API.
*
* Copyright (c) 2010 Ryan McCue
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* * Neither the name of the software nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Toggl API Client
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/
 
/**
* Toggl API Class
*
* @package Toggl API Client
*/
class Toggl {
protected $base_uri = 'http://www.toggl.com';
protected $format = 'json';
 
/**
* @var string
*/
protected $token;
 
/**
* @var string|TogglHTTPClient
*/
protected $http;
 
/**
* @var string
*/
protected $created_with;
 
/**
* Constructor
*
* @param string $token Toggle API access token
* @param string|TogglHTTPClient $client A class name implementing TogglHTTPClient (but with static methods), or a TogglHTTPClient implementation
* @param string $created_with Used in Toggl API requests to show which client created a task
*/
public function __construct($token, $client, $created_with = 'Toggl PHP API') {
$this->token = $token;
$this->http = &$client;
$this->created_with = $created_with;
}
 
/**#@+
* (Task method)
*/
/**
* Get all tasks
*
* @param int $from Timestamp to begin from
* @param int $to Timestamp to end
* @return array
*/
public function get_tasks($from = null, $to = null) {
$data = array();
if ($from !== null && $to !== null) {
$data = array(
'start_date' => date('c', $from),
'end_date' => date('c', $to),
);
}
$result = call_user_func(array($this->http, 'get'), $this->uri('tasks'), $this->headers(), $data );
return json_decode($result->body);
}
/**
* Create a task
*
* @internal Untested
* @param array $params
* @return object|array JSON result
*/
public function create_task($params) {
$workspace = ( empty($params['workspace']) ? $this->workspaces[0]['id'] : $params['workspace'] );
if (!empty($params['project'])) {
$project_id = $this->get_project($params['project']);
if (!$project_id) {
$project_id = $this->create_project($params, $workspace);
}
}
 
$params = array_merge($params, array(
'created_with' => $this->created_with,
'workspace' => array( 'id' => $workspace ),
'project' => array( 'id' => $project_id ),
));
$data = json_encode(array('task' => $params));
$result = call_user_func(array($this->http, 'post'), $this->uri('tasks'), $this->headers(), $data);
return json_decode($result->body);
}
/**
* Update a task
*
* @internal Guarantee this doesn't work
* @param int $id Task ID
* @param array $params Same as create_task
*/
public function update_task($id, $params) {
}
/**
* Delete a task
*
* @internal Untested
* @param int $id Task ID
*/
public function delete_task($id) {
return call_user_func(array($this->http, 'delete'), $this->uri('tasks/' . $id), $this->headers());
}
 
/**
* Construct the request URI to the Toggl API
*
* @param string $name Endpoint name
* @return string URI to send request to
*/
protected function uri($name) {
return $this->base_uri . '/api/v2/' . $name . '.' . $this->format;
}
 
/**
* Construct request headers
*
* @return array
*/
protected function headers() {
return array('Authorization' => 'Basic ' . base64_encode($this->token . ':api_token'));
}
}
 
/**
* The interface expected by the Toggl class
*
* @package Toggl API Client
*/
interface TogglHTTPClient {
/**#@+
* @return object Must contain 'status_code', 'success' and 'body'
*/
public function get($url, $headers, $data);
public function post($url, $headers, $data);
public function delete($url, $headers, $data);
/**#@-*/
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.