如何将具有多个事件的ICS文件导入多个日历?

时间:2018-11-19 14:19:46

标签: php icalendar

生成ICS文件后,当我导入Outlook / Google时,发现Add by URL时仅导入了一个事件。即使使用Import选项,它也会发出一条消息1 out of 9 events imported

在检查了多个链接(iCalendar规范)之后,对我来说很明显这是非常稀疏的文档。另外,到目前为止,没有人能够回答这类问题。有“相似”的问题,尽管它们对我和其他许多人都不起作用!

CODE

PHP

function generateICS()
{
require_once(__DIR__."/libs/icalendar/zapcallib.php");
$iCalObj = new ZCiCal();
$ZDateHelper = new ZDateHelper();
$handle = fopen(__DIR__."\\generated\\eventlist.txt", "r");
if($handle){
    while(($line = fgets($handle)) !== false){
        $eventarr = json_decode($line, true);
        $eventObj = new ZCiCalNode("VEVENT", $iCalObj->curnode);
        // add title
        $eventObj->addNode(new ZCiCalDataNode("SUMMARY:" . $eventarr["EventName"]));
        if(isset($eventarr['EventDuration'])){
            // create timestamp
            $start = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventStart']));
            // add start date
            $eventObj->addNode(new ZCiCalDataNode("DTSTART:" . $ZDateHelper->fromUnixDateTimetoiCal($start)));
            // add duration
            $eventObj->addNode(new ZCiCalDataNode("DURATION:" . $eventarr['EventDuration']));
        }
        else{
            // create timestamp
            $start = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventStart']));
            $end = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventEnd']));

            // add start date
            $eventObj->addNode(new ZCiCalDataNode("DTSTART:" . $ZDateHelper->fromUnixDateTimetoiCal($start)));
            // add end date
            $eventObj->addNode(new ZCiCalDataNode("DTEND:" . $ZDateHelper->fromUnixDateTimetoiCal($end)));
        }
        // UID is a required item in VEVENT, create unique string for this event
        // Adding your domain to the end is a good way of creating uniqueness
        $uid = "event".time()."@company.org";
        $eventObj->addNode(new ZCiCalDataNode("UID:" . $uid));
        $eventObj->addNode(new ZCiCalDataNode("Description:" . ZCiCal::formatContent($eventarr['EventDesc'])));
        $eventObj->addNode(new ZCiCalDataNode("METHOD:" . ZCiCal::formatContent("PUBLISH")));
        $eventObj->addNode(new ZCiCalDataNode("X-WR-CALNAME:" . ZCiCal::formatContent("OurCalendar")));
        $eventObj->addNode(new ZCiCalDataNode("X-WR-TIMEZONE:" . ZCiCal::formatContent("(GMT-05:00) Eastern Time (US & Canada)")));

        $eventObj->addNode(new ZCiCalDataNode("SEQUENCE:" . ZCiCal::formatContent("0")));
        $eventObj->addNode(new ZCiCalDataNode("CLASS:" . ZCiCal::formatContent("PUBLIC")));
        $eventObj->addNode(new ZCiCalDataNode("DTSTAMP:".$ZDateHelper->fromUnixDateTimetoiCal(time())));
        $eventObj->addNode(new ZCiCalDataNode("LAST-MODIFIED:".$ZDateHelper->fromUnixDateTimetoiCal(time())));
    }
    fclose($handle);
}
else{
    echo "<div class='alert alert-danger'>Error reading events file!</div>";
}
$ics = $iCalObj->export();
echo $ics;
file_put_contents(__DIR__."\\generated\\company.ics", $ics);
}

Javascript

function genBtns() {
var _html = "";
var subIcal = "<a class='m-1 btn bg-lightdark text-dark' href='" + encodeURI("webcal://outwebsite.net/generated/ourcal.ics") + "'>Subscribe to iCalendar</a>";
var subGcal = "<a class='m-1 btn bg-lightdark text-dark' href='http://www.google.com/calendar/render?cid=" + encodeURI("webcal://ourwebsite.net/generated/ourcal.ics") + "'>Subscribe with Google Calendar</a>";
var subMcal = "<a class='m-1 btn bg-lightdark text-dark' href='http://calendar.live.com/calendar/calendar.asp?rru=addsubscription&url=" + encodeURI("https://ourwebsite.net/generated/ourcal.ics") + "&name=" + encodeURI("OurCalendar") + "'>Subscribe with Microsoft Calendar</a>";
_html = subIcal + "<br/>" + subGcal + "<br/>" + subMcal;
return _html;
}

其中,第一个按钮直接链接到文件本身,第二个按钮有效并链接到google,最后一个按钮不起作用,但链接到Outlook。

目标

目标是从我们的银行网站后端读取用户的日历,然后解析事件,并使其在多个流行日历中可用,以便用户可以无缝同步其付款日期和其他此类事件。

注意:这样的日历不是iCal格式,但是为了模拟这一点,我编写了php脚本,并假定事件列表以这种格式到达文本文件中。

{"EventName":"Car loan Payment","EventDesc":"","EventStart":"18-Nov-22 09:00","EventDuration":"PT24H"}
{"EventName":"Bernie's Anniversary ","EventDesc":"A small gathering to celebrate Bernie's 10 successful years at the company!","EventStart":"18-Nov-17 16:00","EventEnd":"18-Nov-17 18:00"}
{"EventName":"Snow day","EventDesc":"Snow all day!","EventStart":"18-Nov-15 05:00","EventDuration":"PT24H"}
.
.
.

生成的ICS(iCalendar)文件如下所示:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ZContent.net//ZapCalLib 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
SUMMARY:Car loan Payment
DTSTART:20181122T090000
DURATION:PT24H
UID:event1542634898@Company.org
Description:
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Bernie's Anniversary 
DTSTART:20181117T160000
DTEND:20181117T180000
UID:event1542634898@Company.org
Description:A small gathering to celebrate Bernie's 10 successful years at 
the company!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Snow day
DTSTART:20181115T050000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Snow all day!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Gary's Bday
DTSTART:20181127T170000
DTEND:20181127T190000
UID:event1542634898@Company.org
Description:A small celebration for Gary's Birthday 
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Avengers Movie night
DTSTART:20181115T180000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Watch avengers movie for free tonight!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Electric Bill Payment
DTSTART:20181118T070000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Please make a payment of $50 towards this month's bill!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Gas Bill Payment
DTSTART:20181118T080000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Please make a payment of $150 towards the current gas bill!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Snow day
DTSTART:20181128T060000
DURATION:PT24H
UID:event1542634898@Company.org
Description:A winter storm warning is in effect and DG will remain closed f
or the day
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Thanksgiving Lunch
DTSTART:20181123T120000
DTEND:20181123T140000
UID:event1542634898@Company.org
Description:Please gather for free food friday\, where you can enjoy turkey
  and salad!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
END:VCALENDAR

挑战

  1. 全天事件未正确解析。
  2. 导入其他日历无法正常工作。
  3. 文档太多,例子太少。
  4. 对此的图书馆支持非常有限。
  5. 通过单击打开所有事件后,Windows 10日历中的同一个ics文件可以正常工作(加载所有事件)。但是,时间仍然关闭,无论PT24H持续时间如何,全天事件都在第二天的第二天凌晨12点结束,如先前的多个帖子所述。
  6. 像Stanza.co这样的热门网站会创建自己的日历,然后允许用户从所有其他日历中进行订阅。这不是开源的。

我研究的是使用任何搜索栏时都可以找到的相同的东西。我围绕整个主题访问了多个网站上的至少100篇或更多篇文章。这是我第二次尝试得到答案。

1 个答案:

答案 0 :(得分:2)

有错误。将您的ics文件放入各种ics验证程序中。我会尽我所能,因为他们都不会说同样的话。 Google不会告诉您出了什么问题,而只会拒绝无效事件。使用一个验证器(此https://icalendar.org/validator.htm)处理您的ic内容:

  

问题!找到5个错误错误

UID value is not unique, duplicate found near line # 6Reference: RFC 5545 3.8.4.7. Unique Identifier
UID value is not unique, duplicate found near line # 20Reference: RFC 5545 3.8.4.7. Unique Identifier
Missing DTSTAMP property near line # 35Reference: RFC 5545 3.6.1. Event Component
Missing UID property near line # 35Reference: RFC 5545 3.6.1. Event Component
Missing DTSTART property in VEVENT near line # 35Reference: RFC 5545 3.6.1. Event Component