如何使用 PHP 动态发布 ical 文件以供 Google 日历读取? [英] How can I use PHP to dynamically publish an ical file to be read by Google Calendar?

查看:15
本文介绍了如何使用 PHP 动态发布 ical 文件以供 Google 日历读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

  1. 确切格式是什么,包括页眉、文件格式、页脚等?换句话说,该文件究竟必须具备什么才能被 Google 日历等正确读取?
  2. 如果我使用 .php 扩展名构建此文件,如何将其发布为 ical?我必须写入新的 .ics 文件吗?或者只要内容格式正确,Google Calendar 等是否会读取 .php 文件?(很像 style.css.php 文件如果内容实际上是 CSS 等,则会被读取为 CSS 文件等)
  1. What is the exact ical 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.)

大家可以给予或指点我的任何帮助将不胜感激!!!

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

推荐答案

如果 Google Calendar 不需要 *.ics-extension(这将需要在服务器).

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

$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 文件所需的全部内容,即使在缓存、文本编码等方面可能存在一些问题.但是您可以开始尝试这个简单的代码.

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.

这篇关于如何使用 PHP 动态发布 ical 文件以供 Google 日历读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆