Created
August 28, 2013 00:22
-
-
Save chrisatomix/6360730 to your computer and use it in GitHub Desktop.
Proof of concept Toggl API MultiRequest to load currently running timers.
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 | |
/** | |
* Perform a multi-request to the Toggl API | |
* @param String $url The API URL you are requesting | |
* @param String $method HTTP Request Method (should be 'GET') | |
* @param Array $tokens An array of Toggl API Tokens and names | |
* @return Array An array of Time Entries | |
*/ | |
function multiRequest($url, $method, $tokens) { | |
// array of curl handles | |
$curly = array(); | |
// data to be returned | |
$result = array(); | |
// multi handle | |
$mh = curl_multi_init(); | |
// loop through $tokens and create curl handles | |
// then add them to the multi-handle | |
$apiurl = 'https://www.toggl.com/api/v8/'; | |
$url = $apiurl.$url; | |
foreach ($tokens as $token => $name) { | |
$curly[$name] = curl_init(); | |
curl_setopt($curly[$name], CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; | |
curl_setopt($curly[$name], CURLOPT_USERPWD, $token.":api_token"); | |
curl_setopt($curly[$name], CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curly[$name], CURLOPT_SSL_VERIFYPEER, FALSE); // Needed since Toggl's SSL fails without this. | |
curl_setopt($curly[$name], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); | |
curl_setopt($curly[$name], CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($curly[$name], CURLOPT_URL, $url); | |
curl_multi_add_handle($mh, $curly[$name]); | |
} | |
// execute the handles | |
$running = null; | |
do { | |
curl_multi_exec($mh, $running); | |
} while($running > 0); | |
// get content and remove handles | |
foreach($curly as $name => $c) { | |
$output = curl_multi_getcontent($c); | |
$output = json_decode('['.$output.']',true); | |
if(isset($output[0]) && $output[0] && count($output[0]) > 0) { | |
$result[$name] = $output[0]; | |
} | |
curl_multi_remove_handle($mh, $c); | |
} | |
// all done | |
curl_multi_close($mh); | |
return $result; | |
} | |
function getProjects() { | |
$projects = array(); | |
/* Get a list of all Toggl Projects indexed by Toggl ID. | |
Either do this via the API or a local database of Projects. | |
*/ | |
$projects[12345] = 'Website Build Project'; | |
$projects[98765] = 'Website Design Project'; | |
return $projects; | |
} | |
/* Helper function to convert a duration in seconds into HH:MM:SS */ | |
function getDuration($duration) { | |
$duration = time() + $duration; // Seconds | |
return gmdate("H:i:s", $duration); | |
} | |
/* The Toggl API URL */ | |
$start = urlencode(date('c',strtotime('today'))); | |
$end = urlencode(date('c',strtotime('tomorrow'))); | |
$url = "time_entries?start_date={$start}&end_date={$end}"; | |
/* Toggl API Tokens */ | |
$tokens = array(); | |
$tokens['abcdefghijklmnopqrstuvwxyz'] = 'John Smith'; | |
$tokens['12345678901234567890123456'] = 'Chris Atomix'; | |
/* Perform the Request */ | |
$toggltimes = multiRequest($url, 'GET', $tokens); | |
$timers = array(); | |
$projects = getProjects(); // A list of Projects | |
foreach($tokens as $token=>$name) { | |
$timer = array(); | |
$timer['name'] = $name; | |
$timer['duration'] = ' '; | |
$timer['summary'] = '(No Timer Running)'; | |
$timer['project'] = ' '; | |
$timer['running'] = false; | |
$times = $toggltimes[$name]; | |
if($times && count($times) > 0) { | |
foreach($times as $time) { | |
if($time['duration'] < 0) { | |
// Currently running | |
$timer['duration'] = getDuration($time['duration']); | |
$timer['summary'] = (!empty($time['description'])) ? $time['description'] : '(No Description)'; | |
$timer['running'] = true; | |
$timer['project'] = '(No Project Selected)'; | |
if($time['pid'] > 0 && isset($projects[$time['pid']])) { | |
$timer['project'] = $projects[$time['pid']]; | |
} | |
} | |
} | |
} | |
$timers[] = $timer; | |
} | |
/* Now build the HTML with the $timers array however you want */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please never never never ever use this line on a live website:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
If that line is needed in order to make a script work, fix it, instead of breaking security!! If your key doesn't fit your front door anymore, you fix the lock, you don't leave the door open all day. Just like that, you should fix the security on this issue as well.
It is very simple:
A good example is included in WordPresses wp-includes/class-http.php line 1411.
(Also, curl connects perfectly from my server. If you get yourself a up-to-date webserver, this wouldn't be an issue in the first place.)