2015-10-19 75 views
10

我已经创建日历文件(.ics)输出从我的应用程序的日历应用程序,如谷歌日历中使用的目的。我通过验证器运行了下面的文件,但似乎Google和雅虎日历都不想玩球。我的本地Mac日历客户端工作100%。任何人都可以看到任何明显的关于下面的文件,可能会窒息Google日历?或者他们需要的其他领域?我也从雅虎日历中获得了500个错误。在这一点上撕掉头发。谷歌日历/雅虎日历不工作的iCal格式

我试过了,没有时区信息,它似乎没有太大的区别。

头输出

 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); 
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
     header('Cache-Control: no-store, no-cache, must-revalidate'); 
     header('Cache-Control: post-check=0, pre-check=0', false); 
     header('Pragma: no-cache'); 
     header('Content-type: text/calendar;'); 
     header('Content-Disposition: inline; filename="calendar.ics"'); 
     header("Content-Description: File Transfer"); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: " . strlen($generated)); 

UTC时间或TZID我已经尝试了,似乎不有所作为。如果任何人都有一个非常简单的.ics事件URL,它似乎可以工作,“十字日历”很想看到它。或者,如果有人可以为此提供一些体面的图书馆,那么将其融入CakePHP项目并不是一件痛苦的事,那就足够了。

BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//MS//NONSGML//EN 
X-WR-CALNAME:Holidays 
CALSCALE:GREGORIAN 
METHOD:PUBLISH 
BEGIN:VEVENT 
UID:1075 
DTSTART;TZID=Europe/London:20150114T080000 
DTEND;TZID=Europe/London:20150114T170000 
DTSTAMP:20150114T080000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Stat Holiday (1.0 days) 
LAST-MODIFIED:20150114T080000Z 
LOCATION: 
SUMMARY: Statutory Day 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
BEGIN:VEVENT 
UID:1076 
DTSTART;TZID=Europe/London:20150116T080000 
DTEND;TZID=Europe/London:20150116T170000 
DTSTAMP:20150116T080000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Stat Holiday (1.0 days) 
LAST-MODIFIED:20150116T080000Z 
LOCATION: 
SUMMARY: Statutory Day 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
BEGIN:VEVENT 
UID:1079 
DTSTART;TZID=Europe/London:20151020T123000 
DTEND;TZID=Europe/London:20151020T170000 
DTSTAMP:20151020T123000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Holiday Request (3.0 days) test 
LAST-MODIFIED:20151020T123000Z 
LOCATION: 
SUMMARY:admin Leave 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
BEGIN:VEVENT 
UID:1080 
DTSTART;TZID=Europe/London:20151021T080000 
DTEND;TZID=Europe/London:20151021T123000 
DTSTAMP:20151021T080000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Holiday (3.0 days) test 
LAST-MODIFIED:20151021T080000Z 
LOCATION: 
SUMMARY:admin Leave 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
BEGIN:VEVENT 
UID:1081 
DTSTART;TZID=Europe/London:20151022T080000 
DTEND;TZID=Europe/London:20151022T170000 
DTSTAMP:20151022T080000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Holiday Request (3.0 days) test 
LAST-MODIFIED:20151022T080000Z 
LOCATION: 
SUMMARY:admin Leave 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
BEGIN:VEVENT 
UID:1082 
DTSTART;TZID=Europe/London:20151023T080000 
DTEND;TZID=Europe/London:20151023T170000 
DTSTAMP:20151023T080000Z 
CREATED:20151019T211433Z 
DESCRIPTION:Holiday Request (3.0 days) test 
LAST-MODIFIED:20151023T080000Z 
LOCATION: 
SUMMARY:admin Leave 
SEQUENCE:0 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
END:VCALENDAR 
+1

您是如何输出从PHP ICS的文件,通过回声? 我可以将日历直接导入Google日历,以便文件正常。 –

+0

我不知道_how_你试图加载这个,但是只使用HTTP标头和你提供的.ics文件,我可以[通过使用link_]添加(https://support.google.com/日历/答案/ 37100?hl = en)没有问题。 – HPierce

回答

4

在SO(Google Calendar errors on importing .ics file)使用的答案组合和https://gist.github.com/jakebellacera/635416

我工作的代码在几个工具测试了一些源代码是:

<?php 

//test 
// 
$summary = "test"; 
$datestart=time(); 
$dateend = $datestart + 3600; 
$address = "Amsterdam"; 
$description = "testing ical format"; 
$filename = "test.ics"; 

// Variables used in this script: 
// $summary  - text title of the event 
// $datestart - the starting date (in seconds since unix epoch) 
// $dateend  - the ending date (in seconds since unix epoch) 
// $address  - the event's address 
// $uri   - the URL of the event (add http://) 
// $description - text description of the event 
// $filename - the name of this file for saving (e.g. my-event-name.ics) 
// 
// Notes: 
// - the UID should be unique to the event, so in this case I'm just using 
// uniqid to create a uid, but you could do whatever you'd like. 
// 
// - iCal requires a date format of "yyyymmddThhiissZ". The "T" and "Z" 
// characters are not placeholders, just plain ol' characters. The "T" 
// character acts as a delimeter between the date (yyyymmdd) and the time 
// (hhiiss), and the "Z" states that the date is in UTC time. Note that if 
// you don't want to use UTC time, you must prepend your date-time values 
// with a TZID property. See RFC 5545 section 3.3.5 
// 
// - The Content-Disposition: attachment; header tells the browser to save/open 
// the file. The filename param sets the name of the file, so you could set 
// it as "my-event-name.ics" or something similar. 
// 
// - Read up on RFC 5545, the iCalendar specification. There is a lot of helpful 
// info in there, such as formatting rules. There are also many more options 
// to set, including alarms, invitees, busy status, etc. 
// 
//  https://www.ietf.org/rfc/rfc5545.txt 
// 1. Set the correct headers for this file 
header('Content-type: text/calendar; charset=utf-8'); 
header("Content-Disposition:inline;filename=".$filename); 
// 2. Define helper functions 
// Converts a unix timestamp to an ics-friendly format 
// NOTE: "Z" means that this timestamp is a UTC timestamp. If you need 
// to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART 
// with TZID properties (see RFC 5545 section 3.3.5 for info) 
// 
// Also note that we are using "H" instead of "g" because iCalendar's Time format 
// requires 24-hour time (see RFC 5545 section 3.3.12 for info). 
function dateToCal($timestamp) { 
    return date('Ymd\THis\Z', $timestamp); 
} 
// Escapes a string of characters 
function escapeString($string) { 
    return preg_replace('/([\,;])/','\\\$1', $string); 
} 
// 3. Echo out the ics file's contents 
$iCal="BEGIN:VCALENDAR\r 
VERSION:2.0\r 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r 
CALSCALE:GREGORIAN\r 
BEGIN:VEVENT\r 
DTSTAMP:".dateToCal(time())."\r 
DTSTART:".dateToCal($datestart)."\r 
DTEND:".dateToCal($dateend)."\r 
UID:".uniqid()."\r 
LOCATION:".escapeString($address)."\r 
DESCRIPTION:".escapeString($description)."\r 
SUMMARY:".escapeString($summary)."\r 
END:VEVENT\r 
END:VCALENDAR"; 
file_put_contents($filename,$iCal); 
print file_get_contents($filename); 
unlink($filename); 
exit; 
?> 

看来重要的是,行结束是正确的(所以\ r \ n)。

+0

这适用于Mac/Google日历。雅虎日历还在继续。 – Paul

4

你的文件是正确的,我已经上传至Google日历自己,所以它应该工作,但是如果你没有在文件回声后停止脚本,例如:

//somewhere else in the script 
    echo "this will break the file"; 
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); 
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
    header('Cache-Control: no-store, no-cache, must-revalidate'); 
    header('Cache-Control: post-check=0, pre-check=0', false); 
    header('Pragma: no-cache'); 
    header('Content-type: text/calendar;'); 
    header('Content-Disposition: inline; filename="calendar.ics"'); 
    header("Content-Description: File Transfer"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: " . strlen($generated)); 
    echo $generated; 
    exit;//or return 200 or whatever 
    echo "this could break the file except there is an exit;";//for example. 

你可以本质上是发送格式不正确的文件。

+0

好点,':: foobar ::'或';; foobar ;;'..或者可能是同一个文件呢? –

+1

有趣 - 无论什么原因,无论我添加什么:'echo“foobar”;''或'echo“:: foobar ::”;'甚至不会出现在提供的输出中。 _ **但** _从'$ generated'移除东西实际上将它从输出中移除。我不知道该怎么想。 (我没有使用你输出'$ generated'后的退出的例子) – HPierce

+1

看到我的编辑,我测试过,它打破了它。 –

0

.ics文件我加了一行

METHOD:REQUEST 

和雅虎开始工作。 在我的情况下,我发送邮件到hotmail,Gmail和雅虎与.ics文件和雅虎没有行不工作。 随着

METHOD:PUBLISH 

雅虎还不到风度工作