Back

Step by Step Guide to Building a Setmore Appointments API Integration in PHP

Aug 16, 20246 minute read

Hey there, fellow developer! Ready to dive into the world of appointment scheduling with Setmore? Let's roll up our sleeves and build an awesome PHP integration that'll have you managing appointments like a pro in no time.

Introduction

Setmore's API is a powerhouse for appointment scheduling, and we're about to harness that power in our PHP project. Whether you're looking to add scheduling to your existing app or build something entirely new, this guide's got you covered.

Prerequisites

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

  • A PHP environment ready to rock (PHP 7.4+ recommended)
  • A Setmore account with API credentials (if you don't have one, hop over to Setmore and sign up – it's a breeze!)

Setting Up the Project

First things first, let's get our project off the ground:

  1. Create a new directory for your project
  2. Fire up your terminal and run:
    composer init
    composer require guzzlehttp/guzzle
    

We're using Guzzle to handle our HTTP requests – it's like a Swiss Army knife for APIs!

Authentication

Alright, let's get that access token:

<?php require 'vendor/autoload.php'; $client = new GuzzleHttp\Client(); $response = $client->post('https://api.setmore.com/v1/o/oauth2/token', [ 'form_params' => [ 'grant_type' => 'refresh_token', 'refresh_token' => 'YOUR_REFRESH_TOKEN', 'client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_CLIENT_SECRET' ] ]); $token = json_decode($response->getBody(), true)['access_token'];

Pro tip: Store that token securely and implement a refresh mechanism. Your future self will thank you!

Basic API Requests

Now that we're authenticated, let's fetch some appointments:

$response = $client->get('https://api.setmore.com/v1/bookingapi/appointments', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token ] ]); $appointments = json_decode($response->getBody(), true);

Creating an appointment? Easy peasy:

$response = $client->post('https://api.setmore.com/v1/bookingapi/appointments', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token ], 'json' => [ 'staff_key' => 'STAFF_KEY', 'service_key' => 'SERVICE_KEY', 'customer_key' => 'CUSTOMER_KEY', 'start_time' => '2023-06-01T10:00:00Z' ] ]);

Handling API Responses

Always expect the unexpected:

try { $response = $client->get('https://api.setmore.com/v1/bookingapi/appointments'); $data = json_decode($response->getBody(), true); } catch (GuzzleHttp\Exception\RequestException $e) { echo "Oops! " . $e->getMessage(); }

Implementing Key Features

Here's a quick snippet to list available time slots:

$response = $client->get('https://api.setmore.com/v1/bookingapi/slots', [ 'query' => [ 'staff_key' => 'STAFF_KEY', 'service_key' => 'SERVICE_KEY', 'selected_date' => '2023-06-01' ], 'headers' => ['Authorization' => 'Bearer ' . $token] ]); $slots = json_decode($response->getBody(), true)['data']['slots'];

Webhooks

If you're feeling adventurous, set up a webhook endpoint:

$payload = file_get_contents('php://input'); $event = json_decode($payload, true); if ($event['event_type'] === 'appointment.created') { // Do something awesome! }

Best Practices

  • Respect rate limits – nobody likes a spammer!
  • Cache responses when possible to keep things speedy
  • Keep your API credentials secret – treat them like your Netflix password!

Testing

Don't forget to test your integration! PHPUnit is your friend here:

public function testFetchAppointments() { $client = $this->createMock(GuzzleHttp\Client::class); // Set up your mock and assertions }

Deployment Considerations

When you're ready to go live:

  • Use HTTPS everywhere – security first!
  • Implement proper error logging
  • Consider using a PHP framework for larger projects

Conclusion

And there you have it! You're now armed with the knowledge to create a killer Setmore API integration. Remember, the API docs are your best friend for diving deeper.

Now go forth and schedule like a boss! 🚀