Last active
December 23, 2015 01:39
-
-
Save kythin/6562107 to your computer and use it in GitHub Desktop.
Basic beginnings of a Toggl.com API version 8 PHP SDK. Currently only supports returning the logged in user, and submitting a new session to an existing workspace/project/task id. Other functions should be pretty easy to implement by copying the 'me' function for GET's, or the 'timeEntrySave' for POST's.
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 | |
/** | |
* Toggl v8 PHP SDK | |
* @author github.com/kythin ([email protected]) | |
* @version 1.0 | |
* | |
* Do-whatever-you-want-except-sell-it license, feel free to fork and improve! I think that means GPLv3 right? | |
* | |
* I've gone through some of the examples in https://github.com/toggl/toggl_api_docs/tree/master/chapters (new v8 api docs) | |
* and implemented a handful of the ones I was using in old scripts, so check them out and if you add more support please | |
* somehow send it to me so I can update this for everyone else. | |
* | |
* This should be considered to be very basic, still in development, and I can guarantee absolutely nothing. Contact me by all means, but | |
* please understand I don't offer any kind of promises to help you or to implement features or anything like that. | |
*/ | |
class Toggl { | |
public $userSecret; | |
public $toggl_base_url = "https://www.toggl.com/api/v8"; | |
public $request; | |
public $response; | |
public $method = 'GET'; | |
public $endpoint; | |
private $sdk_identifier = "PHP SDK for Toggl APIv8, by github.com/kythin"; | |
//*********************************************************** | |
// TOGGL SPECIFIC SHORTCUT FUNCTIONS | |
//*********************************************************** | |
public function me() { | |
return $this->simpleGet('/me'); | |
} | |
/** | |
* timeEntrySave | |
* $data should be a key indexed array based on the documentation for the POST /time_entries docs | |
* https://github.com/toggl/toggl_api_docs/blob/master/chapters/time_entries.md | |
*/ | |
public function timeEntrySave($data) { | |
if (!is_array($data)) { | |
die("timeEntrySave requires an array of data."); | |
} | |
if (!@$data['created_with']) { | |
$data['created_with'] = $this->sdk_identifier; | |
} | |
return $this->simplePost('/time_entries', array('time_entry'=>$data),true,true); | |
} | |
//https://github.com/toggl/toggl_api_docs/blob/master/chapters/time_entries.md#start-a-time-entry | |
public function timeEntryStart($data) { | |
if (!is_array($data)) { | |
die("timeEntrySave requires an array of data."); | |
} | |
if (!@$data['created_with']) { | |
$data['created_with'] = $this->sdk_identifier; | |
} | |
return $this->simplePost('/time_entries/start', array('time_entry'=>$data),true,true); | |
} | |
//https://github.com/toggl/toggl_api_docs/blob/master/chapters/time_entries.md#stop-a-time-entry | |
public function timeEntryStop($id) { | |
return $this->simpleOther('PUT',"/time_entries/$id/stop",array(),true); | |
} | |
//*********************************************************** | |
// END OF TOGGL SPECIFIC SHORTCUT FUNCTIONS | |
//*********************************************************** | |
/** | |
* __Construct | |
*/ | |
public function __construct($secret){ | |
$this->userSecret = $secret; | |
} | |
public function checkJSON() { | |
if (!json_decode($this->response,TRUE)) { | |
die("The server returned non-JSON string: ".print_r($this->response,true)); | |
} | |
} | |
/** | |
* simpleGet | |
* Used when the action is a very simple dump of the data provided by the endpoint (most GET requests) | |
* @return array | |
*/ | |
public function simpleGet($endpoint, $postargs = false, $decode = true) { | |
//simply refresh the request and point it at the endpoint. | |
$this->request = array(); | |
$this->method = 'GET'; | |
$this->endpoint = $endpoint; | |
if ($postargs) { | |
$this->request = $postargs; | |
} | |
if ($this->sendRequest($endpoint)) { | |
//if it's successful, the response will be populated with something. | |
if ($decode) { | |
$this->checkJSON(); | |
$response = json_decode($this->response, TRUE); | |
} else { | |
$response = $this->response; | |
} | |
return $response; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* simplePost | |
* Used when the action is a very simple dump of the data provided by the endpoint (for POST endpoints) | |
* @return array | |
*/ | |
public function simplePost($endpoint, $postargs, $decode = true, $sendJSON = false) { | |
return $this->simpleOther('POST',$endpoint, $postargs, $decode); | |
} | |
/** | |
* simpleGet | |
* Used when the action is a very simple dump of the data provided by the endpoint (most GET requests) | |
* @return array | |
*/ | |
public function simpleOther($method, $endpoint, $postargs = false, $decode = true) { | |
//simply refresh the request and point it at the endpoint. | |
$this->method = $method; | |
$this->request = $postargs; | |
$this->endpoint = $endpoint; | |
if ($this->sendRequest($endpoint, $sendJSON)) { | |
//if it's successful, the response will be populated with something. | |
if ($decode) { | |
$this->checkJSON(); | |
$response = json_decode($this->response, TRUE); | |
} else { | |
$response = $this->response; | |
} | |
return $response; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* request | |
* Generic function to send whatever request has been prepared, and save the response. | |
* @return bool true for success or false for fail | |
*/ | |
private function sendRequest($endpoint, $sendJSON = false) { | |
$fullURL = $this->toggl_base_url . $endpoint; | |
//init the curl request | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_USERPWD, $this->userSecret.":api_token"); | |
if ($sendJSON) { | |
$qry_str = json_encode($this->request); | |
//echo $qry_str; | |
} else { | |
$qry_str = http_build_query($this->request); | |
} | |
if ($this->method == "POST") { | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str); | |
if ($sendJSON) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
} | |
} elseif ($this->method == "GET") { | |
$fullURL .= '?'.$qry_str; | |
} elseif ($this->method == "PUT") { | |
curl_setopt($ch, CURLOPT_PUT, 1); | |
//curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str); | |
if ($sendJSON) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
} | |
} | |
curl_setopt($ch, CURLOPT_URL, $fullURL); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, '30'); | |
//send it off & save the result | |
$content = trim(curl_exec($ch)); | |
curl_close($ch); | |
if ($content) { | |
$this->response = $content; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
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 | |
require "toggl_v8_sdk.php"; | |
define('TOGGL_API', 'YOUR API KEY GOES HERE'); | |
$toggl = new Toggl(TOGGL_API); | |
print_r($toggl->me()); //show the person logged in | |
//now let's save a time session. | |
$data = array("duration"=>(60*10), //10mins | |
"billable"=>false, | |
"start"=>date("c",strtotime("-10 minutes")), //10mins ago | |
"pid"=>'YOUR PROJECT ID GOES HERE', | |
"description"=>"Time spent playing WoW", | |
"duronly"=>true, //only show the duration, not start & stops. | |
"tags"=>array('games', 'warcraft'), | |
); | |
print_r($toggl->timeEntrySave($data)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment