views:

1035

answers:

4

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('[email protected]');

$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.

+6  A: 

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.

bumperbox
I saw this before asking but was scared off by the "Zend" in the name. Now you've said I don't need the whole cludging framework, I'll give it a proper look. Thanks.
Oli
A: 

How can I get the events from another calendar than the user default?

Hi there, this is not an answer to the above question, but a new question in itself. If you want people to find your question, please post this as a new question instead. Stackoverflow is not an "old fashioned forum" :-) See you around.
BerggreenDK
A: 

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 />';
}
Josh
+1  A: 

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: [email protected]) Use that with $query->setUser() to use that specific calendar.

Syntax Error