views:

1817

answers:

4

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!!!

A: 
  1. Exact ical format: http://www.ietf.org/rfc/rfc2445.txt
  2. According to the spec, it has to end in .ics

Edit: actually I'm not sure - line 6186 gives an example in .ics naming format, but it also states you can use url parameters. I don't think it matters, so long as the MIME type is correct.

Edit: Example from wikipedia: http://en.wikipedia.org/wiki/ICalendar

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR

MIME type is configured on the server.

lod3n
I've tried to read that spec many times but I can't make heads or tails of it as far as what the ical file will look like. Can you at least point me to some lines where it begins to actually talk about what the .ics file should contain as far as header, where to put the MIME type, etc?
Jason Rhodes
Sure, see above.
lod3n
+4  A: 

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.

Stefan Gehrig
Thanks. I think those headers is what I was missing. I assume there are a few final steps in making this Google Calendar ready, as when I try to feed this file to Google Calendar via URL, it says "Importing calendar from url..." but hangs on that forever.Maybe that's a different question to post?
Jason Rhodes
Have you tried to access the script from your browser? Does it prompt you to download "caneldar.ics"? Can you import the file into iCal or Outlook for example?
Stefan Gehrig
Yes it works fine there, and Entourage loads it up fine as well. I just need the ability to create a file that Google Calendar (GC) will ping over and over to refresh, so that it stays up to date with a calendar of events in my database. Right now, GC won't accept it.
Jason Rhodes
Can you import this file into GC (using the import calendar option)?
Stefan Gehrig
That wouldn't help because it needs to be subscribed to the URL so that it updates regularly. HOWEVER, after a long wait, I refreshed and the calendar had been added to GC. Good news there. However, adding another VEVENT to the .php file and saving it to the server, it's still not appeared on GC. I've read that GC refreshes its subscriptions about once every 3 hours, so I'll check back in 3 hours and see if it's updated then.
Jason Rhodes
Nice to hear... One thing that obviously is required is a UID field within the VEVENT that contains a unique identifier.
Stefan Gehrig
Ok, didn't work. But I'm not sure what you mean by UID field. Just add UID:xxx into each VEVENT block where xxx=a unique id?
Jason Rhodes
Exactly. I updated the example above - and I also added a DTSTAMP property which will tell a client when the events has been updated.
Stefan Gehrig
Okay Gehrig, you're a genius. That worked. Thanks. (So far as I can tell Google Calendar is updating almost immediately, too.)
Jason Rhodes
A: 

@Jason : you can export an existing google calendar to iCal to have a good example

Do google refresh your iCal feeds ? Mine are not :/ Even when i suppress/recrete an iCal feed (from url), it doesnt update...

jujule
A: 

The example by Stefan Gehrig (Sep 23 '09 at 6:59) is very helpful. However, double quotation marks are missing around the file name: It should read

header('Content-Disposition: inline; filename="calendar.ics"');

otherwise there may occur problems (depending on the server configuration).

Lars10000