Back

Step by Step Guide to Building a Planning Center API Integration in PHP

Aug 16, 20246 minute read

Introduction

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!

Prerequisites

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

  • A PHP environment (7.4+ recommended)
  • Composer installed (trust me, it'll make your life easier)
  • Planning Center API credentials (if you don't have these yet, hop over to their developer portal and grab 'em)

Got all that? Great! Let's roll up our sleeves and get to work.

Setting up the project

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

Authentication

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!

Making API requests

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);

Working with specific endpoints

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);

Error handling and rate limiting

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 }

Data processing and storage

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']]); }

Building a simple interface

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>

Testing and debugging

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 } }

Best practices and optimization

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 }

Conclusion

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! 🚀