Created
October 7, 2015 11:39
-
-
Save s-brand/fbf6c4ccdd4d9bf90fa9 to your computer and use it in GitHub Desktop.
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 | |
function tggl_req($user, $path, $method="GET" ,$postFields = array()){ | |
$ch = curl_init('https://www.toggl.com/api/v8/'.$path); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_USERPWD, $user . ":api_token"); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
if(count($postFields) > 0){ | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); | |
} | |
if($method !== "GET"){ | |
curl_setopt(curl_exec, CURLOPT_CUSTOMREQUEST, $method); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$response = curl_exec($ch); | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$header = substr($response, 0, $header_size); | |
$body = substr($response, $header_size); | |
curl_close($ch); | |
$return = (object) array(); | |
$return->header = $header; | |
$return->body = json_decode($body); | |
return $return; | |
} | |
$currentTaskUrl = 'time_entries/current'; | |
$user = ($_GET['user']) ? $_GET['user'] : null; | |
if(!$user) die("User not found"); | |
$currentTask = tggl_req($user,$currentTaskUrl)->body->data; | |
if($currentTask->id){ | |
tggl_req($user,'time_entries/'. $currentTask->id .'/stop','PUT'); | |
} else { | |
echo "No running task!"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment