任何关于 PHP 文件的 Google 搜索只会显示 phpicalendar 以及如何解析或读取 INical 文件。我只是想写一个 PHP 文件,从我的数据库中提取事件,并写出它们的临床格式。
My problem is I can't find anywhere that will answer two questions:
Any help you all can give or point me to will be greatly appreciated!!!
编辑: 实际上我不确定-6186行给出了一个例子。Ics 命名格式,但是它也声明您可以使用 url 参数。我不认为这有什么关系,只要 MIME 类型是正确的。
编辑: 维基百科示例: 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.
如果 Google Calendar 不需要 *.ics扩展(这需要在服务器中重写一些 URL) ,那么这应该很简单。
*.ics
$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;
这基本上就是让客户认为您正在提供 iCalendar 文件所需的全部内容,尽管可能存在一些关于缓存、文本编码等等的问题。但是您可以开始尝试使用这个简单的代码。
Http://www.kanzaki.com/docs/ical/ 有一个稍微更易读的旧规范版本。它作为一个起点是有帮助的——许多事情仍然是相同的。
我的网站台也有
.ics
需要小心处理的 .ics区域:
请确保您像这样格式化字符串,否则它将无法工作
$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";
A note of personal experience in addition to both Stefan Gehrig's answer and Dave None's answer (and mmmshuddup's reply):
当我在 http://severinghaus.org/projects/icv/使用 ICS 验证器时,我在使用 n 和 PHP _ EOL 时遇到了验证问题
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.
也许有点晚了,但这里有一个链接到实际的规范
有一个优秀的 不易理解/不易理解包,允许您轻松地创建 IC 文件。
下面是来自 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();