Hey there, fellow developer! Ready to supercharge your scheduling game? Let's dive into integrating the Acuity Scheduling API with PHP. This powerhouse combo will let you manage appointments like a pro, right from your own app. Buckle up!
Before we jump in, make sure you've got:
First things first, let's get our project structure in order:
Install Guzzle for handling HTTP requests:
composer require guzzlehttp/guzzle
Create a basic PHP file structure. Nothing fancy, just something like:
project/
├── src/
│ └── AcuityClient.php
└── composer.json
Alright, time to get cozy with Acuity:
AcuityClient.php
, let's set up basic authentication:use GuzzleHttp\Client; class AcuityClient { private $client; private $userId; private $apiKey; public function __construct($userId, $apiKey) { $this->userId = $userId; $this->apiKey = $apiKey; $this->client = new Client([ 'base_uri' => 'https://acuityscheduling.com/api/v1/', 'auth' => [$this->userId, $this->apiKey] ]); } }
Now we're cooking! Let's add some methods to make API calls:
public function get($endpoint) { $response = $this->client->get($endpoint); return json_decode($response->getBody(), true); } public function post($endpoint, $data) { $response = $this->client->post($endpoint, ['json' => $data]); return json_decode($response->getBody(), true); }
Let's implement some key features:
public function getAppointmentTypes() { return $this->get('appointment-types'); }
public function getAvailability($appointmentTypeId, $date) { return $this->get("availability/times?appointmentTypeID={$appointmentTypeId}&date={$date}"); }
public function bookAppointment($appointmentTypeId, $datetime, $firstName, $lastName, $email) { $data = [ 'appointmentTypeID' => $appointmentTypeId, 'datetime' => $datetime, 'firstName' => $firstName, 'lastName' => $lastName, 'email' => $email ]; return $this->post('appointments', $data); }
public function cancelAppointment($appointmentId) { return $this->put("appointments/{$appointmentId}/cancel"); }
Let's add some error handling to our methods:
private function handleResponse($response) { $data = json_decode($response->getBody(), true); if ($response->getStatusCode() >= 400) { throw new Exception($data['message'] ?? 'An error occurred'); } return $data; }
Now update our get
and post
methods to use this:
public function get($endpoint) { $response = $this->client->get($endpoint); return $this->handleResponse($response); } public function post($endpoint, $data) { $response = $this->client->post($endpoint, ['json' => $data]); return $this->handleResponse($response); }
If you're feeling adventurous, set up a webhook endpoint in your app:
public function handleWebhook($payload) { $event = $payload['action']; switch ($event) { case 'scheduled': // Handle new appointment break; case 'canceled': // Handle cancellation break; // Add more cases as needed } }
Time to put our code through its paces:
$client = new AcuityClient('your-user-id', 'your-api-key'); // Get appointment types $types = $client->getAppointmentTypes(); print_r($types); // Book an appointment $appointment = $client->bookAppointment(1, '2023-06-01 10:00:00', 'John', 'Doe', '[email protected]'); print_r($appointment);
And there you have it! You've just built a solid Acuity Scheduling API integration in PHP. Pretty cool, right? Remember, this is just the beginning. There's a whole world of possibilities with this API, so don't be afraid to explore and expand on what we've done here.
For more in-depth info, check out the Acuity Scheduling API docs. Now go forth and schedule like a boss! 🚀