Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Toggl API client class for PHP
<?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);
/**#@-*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.