How to use PHP to access the API to login, and get all entries for one day.
Can you please provide documentation on how to use PHP with Toggl? I know a lot of API's out there have examples of sample source code to show how to make it work.
Here's what I currently have:
<?php // This will be where we test the api to Toggl
$send_data = array();
$send_data['start_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['end_date_ms'] = strtotime(date("m/d/Y 23:59:59")) * 1000;
$send_data['version'] = '0.16.12';
$send_data['tzoffset'] = '0';
$send_data['user_login'] = '{myloginaccount}';
$send_data['user_password'] = '{myloginpassword}';
$login = curl_init();
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($login, CURLOPT_URL, 'http://www.toggl.com/user/login');
curl_setopt($login, CURLOPT_POSTFIELDS, json_encode($send_data));
$login_details = json_decode(curl_exec($login));
echo "Login Details <br ?>";
echo "<pre>";
print_r($login_details);
echo "</pre>";
$get_details = curl_init();
curl_setopt($get_details, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_details, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($get_details, CURLOPT_URL, 'http://www.toggl.com/task/list');
curl_setopt($get_details, CURLOPT_POSTFIELDS, json_encode($send_data));
$list_details = json_decode(curl_exec($get_details));
echo "List Details<br />";
echo "<pre>";
print_r($list_details);
echo "</pre>";
curl_close($login);
curl_close($get_details);
?>
Dump of $send_data.
Array
(
[start_date_ms] => 1240372800000
[end_date_ms] => 1240459199000
[version] => 0.16.12
[tzoffset] => 0
[user_login] => {myloginaccount}
[user_password] => {myloginpassword}
)
The login looks great, the tasks list doesn't work at all.
Thanks,
Chad
EDIT: some of the items above are not displaying on this site correctly.
Here's what I currently have:
<?php // This will be where we test the api to Toggl
$send_data = array();
$send_data['start_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['end_date_ms'] = strtotime(date("m/d/Y 23:59:59")) * 1000;
$send_data['version'] = '0.16.12';
$send_data['tzoffset'] = '0';
$send_data['user_login'] = '{myloginaccount}';
$send_data['user_password'] = '{myloginpassword}';
$login = curl_init();
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($login, CURLOPT_URL, 'http://www.toggl.com/user/login');
curl_setopt($login, CURLOPT_POSTFIELDS, json_encode($send_data));
$login_details = json_decode(curl_exec($login));
echo "Login Details <br ?>";
echo "<pre>";
print_r($login_details);
echo "</pre>";
$get_details = curl_init();
curl_setopt($get_details, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_details, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($get_details, CURLOPT_URL, 'http://www.toggl.com/task/list');
curl_setopt($get_details, CURLOPT_POSTFIELDS, json_encode($send_data));
$list_details = json_decode(curl_exec($get_details));
echo "List Details<br />";
echo "<pre>";
print_r($list_details);
echo "</pre>";
curl_close($login);
curl_close($get_details);
?>
Dump of $send_data.
Array
(
[start_date_ms] => 1240372800000
[end_date_ms] => 1240459199000
[version] => 0.16.12
[tzoffset] => 0
[user_login] => {myloginaccount}
[user_password] => {myloginpassword}
)
The login looks great, the tasks list doesn't work at all.
Thanks,
Chad
EDIT: some of the items above are not displaying on this site correctly.
1
person has this question
I have this question, too!
Tell me when someone answers.
The more people who ask this question, the more it gets noticed.
The more people who ask this question, the more it gets noticed.
The company marked this question as answered.
Create a customer community for your own organization
Plans starting at $19/month
-
Inappropriate?I am not actually ending the script at echo "Login Details", that's website acting funky and being goofy.
-
Inappropriate?You didn't handle cookies.
Toggl creates a _toggl_session cookie upon logging in, you need to pass that along to any subsequent requests.
Code here:
<?php // This will be where we test the api to Toggl
$target = "http://www.toggl.com";
$cookie_file = 'cookies.txt';
$send_data = array();
$send_data['start_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['end_date_ms'] = strtotime(date("m/d/Y 23:59:59")) * 1000;
$send_data['version'] = '0.16.12';
$send_data['tzoffset'] = '0';
$send_data['user_login'] = {username or email};
$send_data['user_password'] = {password};
$login = curl_init();
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($login, CURLOPT_URL, $target.'/user/login');
curl_setopt($login, CURLOPT_POSTFIELDS, json_encode($send_data));
// This is added
curl_setopt($login, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($login, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($login, CURLOPT_COOKIEJAR, $cookie_file);
// ***
$login_details = json_decode(curl_exec($login));
echo "<stron?>Login Details<br />";
echo "<pre>";
print_r($login_details);
echo "</pre>";
curl_close($login); // This is moved up here (it was at the end)
$get_details = curl_init();
curl_setopt($get_details, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_details, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($get_details, CURLOPT_URL, $target.'/task/list');
curl_setopt($get_details, CURLOPT_POSTFIELDS, json_encode($send_data));
// This is added
curl_setopt($get_details, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($get_details, CURLOPT_COOKIEJAR, $cookie_file);
// ***
$list_details = json_decode(curl_exec($get_details));
echo "<strong>List Details</strong><br />";
echo "<pre>";
print_r($list_details);
echo "</pre>";
curl_close($get_details);
?> -
Inappropriate?Hey Sander,
Thanks for the reply. You guys should really update your API documentation then. Nowhere does it say to track a cookies file or anything. I understand how it would do that, or better yet have the /task/list do a login if the information is there. That way you could call it with just that and get the data.
I had to modify the target to: "http://www.toggl.com" but other than that it worked.
Thanks,
Chad
I’m happy that Sander helped me.
-
Inappropriate?Hey Sander,
Got another issue that I believe is related to the API not working spot on IMO. As you can see from the code below it should pull everything from midnight until 11:59:59 AM (giving us the morning hours used). It runs fine, but the problem is that it pulls the entire day. Which is not good. Any ideas why that might be?
<?php // This will be where we test the api to Toggl
$target = "http://www.toggl.com";
$cookie_file = 'cookies.txt';
// Remove the current cookie file
unlink($cookie_file);
$send_data = array();
$send_data['start_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['end_date_ms'] = strtotime(date("m/d/Y 11:59:59")) * 1000;
$send_data['version'] = '0.16.12';
$send_data['tzoffset'] = '0';
$send_data['user_login'] = "{myaccount}";
$send_data['user_password'] = "{mypassword}";
$login = curl_init();
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($login, CURLOPT_URL, $target.'/user/login');
curl_setopt($login, CURLOPT_POSTFIELDS, json_encode($send_data));
// This is added
curl_setopt($login, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($login, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($login, CURLOPT_COOKIEJAR, $cookie_file);
// ***
$login_details = json_decode(curl_exec($login));
curl_close($login); // This is moved up here (it was at the end)
$get_details = curl_init();
curl_setopt($get_details, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_details, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($get_details, CURLOPT_URL, $target.'/task/list');
curl_setopt($get_details, CURLOPT_POSTFIELDS, json_encode($send_data));
// This is added
curl_setopt($get_details, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($get_details, CURLOPT_COOKIEJAR, $cookie_file);
// ***
$list_details = json_decode(curl_exec($get_details));
curl_close($get_details);
$currentTime = 0;
$theEntries = array();
foreach($list_details?>content as $tmp_detail)
{
if($tmp_detail->duration > 0)
{
// Break down the number into .25 increments
$theAmount = number_format(((($tmp_detail->duration / 1000) / 60) / 15)) * .25;
$theEntries[$tmp_detail->description] = $theAmount;
$currentTime = $currentTime + $theAmount;
}else{
// This is the end all so it must equal 4 or 8 depending on when we run this.
$offset = 4 - $currentTime;
$theEntries[$tmp_detail->description] = $offset;
}
}
echo "";
";
print_r($theEntries);
echo "
?>
Thanks,
Chad -
Inappropriate?I investigated this matter and found that Toggl adds 24 hours to the end date ms. You can subtract 24 hours (24*3600000 ms) from end_date_ms to work around this unfortunate situation:
$send_data['end_date_ms'] = strtotime(date("m/d/Y 11:59:59")) * 1000 - 24 * 3600000; -
Inappropriate?i'd like to setup a php script to retrieve tasks via json, too. i can't seem to get this script to work, though. i updated the version, of course. i'm working from sander's first response to this topic. i am successfully getting login details and i can modify the script to retrieve other data, but i can't get the tasks.
am i missing something? -
Inappropriate?This one works for me.
<?php // This will be where we test the api to Toggl
$target = "http://www.toggl.com";
$cookie_file = 'cookies.txt';
// Remove the current cookie file
unlink($cookie_file);
$send_data = array();
$send_data['start_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['end_date_ms'] = strtotime(date("m/d/Y 00:00:00")) * 1000;
$send_data['version'] = '1.13.2';
$send_data['tzoffset'] = '0';
$send_data['user_login'] = "123";
$send_data['user_password'] = "123";
$login = curl_init();
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($login, CURLOPT_URL, $target.'/user/login');
curl_setopt($login, CURLOPT_POSTFIELDS, json_encode($send_data));
curl_setopt($login, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($login, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($login, CURLOPT_COOKIEJAR, $cookie_file);
$login_details = json_decode(curl_exec($login));
curl_close($login); // This is moved up here (it was at the end)
$get_details = curl_init();
curl_setopt($get_details, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($get_details, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($get_details, CURLOPT_URL, $target.'/task/list');
curl_setopt($get_details, CURLOPT_POSTFIELDS, json_encode($send_data));
curl_setopt($get_details, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($get_details, CURLOPT_COOKIEJAR, $cookie_file);
$list_details = json_decode(curl_exec($get_details));
curl_close($get_details);
$currentTime = 0;
$theEntries = array();
foreach($list_details?>content as $tmp_detail)
{
if($tmp_detail->duration > 0)
{
// Break down the number into .25 increments
$theAmount = number_format(((($tmp_detail->duration / 1000) / 60) / 15)) * .25;
$theEntries[$tmp_detail->description] = $theAmount;
$currentTime = $currentTime + $theAmount;
}else{
// This is the end all so it must equal 4 or 8 depending on when we run this.
$offset = 4 - $currentTime;
$theEntries[$tmp_detail->description] = $offset;
}
}
echo "
";
print_r($theEntries);
echo "
";
?> -
Inappropriate?Theneonlobster, did you get it working?
-
Inappropriate?nope. i don't want all that detail, either. all i want to do is print the json array. login works just fine, json produces an empty set.
i noticed that sanders' most recent post had significant modifications from his original post in this topic, too.
is it possible that code is being stripped by the cms running this forum?
is it possible to have some demo code published on the api manual page?
-
Inappropriate?also, for some odd reason, when i multiply the date by less than 1000 (100, for example) i get a response, whereas multiplying by 1000 gets no response. i've gone as far to test that as using static strings rather than calculating
-
Inappropriate?What exactly are you sending to the server?
-
Inappropriate?presently, 1256533200 and 1256878800, oct. 26-30. i was hoping to catch SOME results. interestingly, i have absolutely no problem retrieving users, planned tasks, projects or anything else. just tasks
-
Inappropriate?in retrospect, maybe i didn't answer your question. i am sending all the send_data variables to the server, as the example code provided does. also, i should note that i've tried this on several servers.
-
Inappropriate?The numbers you gave seem to be seconds. You must send milliseconds: multiply your values by 1000.
In case the milliseconds do not work, please send the complete JSON to our support e-mail address and we shall look into it in more detail. -
Inappropriate?sorry, i failed to mention that with those strings i get a response. it's empty, but it's a response. if i multiply by 1000, i get nothing.
-
Inappropriate?it's kinda working now. i handed it off to my coworker who used his login credentials to get the array. so, now there's that question.. to my knowledge, the only difference between our accounts is that I have another workspace
-
Inappropriate?is there any explanation for why one user in a workspace can access tracked tasks via the api while other users can't? how can i retrieve my tracked tasks?
I’m wondering if i stumped the team
Loading Profile...



EMPLOYEE