How to Update and Delete an Event with Google Calendar API using PHP

This tutorial is an extension of How to Add an Event with Google Calendar API using PHP in which creating events with Google Calendar API was discussed. It also demonstrated how to create a Google application and perform user authorization.

This tutorial deals with updating and deleting Google Calendar events.

Demo

Go to demo page

Codes for the demo can be downloaded towards the end of the tutorial

Updating Events with Google Calendar API

Once you have created an event with API, updating the event is fairly simple. You need to send the same data (or specific fields) to update the event, only difference being you also need to pass the ID of the event which you want to update.

$capi = new GoogleCalendarApi();
$access_token = 'GOOGLE-ACCESS-TOKEN';

// updating event on the primary calendar
$calendar_id = 'primary';

// Event on primary calendar
$event_id = 'CALENDAR-EVENT-ID';

$event_title = 'Event Title';

// Event starting & finishing at a specific time
$full_day_event = 0; 
$event_time = [ 'start_time' => '2016-12-31T15:00:00', 'end_time' => '2016-12-31T16:00:00' ];

// Full day event
$full_day_event = 1; 
$event_time = [ 'event_date' => '2016-12-31' ];

$capi->UpdateCalendarEvent($event_id, $calendar_id, $event_title, $full_day_event, $event_time, $user_timezone, $access_token);

API call to update an event

class GoogleCalendarApi
{
	......

	public function UpdateCalendarEvent($event_id, $calendar_id, $summary, $all_day, $event_time, $event_timezone, $access_token) {
		$url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events/' . $event_id;

		$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_CUSTOMREQUEST, 'PUT');		
		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 update event');
	}

	......
}

There are many properties in a Google event. You can update most of them. See the whole list of event properties in Calendar Event Resource.

Deleting Events with Google Calenadar API

Once you get the ID of the event, deleting the event is just making a DELETE HTTP request to the event URL.

$capi = new GoogleCalendarApi();
$access_token = 'GOOGLE-ACCESS-TOKEN';

// deleting event on the primary calendar
$calendar_id = 'primary';

// Event on primary calendar
$event_id = 'CALENDAR-EVENT-ID';

$capi->DeleteCalendarEvent($event_id, $calendar_id, $access_token);

API call to delete an event

class GoogleCalendarApi
{
	......

	public function DeleteCalendarEvent($event_id, $calendar_id, $access_token) {
		$url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events/' . $event_id;

		$ch = curl_init();		
		curl_setopt($ch, CURLOPT_URL, $url_events);		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));		
		$data = json_decode(curl_exec($ch), true);
		$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
		if($http_code != 204) 
			throw new Exception('Error : Failed to delete event');
	}

	......
}

Download Codes

Download