Booked PHP API Client Library by [email protected] ( [email protected] )
This is a simple to use PHP client library to be used with Booked (formerly phpScheduleIt) RESTful API by [email protected] found at Booked Home
So here is the first beta of the library. Please report any bugs or suggestions to [email protected]
This project started as a way for me to design a customized Rental Booking System for a wonderful place called True Serenity. So because I have yet to make a contribution to the wonderful world of open source software, I decided to make this my first public attempt. I hope it is useful to someone.
- You must have PHP5.5 or higher
1st - You need to edit the bookedapiconfig.php file so that your booked web services URL points to where your Booked is hosted. Alos make sure to change the Time Zone const to macth yours. Then save the file:
const BOOKEDWEBSERVICESURL = ‘http://your-domain/booked/web/services/index.php’;
const YOURTIMEZONE = 'your time zone';
Please note this library will only work if you have Booked config'ed correctly, which is beyond the scope of this document. Please visit the Booked website / forums to learn how to do this. The best way to find out if your Booked api endpoint is working type this in your browser - http://your-domain/booked/web/services/index.php - if it's working you will get a page that documents the API. If you get a blank page or a server error then you'll need to fix that before this library can be used.
Next upload this library to your server either copy the 2 files and put them in your root or put them in a seperate folder just make sure to include the location of the library in your PHP script. Like this
Your script:
require_once(‘path to file/bookedapi.php’);
Next, make sure you use PHP’s startsession();
. The class library uses the $_SESSION global to store authentication information and will fail if not used. So before you call anything other than the _construct function, remember to call startsession();
first, like this…
Your script:
require_once(‘path to file/bookedapi.php’);
//some of your code
startsession();
$username = ‘your_booked_admin_username’;
$password = ‘your_booked_admin_password';
$bookedapiclient = new bookedapiclient($username, $password);
Then, in the above example, replace the variable $username
with your Booked user name and $password
with your Booked password. If you don't want to change the const BOOKEDAPIURL
in the configuration file and instead would like to set it at runtime then add the extra param like in the next example (changes to the above code in bold):
require_once(‘path to file/bookedapi.php’);
//some of your code
startsession();
$username = ‘your_booked_admin_username’;
$password = ‘your_booked_admin_password';
$bookedApiUrl = ‘http://your-domain/booked/web/services/index.php';
$bookedapiclient = new bookedapiclient($username, $password, $bookedApiUrl);
Next, before you make any API calls, you must call authenticate(true)
at least once. After that, the client automatically checks to see if you are still authenticated by checking to see if the session token has expired. This is to help the library preform faster by not having to re-authenticate every call. The library also automatically re-authenticates you if your session token has expired. For example:
require_once(‘path to file/bookedapi.php’);
//some of your code
startsession();
$username = ‘your_booked_admin_username’;
$password = ‘your_booked_admin_password;
$bookedapiclient = new bookedapiclient($username, $password);
$bookedapiclient-> authenticate(true);
Next you want to make your API call. We will get the current authenticated users reservations, like this:
require_once(‘path to file/bookedapi.php’);
//some of your code
startsession();
$username = ‘your_booked_admin_username’;
$password = ‘your_booked_admin_password;
$bookedapiclient = new bookedapiclient($username, $password);
$bookedapiclient-> authenticate(true);
//the next call will get all reservations for the current user or all of them if that user is the admin
$allReservations = $bookedapiclient->getReservation();
//print the result to the screen
print_r($allReservations);
If the call succeeds it will return a decoded json PHP associative array. If the call fails it will return a reason why unless it there was a server error like 500, then it will just return a Boolean false. In my next release, when I have some more free time, I was thinking about adding more useable error messages in the return. However for now this should work just fine.
For creating reservations, attributes and resources, there are some static methods in the class that can be used to help build the needed objects to then pass the related functions. I’m running out of time for now, but will be updating this readme with more examples.