How to Add Event in Google Calendar Using PHP
Google Calendar has eliminated the use of a diary and pen to maintain a schedule. Also, events created in digital calendars make it possible to never miss an appointment!
Hence, integrating Google calendar with your business improves efficiency and time management.
If you are an online store owner where customers might need to book an appointment, I have curated a solution to add event in Google calendar using PHP.
With the below code you can add event to Google calendar and get notified for the same.
If your business is something like an online boutique appointment or treatment appointment, or simply you ask your customers to schedule an appointment before visiting, and you want to get notified of such events, use the below solution.
Method to Programmatically Add Event to Google Calendar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php class GoogleCalendarApi { public function GetAccessTokenRefresh($client_id, $redirect_uri, $client_secret, $code) { $url = 'https://accounts.google.com/o/oauth2/token'; $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code=' . $code . '&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = json_decode(curl_exec($ch), true); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code != 200) { throw new Exception('Error : Failed to receieve access token'); } return $data; } public function GetUserCalendarTimezone($access_token) { $url_settings = 'https://www.googleapis.com/calendar/v3/users/me/settings/timezone'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url_settings); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $data = json_decode(curl_exec($ch), true); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code != 200) throw new Exception('Error : Failed to get timezone'); return $data['value']; } public function CreateCalendarEvent($calendar_id, $summary, $all_day, $event_time, $event_timezone, $access_token) { $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events'; $curlPost = array('summary' => $summary); if ($all_day == 1) { $curlPost['start'] = array('date' => $event_time['event_date']); $curlPost['end'] = array('date' => $event_time['event_date']); } else { $curlPost['start'] = array('dateTime' => $event_time['start_time'], 'timeZone' => $event_timezone); $curlPost['end'] = array('dateTime' => $event_time['end_time'], 'timeZone' => $event_timezone); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url_events); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token, 'Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost)); $data = json_decode(curl_exec($ch), true); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code != 200) throw new Exception('Error : Failed to create event'); return $data['id']; } } $capi = new GoogleCalendarApi(); const APPLICATION_ID = 'YOUR APPLICATION ID'; const APPLICATION_REDIRECT_URL = 'http://127.0.0.1:8081/magento236/add_event.php'; const APPLICATION_SECRET = 'c88TmBa5qo8cPAMMheV0agr9'; if(isset($_GET['code'])) { $CODE = $_GET['code']; $data = $capi->GetAccessTokenRefresh(APPLICATION_ID, APPLICATION_REDIRECT_URL, APPLICATION_SECRET, $CODE); $access_token = $data['access_token']; $user_timezone = $capi->GetUserCalendarTimezone($data['access_token']); $calendar_id = 'primary'; $event_title = 'Event Title Meetanshi'; // Event starting & finishing at a specific time $full_day_event = 0; $event_time = ['start_time' => '2021-12-15T13:00:00', 'end_time' => '2021-12-15T13:15:00']; // Full day event $full_day_event = 1; $event_time = ['event_date' => '2021-12-15']; // Create event on primary calendar $event_id = $capi->CreateCalendarEvent($calendar_id, $event_title, $full_day_event, $event_time, $user_timezone, $data['access_token']); echo 'new event added'; echo '</br>'; echo 'event Id:-'.$event_id; }else{ $url =$login_url = 'https://accounts.google.com/o/oauth2/auth?scope=' . urlencode('https://www.googleapis.com/auth/calendar') . '&redirect_uri=' . APPLICATION_REDIRECT_URL . '&response_type=code&client_id=' . APPLICATION_ID . '&access_type=offline'; echo '<a href="'.$url.'">click here add event</a>'; } exit(); |
Any doubts? If so, do mention them in the Comments section below. I’d be happy to help.
Also, do share the post via social media for helping the store owners optimize their time management!
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hello, how to attach file when creating an event..
Best Regards
Hello Cesar,
You may refer to the below contents:
https://support.google.com/calendar/answer/6192039?hl=en&co=GENIE.Platform%3DDesktop
https://support.google.com/calendar/answer/6192039?hl=en&co=GENIE.Platform%3DAndroid
Thank You
great idea, i would like to test it, but i’m not a developer.
Where does this code go?
Does it work with magento 2.4.2?
Hello,
The below link will help you to create Google API: https://developers.google.com/workspace/guides/create-project
You need to fetch the below details and replace it in the above code’s variables.
APPLICATION_ID , APPLICATION_REDIRECT_URL, APPLICATION_SECRET
Thank You