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

前端 未结 7 648
名媛妹妹
名媛妹妹 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 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.

提交回复
热议问题