Php – Exporting Single Google Calendar Events to ICS

google-calendar-apiicalendarPHPWordpress

I am currently looking into the possibility of displaying lists of events from multiple Google Calendars on a Web page. At the moment, I'm using the Google Calendar Events plugin for WordPress to display the events on the site (development site behind a firewall, so I can't share a link); but I'd like to see if there's any simple method to dynamically generate ICS files for each individual event.

We would like to allow the website visitors to add individual events to their calendars, but the Google Calendar Data API only seems to provide the option to download an entire calendar in ICS format. I tried simply appending a query string with the eid (event ID) to the end of the ICAL feed address, but that still opens the standard Google ICAL feed, which includes all of the events currently included on the calendar.

I am somewhat familiar with dynamically building ICS files from scratch, and have done it once in the past, pulling events from a local Perl calendar program and generating an ICS file for each event; but if there are any pre-built APIs or PHP classes that are already set up to build ICS files from individual Google Calendar Events, I'd much prefer to use those.

Best Answer

Its so simple its just a page really, try this: (you'll need to substitute you own variables of course ...)

<?php
    /*
    * generates calendar ics file
    */ 

if( 
    empty($_GET['summary']) || 
    empty($_GET['dtstart']) || 
    empty($_GET['dtend']) 
){
header ("Location: /diary/this-week") ;
exit();
} 

  header("Content-type: text/calendar");
  header("Content-Disposition: attachment; filename=".urlencode($_GET['summary']).'-'.time().".ics");
  header("Pragma: no-cache");
  header("Expires: 0");
  echo 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:councilsites
METHOD:PUBLISH
BEGIN:VEVENT
URL:'. htmlentities( $_GET['url'] ) .'
UID:'. htmlentities( $_GET['url'] ) .'
SUMMARY:'. htmlentities( $_GET['summary'] ) .'
DTSTAMP:'. date('Ymd\THis\Z') .'
DTSTART:'. htmlentities( $_GET['dtstart'] ) .'
DTEND:'. htmlentities( $_GET['dtend'] ) .'
LOCATION:'. htmlentities( $_GET['location'] ) .' 
END:VEVENT
END:VCALENDAR';


?>
Related Topic