Hey there, fellow developer! Ready to supercharge your time tracking game? Let's dive into building a Clockify API integration using PHP. We'll be leveraging the awesome jdecool/clockify-api
package to make our lives easier. Buckle up!
Before we jump in, make sure you've got:
First things first, let's get that package installed:
composer require jdecool/clockify-api
Easy peasy, right?
Now, let's initialize our Clockify client:
use JDecool\Clockify\Client; $apiKey = 'your-api-key-here'; $client = new Client($apiKey);
Let's start with something simple:
$workspaces = $client->workspaces()->workspaces(); foreach ($workspaces as $workspace) { echo $workspace->name . "\n"; }
Now, let's grab some projects:
$workspaceId = 'your-workspace-id'; $projects = $client->projects()->projects($workspaceId); foreach ($projects as $project) { echo $project->name . "\n"; }
Time to fetch those time entries:
$userId = 'your-user-id'; $timeEntries = $client->timeEntries()->findAllTimeEntries($workspaceId, $userId); foreach ($timeEntries as $entry) { echo $entry->description . ": " . $entry->timeInterval->duration . "\n"; }
Let's add a new time entry:
$entry = $client->timeEntries()->createTimeEntry($workspaceId, [ 'start' => '2023-06-01T09:00:00Z', 'end' => '2023-06-01T17:00:00Z', 'description' => 'Coding like a boss', 'projectId' => 'your-project-id' ]);
Need to make a change? No problem:
$client->timeEntries()->updateTimeEntry($workspaceId, $entry->id, [ 'description' => 'Coding like a SUPER boss' ]);
Oops, made a mistake? Let's clean it up:
$client->timeEntries()->deleteTimeEntry($workspaceId, $entry->id);
Always be prepared for the unexpected:
try { // Your Clockify API calls here } catch (\JDecool\Clockify\Exception\ClockifyException $e) { echo "Oops! Something went wrong: " . $e->getMessage(); }
Let's put it all together:
$workspaceId = 'your-workspace-id'; $userId = 'your-user-id'; $projects = $client->projects()->projects($workspaceId); $timeEntries = $client->timeEntries()->findAllTimeEntries($workspaceId, $userId); echo "<h1>My Time Tracking Dashboard</h1>"; echo "<h2>Projects:</h2>"; echo "<ul>"; foreach ($projects as $project) { echo "<li>{$project->name}</li>"; } echo "</ul>"; echo "<h2>Recent Time Entries:</h2>"; echo "<ul>"; foreach ($timeEntries as $entry) { echo "<li>{$entry->description}: {$entry->timeInterval->duration}</li>"; } echo "</ul>";
And there you have it! You're now equipped to build some seriously cool time tracking integrations with Clockify. Remember, this is just scratching the surface - there's so much more you can do with the API. So go forth and code, you time-tracking wizard!
Need more info? Check out the Clockify API docs and the jdecool/clockify-api GitHub repo. Happy coding!