从数据库创建的动态ical不起作用

时间:2009-09-29 21:07:28

标签: php mysql calendar icalendar

我在PHP 5和MySQL中构建了一个网站,其中包含一个跟踪预定照片拍摄的表格。我想将这些预定“事件”的提要推送到一个文件中。

我原来是asked this question并得到了S. Gehrig的好答案。每当我在Dreamweaver中手动调整文件时,我都会有一个示例文件工作,并定期在Google日历中更新。但是,现在我已经从数据库中添加了动态PHP,它将无法正常工作。

这是PHP:

<?php
require_once('../../_includes/initialize.php');

$ical = " BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN ";

$slots = Slot::find_all();
foreach($slots as $slot) {
    $job = Job::find_by_id($slot->job_id);

    $start_stamp = strtotime($slot->start);
    $end_stamp = strtotime($slot->endtime);
    $dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time
    $dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time

    $summary = $job->title;

    $ical .= " BEGIN:VEVENT
    UID:" . $slot->id . "@homewoodphoto.jhu.edu
    DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
    DTSTART:" . $dtstart . "
    DTEND:" . $dtend . "
    SUMMARY:" . $summary . "
    END:VEVENT ";
}

$ical .= " END:VCALENDAR";

//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=homewoodphoto_master.ics');
echo $ical;
exit;

?>

据我所知,此文件的输出与我工作的手动硬编码版本完全相同。任何人都可以看到为什么这不起作用????

PS以下是正在运行的文件的代码 - 我刚刚将其发布到我的服务器上,并通过Google日历中的URL订阅。当我在第二次活动中硬编码时,它很快就出现在谷歌日历中。

<?php

$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:20090925T170000Z
DTEND:20090928T035959Z
SUMMARY:Bastille Day Party
END:VEVENT

BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)); . "@yourhost.test
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:20090929T170000Z
DTEND:20090930T035959Z
SUMMARY:Camping Trip
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;

?>

HELP!

一位评论者建议我通过删除标题并回显$ ical var进行测试。以下是该测试的结果,为方便起见添加了换行符:

BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN 
BEGIN:VEVENT 
UID:21@homewoodphoto.jhu.edu 
DTSTAMP:20090929T212141Z 
DTSTART:20091001T230000Z 
DTEND:20091001T230000Z 
SUMMARY:little title 
END:VEVENT 
BEGIN:VEVENT 
UID:22@homewoodphoto.jhu.edu 
DTSTAMP:20090929T212141Z 
DTSTART:20090926T230000Z 
DTEND:20090927T010000Z 
SUMMARY:A big photo shoot 
END:VEVENT 
BEGIN:VEVENT 
UID:23@homewoodphoto.jhu.edu 
DTSTAMP:20090929T212141Z 
DTSTART:20091003T230000Z 
DTEND:20091004T010000Z 
SUMMARY:A big photo shoot 
END:VEVENT 
END:VCALENDAR

谢谢!

3 个答案:

答案 0 :(得分:20)

对于通过搜索偶然发现这一点的人来说,可能并不清楚问题是如何解决的。基本上,iCal规范要求\ r \ n用于换行符,并且行开头没有空格,这是在脚本中修复的,以使其有效。

使用ical时,我发现以下3个验证器最有帮助:

最基本的(错过空白):http://severinghaus.org/projects/icv/?url=

这个会抓到空白:http://icalvalid.cloudapp.net/Default.aspx

这个会抓住其他人没有但过于严格的事情:http://arnout.engelen.eu/icalendar-validator

此外,有关所有不同元素的最佳文档:http://www.kanzaki.com/docs/ical/

答案 1 :(得分:2)

初步猜测是您的阵列未正确填充。所以为了测试它,我将从删除

开始
//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=homewoodphoto_master.ics');

并更改$ slots = Slot :: find_all();到

$slots = Slot::find_all();
print_r($slots); 

确保正在设置对象数组。

然后从命令行或浏览器运行它,以确保在提交给谷歌之前按预期输出。

请尝试以下代码以避免空格:

<?php
require_once('../../_includes/initialize.php');

$ical = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN";

$slots = Slot::find_all();
foreach($slots as $slot) {
    $job = Job::find_by_id($slot->job_id);

    $start_stamp = strtotime($slot->start);
    $end_stamp = strtotime($slot->endtime);
    $dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time
    $dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time

    $summary = $job->title;

    $ical .= "BEGIN:VEVENT\n";
    $ical .= "UID:" . $slot->id . "@homewoodphoto.jhu.edu\n";
    $ical .= "DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z\n";
    $ical .= "DTSTART:" . $dtstart . "\n";
    $ical .= "DTEND:" . $dtend . "\n";
    $ical .= "SUMMARY:" . $summary . "\n";
    $ical .= "END:VEVENT\n";
}

$ical .= "\nEND:VCALENDAR";

//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=homewoodphoto_master.ics');
echo $ical;
exit;

?>

答案 2 :(得分:1)

感谢Mohammad的帮助,我们推断出这是缩进的代码,它会在导致错误的ics文件中添加空格。 M建议使用\ n换行符不起作用,但手动按Enter键创建换行符,但没有缩进下一行,似乎已经完成了。这是有效的代码:

<?php
require_once('../../_includes/initialize.php');

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
";

$slots = Slot::find_all();
foreach($slots as $slot) {
$job = Job::find_by_id($slot->job_id);

$start_stamp = strtotime($slot->start);
$end_stamp = strtotime($slot->endtime);
$dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time
$dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time

$summary = $job->title;

$ical .= "BEGIN:VEVENT
UID:" . $slot->id . "@homewoodphoto.jhu.edu
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:" . $dtstart . "
DTEND:" . $dtend . "
SUMMARY:" . $summary . "
END:VEVENT
";
}

$ical .= "END:VCALENDAR";

//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=homewoodphoto_master.ics');
echo $ical;
exit;
?>