Question

I'm in the process of creating a blog for somebody. They want to pull in a lot of data and integrate it quite tightly into the design, so standard widgets are a no-no. That's been fine until now.

They have a public access Google Calendar with various events on it and I want to grab the next 5 events (from "now" onwards) and display the event title, when that instance of the event starts, its location and a link to the gcal item.

From what I can see, there are three options for grabbing gcal feeds: XML, ical or HTML (containing some really whack JSON). XML seems like the logical choice, right?

Well the XML feed is (after the atom feed description) actually just a lot of faffy HTML. Parsing this is possible but it's a huge pain in the behind because recurring events (of which there are several on the calendar) only show the first instance of that event and (apparently) no information on when the next instance is.

So am I just being a bit dense? Is there a way to show what I want just hacking through the XML API?

Or would I have better luck through iCal? I've never done any iCal with PHP so if you have, please suggest any libs you've used to make things simpler for yourself.

Edit: thanks to the answer, I downloaded the Zend Gdata pack (which, thankfully, is separate to the rest of the Zend Framework). Doing what I need was as easy as this:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
$service = new Zend_Gdata_Calendar();
$query = $service->newEventQuery();

$query->setUser('your_user@googlemail.com');

$query->setVisibility('public');
$query->setProjection('full');
$query->setStartMin(date('Y-n-j'));
$query->setStartMax(date('Y-n-j', time() + (60*60 *24*8)));
$query->setOrderby('starttime');

try { $eventFeed = $service->getCalendarEventFeed($query); }
catch (Zend_Gdata_App_Exception $e) { return; }

foreach ($eventFeed as $event) 
    echo $event; // do something real here

That should get you a week's worth of events (yes setStartMax is exclusive so setting it 8 days in the future is required).

Hope this helps somebody else in the future.

Was it helpful?

Solution

Another option is to use the Zend Google Calendar library, it is a separate part of the zend framework so you don't need the whole zend framework

http://framework.zend.com/manual/en/zend.gdata.calendar.html

it is not that hard once you have a look at the examples.

OTHER TIPS

In case it helps anyone else, I figured out how to get calendars other than the default. If you go into your google calendar and look up calendar settings, at the bottom there is a calendar ID that is formatted like a really long email address.(example: adg5jcq8gk7lsp6rrbmrm8c@group.calendar.google.com) Use that with $query->setUser() to use that specific calendar.

As a heads up to anyone using the code, echo $event; didn't display anything for me, so I thought the query was failing.

Here is a snippet that should print something:

foreach ($eventFeed as $event) {
  echo $event->title->text . '<br />';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top