Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use PHP to dynamically publish an ical file to be read by Google Calendar?

Any Google search on PHP ical just brings up phpicalendar and how to parse or read IN ical files. I just want to write a PHP file that pulls events from my database and writes them out in ical format.

My problem is I can't find anywhere that will answer two questions:

  1. What is the exact ical format, including headers, file format, footers, etc.? In other words, what does the file have to have, exactly, in order to be properly read in by Google Calendar, etc.?
  2. If I build this file using a .php extension, how do I publish it as ical? Do I have to write to a new .ics file? Or will Google Calendar etc. read a .php file as ical so long as the contents are in the correct format? (Much like a style.css.php file will be read as a CSS file if the contents are actually CSS, etc.)

Any help you all can give or point me to will be greatly appreciated!!!

like image 843
rhodesjason Avatar asked Sep 23 '09 01:09

rhodesjason


People also ask

How do I create an ICAL feed?

Go to Settings > Sharing > Create Link. Label the link accordingly (e.g., 'Selected Sub-calendars for iCalendar Feed' or whatever is appropriate.)


2 Answers

This should be very simple if Google Calendar does not require the *.ics-extension (which will require some URL rewriting in the server).

$ical = "BEGIN:VCALENDAR VERSION:2.0 PRODID:-//hacksw/handcal//NONSGML v1.0//EN BEGIN:VEVENT UID:" . md5(uniqid(mt_rand(), true)) . "@yourhost.test DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z DTSTART:19970714T170000Z DTEND:19970715T035959Z SUMMARY:Bastille Day Party END:VEVENT END:VCALENDAR";  //set correct content-type-header header('Content-type: text/calendar; charset=utf-8'); header('Content-Disposition: inline; filename=calendar.ics'); echo $ical; exit; 

That's essentially all you need to make a client think that you're serving a iCalendar file, even though there might be some issues regarding caching, text encoding and so on. But you can start experimenting with this simple code.

like image 176
Stefan Gehrig Avatar answered Oct 12 '22 11:10

Stefan Gehrig


A note of personal experience in addition to both Stefan Gehrig's answer and Dave None's answer (and mmmshuddup's reply):

I was having validation problems using both \n and PHP_EOL when I used the ICS validator at http://severinghaus.org/projects/icv/

I learned I had to use \r\n in order to get it to validate properly, so this was my solution:

function dateToCal($timestamp) {   return date('Ymd\Tgis\Z', $timestamp); }  function escapeString($string) {   return preg_replace('/([\,;])/','\\\$1', $string); }          $eol = "\r\n";     $load = "BEGIN:VCALENDAR" . $eol .     "VERSION:2.0" . $eol .     "PRODID:-//project/author//NONSGML v1.0//EN" . $eol .     "CALSCALE:GREGORIAN" . $eol .     "BEGIN:VEVENT" . $eol .     "DTEND:" . dateToCal($end) . $eol .     "UID:" . $id . $eol .     "DTSTAMP:" . dateToCal(time()) . $eol .     "DESCRIPTION:" . htmlspecialchars($title) . $eol .     "URL;VALUE=URI:" . htmlspecialchars($url) . $eol .     "SUMMARY:" . htmlspecialchars($description) . $eol .     "DTSTART:" . dateToCal($start) . $eol .     "END:VEVENT" . $eol .     "END:VCALENDAR";      $filename="Event-".$id;      // Set the headers     header('Content-type: text/calendar; charset=utf-8');     header('Content-Disposition: attachment; filename=' . $filename);      // Dump load     echo $load; 

That stopped my parse errors and made my ICS files validate properly.

like image 32
Kane Ford Avatar answered Oct 12 '22 09:10

Kane Ford