Skip to content

Instantly share code, notes, and snippets.

@xorik
Last active August 14, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xorik/b2e6580f6d83b6a1d583b84a14b957aa to your computer and use it in GitHub Desktop.
Save xorik/b2e6580f6d83b6a1d583b84a14b957aa to your computer and use it in GitHub Desktop.
Toggl api PHP
  • composer install ajt/guzzle-toggl
  • Put your token into line 8
  • Run getProjects(), put workspace and project ID in lines 9 and 10
  • Run getTasks() or getCurrentTask()
<?php
namespace App\Model;
use AJT\Toggl\TogglClient;
class Toggl
{
const TOKEN = ''; // Put your toggl token here
const WORKSPACE = '1304347';
const PROJECT = '15458745';
protected $toggl_client;
public function __construct()
{
$this->toggl_client = TogglClient::factory(array('api_key' => self::TOKEN));
}
public function getProjects()
{
$workspaces = [];
foreach ($this->api("workspaces") as $w) {
// Projects
$projects = [];
foreach ($this->api("workspaces/{$w["id"]}/projects") as $p) {
$projects[$p["id"]] = $p["name"];
}
$workspaces[$w["id"]] = ["name" => $w["name"], "projects" => $projects];
}
var_dump($workspaces);
}
public function getTasks(\DateTime $startDate, \DateTime $endDate)
{
$page = 1;
$data = [];
while (1) {
$data = array_merge(
$data,
$this->getPage(self::WORKSPACE, self::PROJECT, $startDate, $endDate, $page, $perPage, $total)
);
if ($page * $perPage > $total) {
break;
}
$page++;
}
// Get current task
$now = new \DateTime();
if ($startDate < $now && $endDate > $now) {
if ($x = $this->getCurrentTask()) {
$data = array_merge([$x], $data);
}
}
return $data;
}
public function getCurrentTask()
{
if ($x = $this->api('time_entries/current')['data']) {
return [
'id' => $x['id'],
'start' => $x['start'],
'end' => 'now',
'dur' => ((new \DateTime())->getTimestamp() - (new \DateTime($x['start']))->getTimestamp()) * 1000,
'description' => $x['description'],
'current' => true,
];
}
return null;
}
protected function getPage(
$workspace,
$project,
\DateTime $startDate,
\DateTime $endDate,
$page,
&$perPage,
&$total
) {
$endDate = clone($endDate);
$start = $startDate->format('Y-m-d');
$end = $endDate->modify('+1 day')->format('Y-m-d');
$url = http_build_query(
[
"user_agent" => "xor29a@bk.ru",
"workspace_id" => $workspace,
"project_ids" => $project,
"since" => $start,
"until" => $end,
"page" => $page,
]
);
$data = $this->api("details?".$url, true);
$perPage = $data["per_page"];
$total = $data["total_count"];
return $data["data"];
}
protected function api($url, $reports = false)
{
$url = "https://toggl.com/".($reports ? "reports/api/v2/" : "api/v8/").$url;
$x = $this->toggl_client->get($url);
$x->send();
usleep(100000); // Baby, don't hurt me
return $x->getResponse()->json();
}
}
@JaneVanilla
Copy link

Добрый день!
Подскажите, у вас есть опыт сотрудничества с Twoj StartUp?
Если да, у меня вопрос, хочу с ними работать, но смущает такой момент что просят сразу плату за сотрудничество и не хотят никакой двухсторонний договор подписать. Так всегда происходит?

@xorik
Copy link
Author

xorik commented Aug 14, 2020

@ JaneVanilla
Добрый день. Я в итоге не воспользовался их услугами, т.к. переехал в другую страну по другой визе. Я советую написать им напрямую, они раньше очень подробно на всё отвечали

@JaneVanilla
Copy link

JaneVanilla commented Aug 14, 2020 via email

@xorik
Copy link
Author

xorik commented Aug 14, 2020

@JaneVanilla
Я бы не стал ничего отправлять без договора. Если не хотят, то есть другие инкубаторы, они не сильно друг от друга отличаются

@JaneVanilla
Copy link

JaneVanilla commented Aug 14, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment