{"id":1967,"date":"2021-12-03T06:55:08","date_gmt":"2021-12-03T06:55:08","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-event-in-google-calendar-using-php\/"},"modified":"2025-06-09T09:08:44","modified_gmt":"2025-06-09T03:38:44","slug":"add-event-in-google-calendar-using-php","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-event-in-google-calendar-using-php\/","title":{"rendered":"How to Add Event in Google Calendar Using PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/calendar.google.com\/calendar\/u\/0\/r\" target=\"_blank\" rel=\"noreferrer noopener\">Google Calendar<\/a>&nbsp;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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, integrating Google calendar with your business improves efficiency and time management.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are an online store owner where customers might need to book an appointment, I have curated a solution to\u00a0add event in Google calendar using PHP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the below code you can add event to Google calendar and get notified for the same.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Add Event to Google Calendar:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nclass GoogleCalendarApi\n{\n\n    public function GetAccessTokenRefresh($client_id, $redirect_uri, $client_secret, $code)\n    {\n        $url = 'https:\/\/accounts.google.com\/o\/oauth2\/token';\n\n        $curlPost = 'client_id=' . $client_id . '&amp;redirect_uri=' . $redirect_uri . '&amp;client_secret=' . $client_secret . '&amp;code=' . $code . '&amp;grant_type=authorization_code';\n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, $url);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n        curl_setopt($ch, CURLOPT_POST, 1);\n        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\n        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);\n        $data = json_decode(curl_exec($ch), true);\n        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        if ($http_code != 200) {\n            throw new Exception('Error : Failed to receieve access token');\n        }\n        return $data;\n    }\n\n    public function GetUserCalendarTimezone($access_token)\n    {\n        $url_settings = 'https:\/\/www.googleapis.com\/calendar\/v3\/users\/me\/settings\/timezone';\n\n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, $url_settings);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));\n        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\n        $data = json_decode(curl_exec($ch), true);\n        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        if ($http_code != 200)\n            throw new Exception('Error : Failed to get timezone');\n\n        return $data['value'];\n    }\n\n\n    public function CreateCalendarEvent($calendar_id, $summary, $all_day, $event_time, $event_timezone, $access_token)\n    {\n        $url_events = 'https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/' . $calendar_id . '\/events';\n\n        $curlPost = array('summary' => $summary);\n        if ($all_day == 1) {\n            $curlPost['start'] = array('date' => $event_time['event_date']);\n            $curlPost['end'] = array('date' => $event_time['event_date']);\n        } else {\n            $curlPost['start'] = array('dateTime' => $event_time['start_time'], 'timeZone' => $event_timezone);\n            $curlPost['end'] = array('dateTime' => $event_time['end_time'], 'timeZone' => $event_timezone);\n        }\n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, $url_events);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n        curl_setopt($ch, CURLOPT_POST, 1);\n        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\n        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token, 'Content-Type: application\/json'));\n        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));\n        $data = json_decode(curl_exec($ch), true);\n        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        if ($http_code != 200)\n            throw new Exception('Error : Failed to create event');\n\n        return $data['id'];\n    }\n\n}\n\n$capi = new GoogleCalendarApi();\nconst APPLICATION_ID = 'YOUR APPLICATION ID';\nconst APPLICATION_REDIRECT_URL = 'http:\/\/127.0.0.1:8081\/magento236\/add_event.php';\nconst APPLICATION_SECRET = 'c88TmBa5qo8cPAMMheV0agr9';\n\nif(isset($_GET['code'])) {\n    $CODE = $_GET['code'];\n    $data = $capi->GetAccessTokenRefresh(APPLICATION_ID, APPLICATION_REDIRECT_URL, APPLICATION_SECRET, $CODE);\n    $access_token = $data['access_token'];\n\n    $user_timezone = $capi->GetUserCalendarTimezone($data['access_token']);\n    $calendar_id = 'primary';\n    $event_title = 'Event Title Meetanshi';\n\n\/\/ Event starting &amp; finishing at a specific time\n    $full_day_event = 0;\n    $event_time = ['start_time' => '2021-12-15T13:00:00', 'end_time' => '2021-12-15T13:15:00'];\n\n\/\/ Full day event\n    $full_day_event = 1;\n    $event_time = ['event_date' => '2021-12-15'];\n\n\/\/ Create event on primary calendar\n    $event_id = $capi->CreateCalendarEvent($calendar_id, $event_title, $full_day_event, $event_time, $user_timezone, $data['access_token']);\n\n    echo 'new event added';\n    echo '&lt;\/br>';\n    echo 'event Id:-'.$event_id;\n\n}else{\n\n    $url =$login_url = 'https:\/\/accounts.google.com\/o\/oauth2\/auth?scope=' . urlencode('https:\/\/www.googleapis.com\/auth\/calendar') . '&amp;redirect_uri=' . APPLICATION_REDIRECT_URL . '&amp;response_type=code&amp;client_id=' . APPLICATION_ID . '&amp;access_type=offline';\n\n    echo '&lt;a href=\"'.$url.'\">click here add event&lt;\/a>';\n}\n\n\nexit();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post via social media for helping the store owners optimize their time management!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn the solution to programmatically add event in Google calendar using PHP for better appointment management.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1967","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1967"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1967\/revisions"}],"predecessor-version":[{"id":16498,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1967\/revisions\/16498"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}