Back

Step by Step Guide to Building a TeamUp API Integration in PHP

Aug 18, 20246 minute read

Introduction

Hey there, fellow code wranglers! Ready to dive into the world of TeamUp API integration? You're in for a treat. We're going to walk through building a robust PHP integration that'll have you syncing calendars and events like a pro. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment (7.4+ recommended)
  • A TeamUp account with an API key (if you don't have one, hop over to TeamUp and grab it)
  • cURL extension enabled (we'll be making HTTP requests)

Got all that? Great! Let's move on.

Setting up the project

First things first, let's set up our project structure:

teamup-integration/
├── src/
│   └── TeamUpAPI.php
├── tests/
│   └── TeamUpAPITest.php
├── composer.json
└── .gitignore

If you're using Composer (and you should be!), initialize your project:

composer init

Authentication

Alright, let's get that authentication sorted. We'll create a base class for our API interactions:

<?php class TeamUpAPI { private $apiKey; private $baseUrl = 'https://api.teamup.com'; public function __construct($apiKey) { $this->apiKey = $apiKey; } private function makeRequest($endpoint, $method = 'GET', $data = null) { // Implementation here } }

Core API Interactions

Now for the fun part - let's implement some core API methods:

public function getCalendars() { return $this->makeRequest('/calendars'); } public function getEvents($calendarKey, $params = []) { return $this->makeRequest("/calendars/{$calendarKey}/events", 'GET', $params); } public function createEvent($calendarKey, $eventData) { return $this->makeRequest("/calendars/{$calendarKey}/events", 'POST', $eventData); } // Add update and delete methods here

Error Handling

Don't forget to handle those pesky errors:

private function makeRequest($endpoint, $method = 'GET', $data = null) { // ... existing code ... $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { throw new Exception("cURL Error: " . $err); } $decodedResponse = json_decode($response, true); if (isset($decodedResponse['error'])) { throw new Exception("API Error: " . $decodedResponse['error']['message']); } return $decodedResponse; }

Data Processing

When you get that JSON back, you'll want to make it play nice with your app:

public function formatEvent($event) { return [ 'id' => $event['id'], 'title' => $event['title'], 'start_dt' => new DateTime($event['start_dt']), 'end_dt' => new DateTime($event['end_dt']), // Add more fields as needed ]; }

Optimization

Let's not hammer the API unnecessarily. A simple caching mechanism can go a long way:

private $cache = []; public function getEvents($calendarKey, $params = []) { $cacheKey = $calendarKey . serialize($params); if (isset($this->cache[$cacheKey])) { return $this->cache[$cacheKey]; } $events = $this->makeRequest("/calendars/{$calendarKey}/events", 'GET', $params); $this->cache[$cacheKey] = $events; return $events; }

Testing

Don't skip testing! Here's a quick PHPUnit test to get you started:

class TeamUpAPITest extends PHPUnit\Framework\TestCase { private $api; protected function setUp(): void { $this->api = new TeamUpAPI('your-api-key'); } public function testGetCalendars() { $calendars = $this->api->getCalendars(); $this->assertIsArray($calendars); // Add more assertions } }

Security Considerations

Remember, keep that API key safe! Consider using environment variables:

$apiKey = getenv('TEAMUP_API_KEY'); $api = new TeamUpAPI($apiKey);

Conclusion

And there you have it! You've just built a solid foundation for your TeamUp API integration. Remember, this is just the beginning - there's always room for improvement and expansion. Keep exploring the API docs, and don't be afraid to push the boundaries of what you can do with this integration.

Happy coding, and may your calendars always be in sync!

Sample Code Repository

Want to see it all put together? Check out the complete integration on GitHub: TeamUp PHP Integration

Now go forth and integrate with confidence!