Hey there, fellow developer! Ready to dive into the world of Zoho Bookings API? Great, because we're about to embark on a journey to create a slick PHP integration that'll have you managing bookings like a pro. This guide is all about getting you up and running with the Zoho Bookings API, so let's cut to the chase and get started!
Before we jump in, make sure you've got:
Got those? Awesome, let's move on!
First things first, let's get our project set up:
composer require guzzlehttp/guzzle
Alright, time to get that access token:
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client(); $response = $client->post('https://accounts.zoho.com/oauth/v2/token', [ 'form_params' => [ 'grant_type' => 'authorization_code', 'client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_CLIENT_SECRET', 'code' => 'YOUR_AUTH_CODE', 'redirect_uri' => 'YOUR_REDIRECT_URI' ] ]); $token = json_decode($response->getBody())->access_token;
Don't forget to implement a refresh mechanism – your future self will thank you!
Let's fetch some booking info:
$response = $client->get('https://bookings.zoho.com/api/v1/json/appointments', [ 'headers' => [ 'Authorization' => 'Zoho-oauthtoken ' . $token ] ]); $bookings = json_decode($response->getBody(), true);
Time to add a new booking:
$response = $client->post('https://bookings.zoho.com/api/v1/json/appointments', [ 'headers' => [ 'Authorization' => 'Zoho-oauthtoken ' . $token ], 'json' => [ 'service_id' => 'SERVICE_ID', 'staff_id' => 'STAFF_ID', 'datetime' => '2023-05-15T10:00:00+0000', 'customer' => [ 'name' => 'John Doe', 'email' => '[email protected]' ] ] ]);
Updating is a breeze:
$client->put('https://bookings.zoho.com/api/v1/json/appointments/BOOKING_ID', [ 'headers' => [ 'Authorization' => 'Zoho-oauthtoken ' . $token ], 'json' => [ 'datetime' => '2023-05-15T11:00:00+0000' ] ]);
And canceling? Even easier:
$client->delete('https://bookings.zoho.com/api/v1/json/appointments/BOOKING_ID', [ 'headers' => [ 'Authorization' => 'Zoho-oauthtoken ' . $token ] ]);
Always wrap your API calls in try-catch blocks:
try { // Your API call here } catch (\GuzzleHttp\Exception\RequestException $e) { error_log($e->getMessage()); // Handle the error gracefully }
If you're using webhooks, set up an endpoint to receive them:
$payload = file_get_contents('php://input'); $event = json_decode($payload, true); // Process the event switch ($event['type']) { case 'appointment.created': // Handle new appointment break; // Add more cases as needed }
Unit test your functions and don't be afraid to use var_dump()
when things get weird. We've all been there!
And there you have it! You're now armed with the knowledge to build a solid Zoho Bookings API integration. Remember, the API docs are your best friend, so keep them close. Now go forth and code something awesome!
Need more? Check out the Zoho Bookings API Documentation for the nitty-gritty details.
Happy coding, and may your bookings always be on time!