来自 php 的动态 .ics 未在 Google 日历中更新

发布于 2024-12-02 23:16:58 字数 2647 浏览 2 评论 0原文

我一直在尝试在 php 中创建一个 .ics 页面,该页面将通过“按 URL 添加”添加到 Google 日历,以便我从数据库中检索事件并将其呈现在日历中。

我在 SO 上搜索了一段时间,发现另一篇文章对入门非常有帮助,但我现在遇到了一个障碍,这似乎不会影响那些具有类似代码的人。可以找到帖子 此处

所以,我的问题是我似乎无法让 Google 日历更新我的页面,就好像它已在日历中缓存了该页面一样。因此,当我向页面添加另一个 VEVENT 时,它不会更新日历。

创建文件时,它会生成正确格式的 *.ics 文件。格式如下所示。

如果我直接进入该页面,我确实会收到下载文件的提示,该文件可以导入到 GC 中,如果我将相同的代码添加到不同的页面,例如从 cal.php 到 cal2.php,将使 GC 读取所有事件,而当我简单地添加与之前相同的 URL 时,它不会拾取对文件所做的任何更改。

有人知道为什么不更新吗?

其他遇到类似问题的人已经通过向他们的 VEVENT 添加 UID 解决了这个问题,但这并没有为我解决任何问题...

格式生成的

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110925T170000Z
DTEND:20110928T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110929T170000Z
DTEND:20110930T035959Z
SUMMARY:Camping Trip
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110914T170000Z
DTEND:20110915T035959Z
SUMMARY:Testing new Event
END:VEVENT
END:VCALENDAR

代码使用

<?php
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=\"calendar.ics\"");
echo "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:20110925T170000Z
DTEND:20110928T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@yourhost.test
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20110929T170000Z
DTEND:20110930T035959Z
SUMMARY:Camping Trip
END:VEVENT
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@yourhost.test
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20110914T170000Z
DTEND:20110915T035959Z
SUMMARY:Testing new Event
END:VEVENT
END:VCALENDAR";
?>

I have been trying to create a .ics page in php which is to be added to Google Calendar via "Add by URL" in order for me to retrieve events from a database and present them in the calendar.

I have searched around a while on SO and found another post which was very helpful in getting started, but I have now hit a snag which doesn't seem to affect those with similar code. Post can be found here.

So, my problem is that I cannot seem to get the Google Calendar to update my page, as if it has cached the page within the calendar. So when I add another VEVENT to the page it does not update the calendar.

When creating the file it generates the correct format for an *.ics file. Format shown below.

If I go directly to the page I do get prompted to download the file, which works to import into GC, and if I add the same code to a different page, for instance changing the pages name from cal.php to cal2.php, will make GC read all of the events whilst when I simply add the same URL as before it does not pick up on any changes made to the file.

Anyone have any idea to why this isn't updating?

Others who have had similar problems have solved this by adding a UID to their VEVENT, which did not solve this anything for me...

Format generated

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110925T170000Z
DTEND:20110928T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110929T170000Z
DTEND:20110930T035959Z
SUMMARY:Camping Trip
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20110901T092002Z
DTSTART:20110914T170000Z
DTEND:20110915T035959Z
SUMMARY:Testing new Event
END:VEVENT
END:VCALENDAR

Code used

<?php
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=\"calendar.ics\"");
echo "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:20110925T170000Z
DTEND:20110928T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@yourhost.test
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20110929T170000Z
DTEND:20110930T035959Z
SUMMARY:Camping Trip
END:VEVENT
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@yourhost.test
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20110914T170000Z
DTEND:20110915T035959Z
SUMMARY:Testing new Event
END:VEVENT
END:VCALENDAR";
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欲拥i 2024-12-09 23:16:58

csanyigabor 是正确的。此外,您还必须确保 UID 相同,以便应用程序能够匹配事件。
这里的问答都是关于取消的,但是原理是一样的
如何使用 ics 文件取消日历事件?

csanyigabor is correct. Also you must ensure that the UID's are the same so that the application will match the events.
This question and answer here deals with cancelations, but the principle is the same
How to cancel an calendar event using ics files?

Bonjour°[大白 2024-12-09 23:16:58

我认为您应该在 VEVENT 中使用 SEQUENCE 属性,并且每次更改后它都应该增加

I think you should use the SEQUENCE attribute in VEVENT and it should increases after every change.

对风讲故事 2024-12-09 23:16:58

@csanyigabor 是对的,@anmari 也是对的,但我还学到了一些其他技巧。当您构建 ICS 文件时,就像 anmari 所说,请确保每次同步 feed 时您的 UID 都是一致的。我让它根据用户提供的一些文本创建一个哈希(糟糕,糟糕的主意。5年前我构建它时为自己感到羞耻)。

如果你去这里-> https://www.rfc-editor.org/rfc/rfc5545(以及这里有一些更新点 -> https://www.rfc-editor.org/rfc/rfc7986 )它给出了一些有用的内容的概述。具体来说是 3.8.7.4。序列号,它概述了新序列的标准。

  Description: When a calendar component is created, its sequence
  number is 0.  It is monotonically incremented by the "Organizer's"
  CUA each time the "Organizer" makes a significant revision to the
  calendar component.

3.8.4.7 中有关 UID 的说明也很有帮助。唯一标识符

  Description:  The "UID" itself MUST be a globally unique identifier.
  The generator of the identifier MUST guarantee that the identifier
  is unique.  There are several algorithms that can be used to
  accomplish this.  A good method to assure uniqueness is to put the
  domain name or a domain literal IP address of the host on which
  the identifier was created on the right-hand side of an "@", and
  on the left-hand side, put a combination of the current calendar
  date and time of day (i.e., formatted in as a DATE-TIME value)
  along with some other currently unique (perhaps sequential)
  identifier available on the system (for example, a process id
  number).  Using a DATE-TIME value on the left-hand side and a
  domain name or domain literal on the right-hand side makes it
  possible to guarantee uniqueness since no two hosts should be
  using the same domain name or IP address at the same time.  Though
  other algorithms will work, it is RECOMMENDED that the right-hand
  side contain some domain identifier (either of the host itself or
  otherwise) such that the generator of the message identifier can
  guarantee the uniqueness of the left-hand side within the scope of
  that domain.

Example: The following is an example of this property:

 UID:[email protected]

@csanyigabor is right and so is @anmari, but a few other tips I picked up. When you are building your ICS file, like anmari said, make sure your UID's are consistent every time your feed is sync'd. I had made it create a hash based on some text provided by the user (bad, bad idea. Shame on myself 5 years ago when I built it).

If you go here -> https://www.rfc-editor.org/rfc/rfc5545 (and a few updated points here -> https://www.rfc-editor.org/rfc/rfc7986 ) it gives some great outlines on stuff that's helpful. Specifically 3.8.7.4. Sequence Number and it outlines what should be criteria for a new sequence.

  Description: When a calendar component is created, its sequence
  number is 0.  It is monotonically incremented by the "Organizer's"
  CUA each time the "Organizer" makes a significant revision to the
  calendar component.

Also helpful is what it says about UID in 3.8.4.7. Unique Identifier

  Description:  The "UID" itself MUST be a globally unique identifier.
  The generator of the identifier MUST guarantee that the identifier
  is unique.  There are several algorithms that can be used to
  accomplish this.  A good method to assure uniqueness is to put the
  domain name or a domain literal IP address of the host on which
  the identifier was created on the right-hand side of an "@", and
  on the left-hand side, put a combination of the current calendar
  date and time of day (i.e., formatted in as a DATE-TIME value)
  along with some other currently unique (perhaps sequential)
  identifier available on the system (for example, a process id
  number).  Using a DATE-TIME value on the left-hand side and a
  domain name or domain literal on the right-hand side makes it
  possible to guarantee uniqueness since no two hosts should be
  using the same domain name or IP address at the same time.  Though
  other algorithms will work, it is RECOMMENDED that the right-hand
  side contain some domain identifier (either of the host itself or
  otherwise) such that the generator of the message identifier can
  guarantee the uniqueness of the left-hand side within the scope of
  that domain.

Example: The following is an example of this property:

 UID:[email protected]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文