如何使用 PHP 动态发布要由 Google 日历读取的 ical 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1463480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:42:33  来源:igfitidea点击:

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

phpcalendaricalendar

提问by rhodesjason

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 writes them out in ical format.

任何关于 PHP ical 的 Google 搜索都会显示 phpicalendar 以及如何解析或读取 IN ical 文件。我只想编写一个 PHP 文件,从我的数据库中提取事件并以 ical 格式将它们写出。

My problem is I can't find anywhere that will answer two questions:

我的问题是我找不到任何可以回答两个问题的地方:

  1. What is the exactical format, including headers, file format, footers, etc.? In other words, what does the file have to have, exactly, in order to be properly read in by Google Calendar, etc.?
  2. If I build this file using a .php extension, how do I publish it as ical? Do I have to write to a new .ics file? Or will Google Calendar etc. read a .php file as ical so long as the contents are in the correct format? (Much like a style.css.php file will be read as a CSS file if the contents are actually CSS, etc.)
  1. 确切的ical 格式是什么,包括页眉、文件格式、页脚等?换句话说,该文件必须具备什么才能被 Google 日历等正确读取?
  2. 如果我使用 .php 扩展名构建这个文件,我如何将它发布为 ical?我是否必须写入新的 .ics 文件?或者,只要内容格式正确,Google Calendar 等会像 ical 一样读取 .php 文件吗?(很像如果内容实际上是 CSS 等,则 style.css.php 文件将被读取为 CSS 文件。)

Any help you all can give or point me to will be greatly appreciated!!!

你们可以提供或指向我的任何帮助将不胜感激!!!

回答by Stefan Gehrig

This should be very simple if Google Calendar does not require the *.ics-extension (which will require some URL rewriting in the server).

如果 Google 日历不需要*.ics-extension(这将需要在服务器中重写一些 URL),这应该非常简单。

$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.

这基本上就是让客户认为您正在提供 iCalendar 文件所需的全部内容,即使可能存在一些有关缓存、文本编码等的问题。但是您可以开始试验这个简单的代码。

回答by Kane Ford

A note of personal experience in addition to both Stefan Gehrig's answer and Dave None's answer (and mmmshuddup's reply):

除了 Stefan Gehrig 的回答和 Dave None 的回答(以及 mmmshuddup 的回答)之外,还有一点个人经验:

I was having validation problems using both \n and PHP_EOL when I used the ICS validator at http://severinghaus.org/projects/icv/

当我在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:

我了解到我必须使用 \r\n 才能使其正确验证,所以这是我的解决方案:

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.

这停止了​​我的解析错误并使我的 ICS 文件正确验证。

回答by Ivan Yarych

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

有一个出色的eluceo/ical包,可以让您轻松创建 ics 文件。

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();

回答by Seirddriezel

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

也许有点晚了,但这里有一个指向实际规范的链接。http://tools.ietf.org/html/rfc5545 1

回答by anmari

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.

http://www.kanzaki.com/docs/ical/有一个比旧规范更易读的版本。它有助于作为起点 - 许多事情仍然相同。

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 notesrecorded on my journey working with .icsover the last few years. In particular, you may find this repeating events 'cheatsheet'to be useful.
  1. 一些有用的资源列表(见侧栏右下角)
    • 物理规范 RFC 5545
    • 测试资源
  2. 在我.ics过去几年的工作过程中记录了一些笔记。特别是,您可能会发现这个重复事件“备忘单”很有用。

.icsareas that need careful handling:

.ics需要小心处理的地方:

  • 'all day' events
  • types of dates (timezone, UTC, or local 'floating') - nb to understand distinction
  • interoperability of recurrence rules
  • “全天”活动
  • 日期类型(时区、UTC 或本地“浮动”)-nb 以了解区别
  • 重复规则的互操作性

回答by Dave None

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";

回答by lod3n

  1. Exact ical format: http://www.ietf.org/rfc/rfc2445.txt
  2. According to the spec, it has to end in .ics
  1. 确切的格式:http: //www.ietf.org/rfc/rfc2445.txt
  2. 根据规范,它必须以 .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.

编辑:实际上我不确定 - 第 6186 行给出了一个 .ics 命名格式的例子,但它也说明你可以使用 url 参数。我认为这并不重要,只要 MIME 类型正确即可。

Edit: Example from wikipedia: http://en.wikipedia.org/wiki/ICalendar

编辑:来自维基百科的示例: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.

MIME 类型在服务器上配置。