Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Google Calendar events in PHP

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.

like image 230
Oli Avatar asked May 12 '09 10:05

Oli


People also ask

How do I fetch events in Google Calendar?

To retrieve calendar IDs call the calendarList. list method. If you want to access the primary calendar of the currently logged in user, use the " primary " keyword. Event identifier.

Is Google Calendar API free?

All use of the Google Calendar API is available at no additional cost.

Does Google Calendar have API?

The Calendar API lets you integrate your app with Google Calendar, creating new ways for you to engage your users.


1 Answers

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.

like image 185
bumperbox Avatar answered Sep 28 '22 00:09

bumperbox