Exporting Single Google Calendar Events to ICS?

13,654

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';


?>
Share:
13,654
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Admin
    Admin almost 13 years
    Thanks for the tip. That does look simple. I also found that Google does seem to generate ICS files for single events; but I was just using the wrong URL structure (it doesn't seem to be documented anywhere). I think if you use a URL structure like: http://google.com/calendar/ical/[calendarID]/[visibility i.e. 'public']/[projection i.e. full]/[eventID].ics you get an ICS file for the event. The auto-generated ICS file includes information about the source calendar, so you have to remove the line that starts X-WR-CALNAME: if you want it to open as an appointment. More testing to do.
  • Timo
    Timo over 10 years
    @cgrymala I tried to get this working using my private calendar url. However, either I did not understand what is eventID (figured it is likely the UID that I got from the full calendar ics) or there's some other problem, resulting in 404 every time. Can you confirm that this works with private visibility and how to obtain the correct eventID?