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

前端 未结 7 616
名媛妹妹
名媛妹妹 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:17

    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.

    0 讨论(0)
提交回复
热议问题