Parsing Google Calendar - Starting Out

Pulling and parsing a Google Calendar feed sounds like a fairly straightforward project. After all, you are provided with both an XML and iCal Address (even for private calendars) for each calendar on their settings page. It didn't take me long to realize that there are unique pieces of these feeds to complicate an seemingly easily request.

I chose to move forward with the XML feed, as I've dealt with SimpleXML before and it's built within PHP (there are no built-in iCal processors). After grabbing the XML feed for the private calendar I plugged it into the constructor, making sure to wrap up my error levels on the way.

  1. $feed_url = GOOGLE_CALENDAR_FEED;

  2. libxml_use_internal_errors(TRUE);

  3. try {

  4. // params: url, options, and flag to show first param is a url

  5. $feed_xml = new SimpleXMLElement($feed_url, NULL, TRUE);

  6. } catch(Exception $e) {

  7. var_dump($e);

  8. exit;

  9. }

This sets up a nice SimpleXML object that you can loop through, with each event occupying a unique entry node. All you need to do is loop through each event and grab pieces of information... Oh, wait. The only real information in each node is a title and summary (and the author and published date, neither of which are terribly helpful). The location and date of the event are locked up in the html-formatted summary.

To get around this you need to modify the feed address that they give in the settings. It ends in /basic, change it to /full. This will give you the event information broken up into separate nodes, which is much easier to process. There is one big hang up, though. Some of the elements in the Google Calendar feed use different namespacing. Just accessing the new node will not be enough to grab all the pieces of data for the event - you will need to grab the namespaced children within the loop.

  1. foreach($feed_xml->entry as $event)

  2. {

  3. $gd_nodes = $event->children(''); // found in attribute of feed node

  4. $gcal_nodes = $event->children(''); // found in attribute of feed node

  5. $id = (string) $gcal_nodes->uid->attributes()->value;

  6. $title = (string) $event->title;

  7. $description = (string) $event->content;

  8. $location = (string) $gd_nodes->where->attributes()->valueString;

  9. $start_time = (string) $gd_nodes->when->attributes()->startTime;

  10. $end_time = (string) $gd_nodes->when->attributes()->endTime;

  11. }

Once you request the proper address, modifying the one that Google gives you, it is very easy to grab all the normal fields for an event, the 'where, what, and when' that people are usually interested in. This is all dandy - until you have repeating events. Then things get real nasty real quick. I'll cover how to deal with repeating events in the next post!