Search code examples
phpicalendar

What's wrong with this iCal calendar?


I've written a script to generate an iCal calendar from events in my database. Here's a sample output:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
ORGANIZER;CN=Blodgruppen
DTSTART:20130128T173000Z
DTEND: 20130128T193000Z
SUMMARY: Su-möte
END:VEVENT
BEGIN:VEVENT
ORGANIZER;CN=F-styret
DTSTART:20130128T173000Z
DTEND: 20130128T193000Z
SUMMARY: Su-möte
END:VEVENT
BEGIN:VEVENT
ORGANIZER;CN=Blodgruppen
DTSTART:20130412T100000Z
DTEND: 20130414T180000Z
SUMMARY: KTH
END:VEVENT
BEGIN:VEVENT
ORGANIZER;CN=F-styret
DTSTART:20130412T100000Z
DTEND: 20130414T180000Z
SUMMARY: KTH
END:VEVENT
END:VCALENDAR

When I try to subscribe to this in iCal it says that the file is invalid. Does anyone know why?

Also, does anyone know if I will run into problem somewhere because the ending of my file is .php and not .ics?

(In case it helps, I constructed the above by looking at the example on Wikipedia: http://en.wikipedia.org/wiki/ICalendar#Core_object )


Solution

  • Going back to the basics a bit you cannot upload your PHP script, some calendar clients allow you to subscribe to a URL to pick up an iCal feed, make sure this is what you are doing and that you are not trying to upload the PHP script file.

    If that is OK, then we can look at the definition. According to the documentation the ORGANIZER parameter is optional, if it is included it has to have a colon : and a cal-address which must be a URI. Your ORGANIZER fields look like this:

    ORGANIZER;CN=Blodgruppen
    

    They should look like this:

    ORGANIZER;CN=Blodgruppen:URI
    

    Where URI doesn't necessarily have to be a mailto, here are some examples. For convenience an email address can be added like this...

    ORGANIZER;CN=Blodgruppen:MAILTO:[email protected]
    

    Or a phone number...

    ORGANIZER;CN=Blodgruppen:TEL:+49-01234-56789
    

    You can include a web of ftp URLs as well as some other less used things. Alternatively the parameter is not required, so you can simply remove it.

    Also it appears that the VEVENT is missing a UID and a DTSTAMP which are required properties.

    UID can be an identifier of your choosing but it should be globally unique. I'll take the moment to explain what is meant by globally unique by describing the implications of the UID parameter. The calendar client will use this to identify the event, if you use an id of an event that already exists in the calendar it will update that event thinking you mean for it to do that, therefore it must not match the id of an existing event. If you think of adding events to lots of different calendar clients that may also contain events, this is why it must by "globally" unique. There is a uniqid() function you can use in PHP which might help create your own globally unique identifiers.

    DTSTAMP is the time the event was created or last updated.

    The white-space in your definition is inconsistent, the documentation does not discuss white-space between parameters and values but all examples are shown without whitespace so you might also want to strip any non-essential whitespace...

    DTEND: 20130128T193000Z
    

    To become...

    DTEND:20130128T193000Z
    

    It might not hurt but there is the possibility. I would also start off with trying one event with the bare minimum of parameters just to check it is the iCal definition causing the issue.

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    BEGIN:VEVENT
    UID:20121229T212000Z-test-1
    DTSTAMP:20121229T212000Z
    DTSTART:20130128T173000Z
    DTEND:20130128T193000Z
    END:VEVENT
    END:VCALENDAR
    

    You might want to check your PHP script is using the correct mime-type as well text/calendar. If it isn't you can add the Content-Type header to the response using the header function:

     header("Content-Type: text/calendar");
    

    I hope this helps...