Hey there, fellow developer! Ready to dive into the world of Planning Center API integration? You're in for a treat. This guide will walk you through building a robust integration using PHP, allowing you to tap into the power of Planning Center's suite of tools. Let's get our hands dirty and create something awesome!
Before we jump in, make sure you've got these basics covered:
Got all that? Great! Let's roll up our sleeves and get to work.
First things first, let's set up our project:
mkdir planning-center-integration cd planning-center-integration composer init
Now, let's add the dependencies we'll need:
composer require guzzlehttp/guzzle league/oauth2-client
Planning Center uses OAuth 2.0, so let's implement that:
<?php use League\OAuth2\Client\Provider\GenericProvider; $provider = new GenericProvider([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'YOUR_REDIRECT_URI', 'urlAuthorize' => 'https://api.planningcenteronline.com/oauth/authorize', 'urlAccessToken' => 'https://api.planningcenteronline.com/oauth/token', 'urlResourceOwnerDetails' => 'https://api.planningcenteronline.com/people/v2/me' ]); // Get the auth URL and redirect the user $authorizationUrl = $provider->getAuthorizationUrl(); header('Location: ' . $authorizationUrl); exit;
Don't forget to handle the callback and store the access token securely!
Now that we're authenticated, let's make some requests:
$client = new GuzzleHttp\Client(); $response = $client->request('GET', 'https://api.planningcenteronline.com/services/v2/service_types', [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken, ], ]); $data = json_decode($response->getBody(), true);
Planning Center has several APIs. Here's a quick example with the People API:
$response = $client->request('GET', 'https://api.planningcenteronline.com/people/v2/people', [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken, ], ]); $people = json_decode($response->getBody(), true);
Always handle errors gracefully and respect rate limits:
try { $response = $client->request('GET', 'https://api.planningcenteronline.com/services/v2/service_types'); } catch (GuzzleHttp\Exception\ClientException $e) { $errorResponse = $e->getResponse(); $errorBody = json_decode($errorResponse->getBody(), true); // Handle the error appropriately }
Once you've got your data, you'll probably want to store it:
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password'); foreach ($people['data'] as $person) { $stmt = $pdo->prepare("INSERT INTO people (id, first_name, last_name) VALUES (?, ?, ?)"); $stmt->execute([$person['id'], $person['attributes']['first_name'], $person['attributes']['last_name']]); }
Let's create a basic PHP frontend to display our data:
<!DOCTYPE html> <html> <body> <h1>People in Planning Center</h1> <ul> <?php foreach ($people['data'] as $person): ?> <li><?= htmlspecialchars($person['attributes']['first_name'] . ' ' . $person['attributes']['last_name']) ?></li> <?php endforeach; ?> </ul> </body> </html>
Always test your integration thoroughly. Here's a simple PHPUnit test:
use PHPUnit\Framework\TestCase; class PlanningCenterIntegrationTest extends TestCase { public function testGetPeople() { // Your test code here } }
Remember to implement caching to reduce API calls and improve performance:
$cacheKey = 'people_list'; if ($cache->has($cacheKey)) { $people = $cache->get($cacheKey); } else { // Fetch from API $people = // ... fetch from API $cache->set($cacheKey, $people, 3600); // Cache for 1 hour }
And there you have it! You've just built a Planning Center API integration in PHP. Pretty cool, right? Remember, this is just the beginning. There's so much more you can do with this API. Keep exploring, keep building, and most importantly, keep having fun with it!
Happy coding, and may your integration be ever awesome! 🚀