[HowTo] Google Calendar API : PHP – Retrieve events (Part 2)
In earlier post we saw how to install GData and retrieve a list of calendars. Continuing the same topic lets now see how to retrieve events from Google calendar using GData library. If you haven't done so, please read the part 1 of this series before continuing.
Firstly, lets instantiate the Zend_Gdata_Calendar class.
-
$path = '/home/abbas/ZendGdata/library';
-
-
require_once 'Zend/Loader.php';
-
Zend_Loader::loadClass('Zend_Gdata');
-
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
-
Zend_Loader::loadClass('Zend_Gdata_Calendar');
-
-
// User whose calendars you want to access
-
$user = 'username@gmail.com';
-
$pass = 'yourpass';
-
$serviceName = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
-
-
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $serviceName);
-
$service = new Zend_Gdata_Calendar($client);
For retrieving events, specially constructed URLs are used and Zend_Gdata_Calendar_EventQuery makes it easy to construct URL based on the parameters passed to it. Following three parameters are important.
- User: If not specified, the "default" (currently authenticated) user is used i.e. default calendar. Use email address for shared calendars or use the Calendar ID
- Visibility: Whether user's public or private calendars should be searched
- Projection: Specifies the amount of data that should be returned. Generally use "full".
Retrieve all events
Lets get a list of events from default calendar.
-
$query = $service->newEventQuery();
-
// Set different query parameters
-
$query->setUser('default');
-
$query->setVisibility('private');
-
$query->setProjection('full');
-
$query->setOrderby('starttime');
-
-
// Get the event list
-
try {
-
$eventFeed = $service->getCalendarEventFeed($query);
-
} catch (Zend_Gdata_App_Exception $e) {
-
}
-
-
echo "<ul>";
-
foreach ($eventFeed as $event) {
-
}
-
echo "</ul>";
Retrieve events occuring between a date range
Just add these two parameters to the query before getting the event feed
-
// Start date from where to get the events
-
$query->setStartMin('2010-02-01');
-
// End date
-
$query->setStartMax('2010-03-15');
Retrieving individual events
This can be done in two ways. If you know the event ID then set the event parameter in the query and use getCalendarEventEntry method instead of getCalendarEventFeed.
-
$query->setEvent('pomb7gnnum6sjiii6qgb76xyz');
-
// Get the individual event
-
try {
-
$event = $service->getCalendarEventEntry($query);
-
} catch (Zend_Gdata_App_Exception $e) {
-
}
or if you know the full event URL (you get this when you retrieve all events), you can skip creating the query object and directly pass the event URL to getCalendarEventEntry.
-
$eventURL = "http://www.google.com/calendar/feeds/default/private/full/pomb7gnnum6sjiii6qgb76xyz";
-
-
try {
-
$event = $service->getCalendarEventEntry($eventURL);
-
} catch (Zend_Gdata_App_Exception $e) {
-
}
Next we will see how to create, update and delete events.
About this entry
You’re currently reading “ [HowTo] Google Calendar API : PHP – Retrieve events (Part 2) ,” an entry on SANIsoft – PHP for E Biz
- Published:
- 6.7.10 / 8:56am
- Category:
- Google Calendar, HowTo, PHP, Zend Framework
- Author:
- Abbas Ali
No comments
Jump to comment form | comments rss | trackback uri