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

前端 未结 7 615
名媛妹妹
名媛妹妹 2020-11-28 17:18

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 writ

相关标签:
7条回答
  • 2020-11-28 17:55

    There is an excellent eluceo/ical package that allows you to easily create ics files.

    Here is an example usage from docs:

    // 1. Create new calendar
    $vCalendar = new \Eluceo\iCal\Component\Calendar('www.example.com');
    
    // 2. Create an event
    $vEvent = new \Eluceo\iCal\Component\Event();
    $vEvent->setDtStart(new \DateTime('2012-12-24'));
    $vEvent->setDtEnd(new \DateTime('2012-12-24'));
    $vEvent->setNoTime(true);
    $vEvent->setSummary('Christmas');
    
    // Adding Timezone (optional)
    $vEvent->setUseTimezone(true);
    
    // 3. Add event to calendar
    $vCalendar->addComponent($vEvent);
    
    // 4. Set headers
    header('Content-Type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename="cal.ics"');
    
    // 5. Output
    echo $vCalendar->render();
    
    0 讨论(0)
  • 2020-11-28 17:56

    Maybe a little late, but here's a link to the actual specification. http://tools.ietf.org/html/rfc55451

    0 讨论(0)
  • 2020-11-28 18:01

    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.

    0 讨论(0)
  • 2020-11-28 18:02
    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.

    0 讨论(0)
  • 2020-11-28 18:10

    http://www.kanzaki.com/docs/ical/ has a slightly more readable version of the older spec. It helps as a starting point - many things are still the same.

    Also on my site, I have

    1. Some lists of useful resources (see sidebar bottom right) on
      • ical Spec RFC 5545
      • ical Testing Resources
    2. Some notes recorded on my journey working with .ics over the last few years. In particular, you may find this repeating events 'cheatsheet' to be useful.

    .ics areas that need careful handling:

    • 'all day' events
    • types of dates (timezone, UTC, or local 'floating') - nb to understand distinction
    • interoperability of recurrence rules
    0 讨论(0)
  • 2020-11-28 18:16

    Make sure you format the string like this or it wont work

     $content = "BEGIN:VCALENDAR\n".
                "VERSION:2.0\n".
                "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n".
                "BEGIN:VEVENT\n".
                "UID:".uniqid()."\n".
                "DTSTAMP:".$time."\n".
                "DTSTART:".$time."\n".
                "DTEND:".$time."\n".
                "SUMMARY:".$summary."\n".
                "END:VEVENT\n".
                "END:VCALENDAR";
    
    0 讨论(0)
提交回复
热议问题