Google Calendar API with PHP

Installation

  • Download the latest release
  • copy library/Zend to your web folder

Connection

require_once 'Zend/Loader.php';
 
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
 
$user = 'username@gmail.com';
$pass = 'password';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
 
try
{
	$client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);			
}
catch(Exception $e)
{
	// prevent Google username and password from being displayed
	// if a problem occurs
	echo "Could not connect to calendar.";
	die();
}

Update $user and $pass.

List of events

Get events

// parameters
$calendar_user = '0470gkp8g7ftv8ov5nfvjplrdk%40group.calendar.google.com';
$calendar_visibility = 'private-50642f5681afbb525ed30d2b94b79b4b';
 
$start_date = '2010-06-01';
$end_date = '2010-06-30';
 
// build query
$gdataCal = new Zend_Gdata_Calendar($client);
 
$query = $gdataCal->newEventQuery();
 
$query->setUser($calendar_user);
$query->setVisibility($calendar_visibility);			
 
$query->setSingleEvents(true);
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setSortOrder('ascending');
$query->setMaxResults(100);
 
 
$query->setStartMin($start_date);
$query->setStartMax($end_date);
 
// execute and get results
$event_list = $gdataCal->getCalendarEventFeed($query);
  • $calendar_user and $calendar_visibility: calendar dropdown -> Calendar settings -> Private Address -> “XML” link:
http://www.google.com/calendar/feeds/$calendar_user/$calendar_visibility/basic
$start_date = $start_date  . 'T00:00:00.000-07:00';
$end_date = $end_date  . 'T00:00:00.000-07:00';

Display events

foreach ($event_list as $event)
{
	// id
	print $event->id . '<br />';
 
	// title
	print $event->title . '<br />';
 
	// where
	print $event->where[0] . '<br />';
 
	// description
	print $event->content . '<br />';
 
	// when (ex: 2010-06-11T07:30:00.000-07:00)
	print $event->when[0]->startTime . '<br />';
	print $event->when[0]->endTime . '<br />';
 
	print '-----<br />';
}

Notes

  • $query->setSingleEvents(true): if set to false (default), when a single instance of a repeating event is modified, the original instance AND the modified version will be returned
  • convert date to SQL format
// from 	2010-06-11T07:30:00.000-07:00
// to 		2010-06-11 07:30:00
function date_google_to_sql($str)
{
	$t = explode('T', $str);
 
	$date = $t[0];
	$time = $t[1];
	$time = substr($time, 0, 8);
 
	$str = $date . ' ' . $time;
	return $str;
}

Create event

// parameters
$title = "French revolution celebration";
$where = "at my place";
$description = "only mineral water";
 
$start_date = "2010-07-14 07:00:00";
$end_date = "2010-07-14 07:00:00";
 
$calendar_user = 'h9s3qp5al1m5a9440n6ah8c7qs%40group.calendar.google.com';
$tzOffset = '-07'; // timezone offset
 
// build event
$start_date = str_replace(' ', 'T', $start_date);
$end_date = str_replace(' ', 'T', $end_date);
 
$gdataCal = new Zend_Gdata_Calendar($client);
$newEvent = $gdataCal->newEventEntry();
 
$newEvent->title = $gdataCal->newTitle($title);
$newEvent->where = array($gdataCal->newWhere($where));
$newEvent->content = $gdataCal->newContent($description);
 
$when = $gdataCal->newWhen();
$when->startTime = "{$start_date}.000{$tzOffset}:00";
$when->endTime = "{$end_date}.000{$tzOffset}:00";
$newEvent->when = array($when);
 
// insert event
$createdEvent = $gdataCal->insertEvent($newEvent, "http://www.google.com/calendar/feeds/$calendar_user/private/full");
 
// event id
$event_id = $createdEvent->id->text;
  • $calendar_user: same as above

Get event from id

// parameters
$event_id = 'http://www.google.com/calendar/feeds/h9s3qp5al1m5a9440n6ah8c7qs%40group.calendar.google.com/private-3056301e77adcd8d353a6ea7bc23a4d8/full/nk8jd3jnftoe46lm0sr2da23pk';
 
// get event
$gdataCal = new Zend_Gdata_Calendar($client);
$event = $gdataCal->getCalendarEventEntry($event_id);

Reference

Feedback

Thank you for this page...I've been searching for the way to delete an event and this was the answer:

// parameters
$event_id = 'http://www.google.com/calendar/feeds/h9s3qp5al1m5a9440n6ah8c7qs@group.calendar.google.com/private-3056301e77adcd8d353a6ea7bc23a4d8/full/nk8jd3jnftoe46lm0sr2da23pk';

// get event
$gdataCal = new Zend_Gdata_Calendar($client);
$event = $gdataCal->getCalendarEventEntry($event_id);
Peter
Apr 25, 2011
#1
// delete them
$event->delete();

Apr 25, 2011
#2
how was i not able to find this a week ago?? this is exactly what i needed/wanted and i had to figure it all out from scratch, and here! you have it--exactly it, and you're even in the same time zone offset as i am!

(shakes fist at the heavens)
Mel A.
Jun 10, 2011
#3
Thanks for this, it is very useful. Is there any way to bring in events from multiple calendars?
Mike
Jul 7, 2011
#4
Thanks for this.. is there a way to retrieve the event edited date?
Marc
Oct 3, 2011
#5
edited = $event->updated->text
Marc
Oct 6, 2011
#6
hey can anyone help new to zend and gdata, when i run the first code at the start (ClientLogin) on Easy_PHP server i get an error saying 'could not connect to calendar'. Which i know is apart of the try/catch loop. can anybody help me to just connect to my calendar??
mooooo
Nov 17, 2011
#7
it's because your email and passwords are wrong
freebird
Mar 22, 2012
#8
Thank you, this is great and easy to implement. One question (probably a generic PHP question): Why does the /Zend folder have to reside next to my document? Like when I use index.php in / I have to place the Zend folder in / too, not /lib/ because that throws errors from require_once in Zend/Loader.php. How do I configure this to allow it to go somewhere else?
Thanks again!
foreign_cars
Mar 22, 2012
#9
@foreign_cars--You need to set your path to Zend. You can do this on the fly in your PHP document. Then you can put the Zend framework wherever you like.

$path = '/path/to/where/Zend/lives/on/your/server';

set_include_path(get_include_path() . PATH_SEPARATOR . $path);
Dean
Apr 9, 2012
#10
Thanks, it's very useful for me!
Martin (Czech republic)
Apr 16, 2012
#11
HOw to implement this with cakePhp framework. Please assist.
Vikas
Dec 10, 2012
#12
How to get a specific event id immediately after insert?
Wampirer
Mar 21, 2013
#13
I am getting you calendar events, may be $calendar_user and $calendar_visibility is yours, how can i get mine $calendar_user and $calendar_visibility

YOUR
======================
$calendar_user = '0470gkp8g7ftv8ov5nfvjplrdk@group.calendar.google.com';
$calendar_visibility = 'private-50642f5681afbb525ed30d2b94b79b4b';
adnan
Jun 21, 2013
#14
Thank you

Delete event helped me
Joe
Sep 21, 2013
#15
I am getting Could not connect to calendar error.

E-mail and password are 100% correct.

Can someone help me?
Tony
Oct 21, 2013
#16
what is correct method to add events dynamically..
its not working under
while($row = mysql_fetch_array($sql)){
$title = "French revolution celebration";
$where = "at my place";
$description = "only mineral water";

$start_date = "2010-07-14 07:00:00";
$end_date = "2010-07-14 07:00:00";

$calendar_user = 'h9s3qp5al1m5a9440n6ah8c7qs@group.calendar.google.com';
$tzOffset = '-07'; // timezone offset

// build event
$start_date = str_replace(' ', 'T', $start_date);
$end_date = str_replace(' ', 'T', $end_date);

$gdataCal = new Zend_Gdata_Calendar($client);
$newEvent = $gdataCal->newEventEntry();

$newEvent->title = $gdataCal->newTitle($title);
$newEvent->where = array($gdataCal->newWhere($where));
$newEvent->content = $gdataCal->newContent($description);

$when = $gdataCal->newWhen();
$when->startTime = "{$start_date}.000{$tzOffset}:00";
$when->endTime = "{$end_date}.000{$tzOffset}:00";
$newEvent->when = array($when);

// insert event
$createdEvent = $gdataCal->insertEvent($newEvent, "http://www.google.com/calendar/feeds/$calendar_user/private/full");

// event id
$event_id = $createdEvent->id->text;
}
John Doe
Nov 12, 2013
#17
Thank you! Peter's top 2 comments on how to delete a calendar event by the Google calendar API was extremely useful and solved my problem! Perhaps add it into the main article if you have time! Merci beaucoup.
Alastair
Nov 17, 2013
#18
am i able to insert more than one events at a same time

sky_stars
Jan 23, 2014
#19
what if i want to add recurring event?can someone help me with this?i need this really fast..insert recurring event to other's calendar..
junaidi92
Mar 14, 2014
#20