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.
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.
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
composer init
composer require guzzlehttp/guzzle
We're using Guzzle to handle our HTTP requests – it's like a Swiss Army knife for APIs!
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!
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' ] ]);
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(); }
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'];
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! }
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 }
When you're ready to go live:
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! 🚀