14.6. Using Google Calendar

You can use the Zend_Gdata_Calendar class to view, create, update, and delete events in the online Google Calendar service.

See http://code.google.com/apis/calendar/overview.html for more information about the Google Calendar API.

14.6.1. Querying Google Calendar

You can specify the owner of the calendar you want to view using the setUser() method. The value is a string, typically the account of a user of Google Calendar. It can also be a specification of a group calendar.

<?php
$gdataCal = new Zend_Gdata_Calendar();

// get public calendar for Google Developers
$gdataCal->setUser('developer-calendar@google.com');
$feed = $gdataCal->getCalendarFeed();
?>

See the documentation for Zend_Feed for more information on how to use this object.

If you do not specify a user, the default is "default". If you are making the query using an authenticated Http client, this becomes the calendar associated with your Google account you used to authenticate.

<?php
$gdataCal = new Zend_Gdata_Calendar($authenticatedHttpClient);

// get current user's calendar
$myCalendarFeed = $gdataCal->getCalendarFeed();
?>

14.6.1.1. Event visibility

The setVisibility() method allows you to specify that the entries returned in the feed are those that are publically visible or are private calendar entries. Private entries are accessible only if you are using an authenticated Http client. The default value is "public".

<?php
$gdataCal = new Zend_Gdata_Calendar($authenticatedHttpClient);

// show my events that other people can see
$gdataCal->setVisiblity('public');
$feed = $gdataCal->getCalendarFeed();
?>

14.6.1.2. Event data projection

The setProjection() method allows you to specify how much information is returned in the feed. For example, "full" means to include all calendar event properties, but not comments that may be attached to the entry. The value "composite" is like "full", and the feed entries contain subfeed data for comments. See the Google Calendar API documentation for information on other projection values. The default value is "full".

<?php
$gdataCal = new Zend_Gdata_Calendar();

// get minimal information from johndoe's calendar
$gdataCal->setUser('johndoe@gmail.com');
$gdataCal->setProjection('free-busy');
$feed = $gdataCal->getCalendarFeed();
?>

14.6.1.3. Event dates

The setStartMin() and setStartMax() methods allow you to specify bounds on the calendar event date. If you specify a value for startMin, no events that begin earlier than the date you specify are included in the feed. Likewise no events that begin after the date specified by startMax are included.

<?php
$gdataCal = new Zend_Gdata_Calendar($authenticatedHttpClient);

// get only events after 12/1
$gdataCal->setStartMin('2006-12-01');
$feed = $gdataCal->getCalendarFeed();
?>

14.6.1.4. List calendars

Use the getCalendarListFeed() method to get a feed containing a list of the calendars associated with the currently authenticated user. You must be using an authenticated Http client to get this feed.

<?php
$gdataCal = new Zend_Gdata_Calendar($authenticatedHttpClient);
$myCalendars = $gdataCal->getCalendarListFeed();
?>

14.6.2. Posting to Google Calendar

You can post a new event to your calendar using the post() method. Construct the XML definition for the event entry, according to the guidelines in the Google Calendar API documentation. Then post the XML. You must be using an authenticated Http client to do this operation.

<?php
$gdataCal = new Zend_Gdata_Calendar($authenticatedHttpClient);

$xmlString = <<<XML
<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'>
  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'></category>
  <title type='text'>Tennis with Beth</title>
  <content type='text'>Meet for a quick lesson.</content>
  <author>
    <name>Jo March</name>
    <email>jo@gmail.com</email>
  </author>
  <gd:transparency
    value='http://schemas.google.com/g/2005#event.opaque'>
  </gd:transparency>
  <gd:eventStatus
    value='http://schemas.google.com/g/2005#event.confirmed'>
  </gd:eventStatus>
  <gd:where valueString='Rolling Lawn Courts'></gd:where>
  <gd:when startTime='2006-04-17T15:00:00.000Z'
    endTime='2006-04-17T17:00:00.000Z'></gd:when>
</entry>
XML;

$xml = new SimpleXMLElement($xmlString);

$gdataCal->post($xml->asXML());
?>