This lightweight PHP integration enables you to seamlessly book and manage appointments with Google Calendar. It provides a simple API for scheduling and appointment management.
- Fetch available time slots from Google Calendar.
- Create new events on your Google Calendar.
- Streamline appointment management with minimal setup.
- PHP 7.4+
- Composer (Dependency Manager)
- Google Cloud Project with Google Calendar API enabled
- OAuth 2.0 credentials
Before using the API, you need to set up your Google Cloud Project:
-
Create a Google Cloud Project:
- Visit Google Cloud Console.
- Create or select an existing project.
-
Enable Google Calendar API:
- Navigate to APIs & Services > Library.
- Search for and enable Google Calendar API.
-
Set Up OAuth 2.0 Credentials:
- Go to APIs & Services > Credentials.
- Click Create Credentials > OAuth 2.0 Client ID.
- Configure redirect URIs and download the
credentials.jsonfile.
-
Calendar Configuration:
- Access your Google Calendar's Settings and Sharing.
- Share the calendar with the OAuth service account and grant Make changes to events permission.
-
Clone the Repository:
git clone https://github.com/johnbenet009/php-google-calendar.git cd php-google-calendar -
Install Dependencies:
composer install
-
Configure Credentials:
- Place the
credentials.jsonfile, created in the previous step, in the project directory.
- Place the
To perform the initial authentication and generate the token.json file for the Google Calendar API, follow these steps:
-
Ensure the credentials.json file is available:
- Ensure you have placed the
credentials.jsonfile from the Google Cloud Console in the project directory.
- Ensure you have placed the
-
Run the script in CLI mode:
- Run the
php calendar.phpscript using the PHP CLI (Command Line Interface). This is necessary because it prompts you to input an authorization code, which is not possible in a browser-based setup.
php calendar.php
- Run the
-
Follow the authentication flow:
- When the script runs, it will detect that no
token.jsonfile exists and generate an authentication URL. - Open the URL in a browser to log in to your Google account and authorize the application.
- When the script runs, it will detect that no
-
Get the authorization code:
- After authorizing, Google will provide an authorization code in the callback URL as
?code=****. Copy this code.
- After authorizing, Google will provide an authorization code in the callback URL as
-
Input the code:
- Paste the code into the terminal where the script is running. The script will exchange the code for an access token and refresh token, then save them in the
token.jsonfile.
- Paste the code into the terminal where the script is running. The script will exchange the code for an access token and refresh token, then save them in the
-
Verify:
- The script should now be able to make authenticated requests to the Google Calendar API.
Example of Running the Script:
php calendar.phpIf no token.json exists, you will see an output like this:
Auth URL:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=...
Enter verification code:
Copy the URL, paste it into your browser, and complete the authorization process. Copy the code provided by Google and paste it into the terminal. The script will create a token.json file in the same directory, which will be used for future authenticated requests.
The API provides the following endpoints:
-
Fetch Available Time Slots
GEThttp://your-domain.com/calendar.php?date=YYYY-MM-DD -
Create an Appointment
POSThttp://your-domain.com/calendar.php
Content-Type:application/json{ "date": "YYYY-MM-DD", "time": "HH:MM", "summary": "Appointment Title", "description": "Details of the appointment" }
Get Time Slots:
curl -X GET "http://your-domain.com/calendar.php?date=2025-01-08"Response:
{
"availableTimeSlots": ["09:00", "13:00", "14:00"],
"bookedTimeSlots": ["10:00", "11:00"]
}Book an Appointment:
curl -X POST "http://your-domain.com/calendar.php" -H "Content-Type: application/json" -d '{
"date": "2025-01-08",
"time": "14:00",
"summary": "Team Meeting",
"description": "Quarterly updates"
}'Response:
{
"success": true,
"eventLink": "https://www.google.com/calendar/event?eid=abc123"
}- Ensure proper calendar sharing and API configurations.
- Update time slots as needed in the source code.
This project is licensed under the MIT License.