AuthSub in the Google Data Protocol Client Libraries
http://code.google.com/intl/fr/apis/gdata/docs/auth/authsub.html
Registration for Web-Based Applications
http://code.google.com/intl/fr/apis/accounts/docs/RegistrationForWebAppsAuto.html
Manage your domains
https://www.google.com/accounts/ManageDomains
Generate SSL key (optionnal)
http://code.google.com/intl/fr/apis/gdata/docs/auth/authsub.html#Registered
HowTo Google Calendar API : PHP (Part 1)
Google Calendar API request/response works in the form of Google Data API feeds. The popular PHP client library used to work with Calendar service is Zend's GData which is also distributed as a part of Zend Framework.
Install GData
First download the latest release GData library from here. Extract it and keep the folder anywhere you like. Lets assume that we kept it at /home/abbas/ZendGdata/.
Next step is to make sure you can include ZendGdata/library in your php script. This is done by setting include_path directive and this can be done in the following three ways:
* Permanently set the include_path directive in your php.ini configuration file from the command line
* Set the include_path path variable on a "per directory" level using .htaccess
* Use the set_include_path() function to dynamically set the include path in your scripts
We will use the set_include_path() function to set the include path. The code will look something like this...
$path = '/home/abbas/ZendGdata/library';
// Append the library path to existing paths
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
Authentication
The Gdata libarary can be used to access both public as well as private calendars. For private calendars we need to authenticate to the calendar servers. There are three types of authentication viz AuthSub proxy authentication, ClientLogin username/password authentication and Magic cookie authentication. We will be using ClientLogin username/password authentication in this tutorial as it is much easier to use and fits in the example we are going to take. Here's how to authenticate using username/password (Google calendar account credentials):
$path = '/home/abbas/ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
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';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
At this point authentication token is retrieved and $client is an object of Zend_Http_Client with appropriate Authentication header.
Retrieving list of Calendars
Now lets use the http client object to retrieve a list of all calanders.
(In continuation of above code..)
$service = new Zend_Gdata_Calendar($client);
// Get the calendar list feed
$listFeed = $service->getCalendarListFeed();
echo "<h1>Calendar List</h1>";
echo "<ul>";
foreach ($listFeed as $calendar) {
echo "<li>" . $calendar->title . " (" . $calendar->id . ")</li>";
}
echo "</ul>";
HowTo Google Calendar API : PHP – Retrieve events (Part 2)
Continuing the same topic lets now see how to retrieve events from Google calendar using GData library.
Firstly, lets instantiate the Zend_Gdata_Calendar class.
$path = '/home/abbas/ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
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 "Error: " . $e->getMessage();
}
echo "<ul>";
foreach ($eventFeed as $event) {
echo "<li>" . $event->title . " </li>";
}
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) {
echo "Error: " . $e->getMessage();
}
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) {
echo "Error: " . $e->getMessage();
}
HowTo Google Calendar API : PHP – Manage events (Part 3)
Now its time to manage events i.e. create, update and delete events.
First we need the Calendar service instance, so lets instantiate the Zend_GData_Calendar class.
$path = '/home/abbas/ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
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);
Creating Single Occurrence Events
Lets directly look at the code which has self explanatory comments
// Create a new event object using calendar service's factory method.
// We will then set different attributes of event in this object.
$event= $service->newEventEntry();
// Create a new title instance and set it in the event
$event->title = $service->newTitle("Some Event");
// Where attribute can have multiple values and hence passing an array of where objects
$event->where = array($service->newWhere("Nagpur, India"));
$event->content = $service->newContent("Some event content.");
// Create an object of When and set start and end datetime for the event
$when = $service->newWhen();
// Set start and end times in RFC3339 (http://www.ietf.org/rfc/rfc3339.txt)
$when->startTime = "2010-07-08T16:30:00.000+05:30"; // 8th July 2010, 4:30 pm (+5:30 GMT)
$when->endTime = "2010-07-08T17:30:00.000+05:30"; // 8th July 2010, 5:30 pm (+5:30 GMT)
// Set the when attribute for the event
$event->when = array($when);
// Create the event on google server
$newEvent = $service->insertEvent($event);
// URI of the new event which can be saved locally for later use
$eventUri = $newEvent->id->text;
If you want the event to last the whole day then only set the date in startTime and skip endTime attribute of "when"
$when->startTime = "2010-07-08"; // 8th July 2010, Whole day event
Creating recurring events
Recurring events are created almost similar to non recurring events. The only difference is that instead of "where" attribute we would set "recurrence" attribute for the event. Recurrence attribute is a string pattern in iCalendar standard RFC2445. For more details please check out Google Data API Guide.
// This event will occur all day every Friday starting from 9th July 2010 until 8th Dec 2015
$recurrence = "DTSTART;VALUE=DATE:20100709\r\n" .
"DTEND;VALUE=DATE:20100710\r\n" .
"RRULE:FREQ=WEEKLY;BYDAY=Fr;UNTIL=20151208\r\n";
$event->recurrence = $service->newRecurrence($recurrence);
Updating an event
For modifying an event, we first need to get its reference. This can be done using the event URI which we got after we created the event (see above). Once we get the event reference, we can pretty much change any attribute of it (as we did while creating an event) and then we have to save it.
// URI of the event which we got after creating it.
$eventUri = "http://www.google.com/calendar/feeds/default/private/full/53608ibmrtnb57o7hqf8l1tsu4";
// Get the event
$event = $service->getCalendarEventEntry($eventUri);
// Change the title
$event->title = $service->newTitle("New Title!");
// Save the event
$event->save();
Deleting an event
Deleting an event is very simple. Just get the event reference (as we did for updating) and call the event's delete method.
// Get the event
$event = $service->getCalendarEventEntry($eventUri);
// Delete the event
$event->delete();