PHP中的订阅日历URL

时间:2014-01-04 08:39:04

标签: php calendar icalendar

嗨, 我正在开发一个基于事件管理的项目,每个用户都会在那里存储包含开始和结束日期的事件详细信息。现在我想要一个功能来订阅桌面应用程序(Outlook,Ical for apple和google calendar),这样任何新事件都会保存在数据库中,它会自动同步到桌面应用程序。实现此功能的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

以下是生成单个事件iCal输出的示例代码:

$eventData = array(
        'title'       => $event->getTitle(),
        'address'     => $address,
        'description' => strip_tags($event->getBody()),
        'stage'       => $stage,
        'date'        => $event->getDate()
    );

    // Build the ics file


    $ical= 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTEND:' . $this->dateToCal($eventData['date']) . '
UID:' . md5($eventData['title']) . '
DTSTAMP:' . time() . '
LOCATION:' . $eventData['address'] . '
DESCRIPTION:' . $eventData['description'] . '
URL;VALUE=URI:http://go.okdo.it' . '
SUMMARY:' . $eventData['title'] . '
DTSTART:' . $this->dateToCal($eventData['date']) . '
END:VEVENT
END:VCALENDAR';

这是将日期对象格式化为iCal格式的函数:

function dateToCal($timestamp)
{
    return date('Ymd\This', time()) . 'Z';
}

在输出内容之前,您需要设置适当的标题:

   header('Content-type: text/calendar; charset=utf-8');
   header('Content-Disposition: attachment; filename=' . $task->getTitle());
   echo $ical;

答案 1 :(得分:0)

我从@masnun的答案开始,这就是我所得到的。

一旦您使该文件输出了有效的法律摘要(有在线验证程序要检查),您就可以通过webcal://yoursite.com/your-ical-script.php在浏览器中访问此文件,它将提供您一个订阅选项

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

foreach($class_list_temp as &$c){
    $c["description"] = strip_tags($c["blurb"] . " " . $c["url"]);
    $c["dtstart"] = strtotime($c["date"]);
$c["dtend"] = (int)($c["dtstart"]) + 60*60; // hour duration
    $ical .= "\nBEGIN:VEVENT
UID:" . md5($c["title"] . $c["id"] . $c["dtstart"]) . "
DTSTAMP:" . dateToCal(time()) . "
" . wordwrap("DESCRIPTION:" . $c["description"], 75, "\n") . "
URL;VALUE=URI:" . $c["url"] . "
SUMMARY:" . $c["title"] . "
DTSTART:" . dateToCal($c["dtstart"]) . "
DTEND:" . dateToCal($c["dtend"]) . "
END:VEVENT";
}

$ical .= "\nEND:VCALENDAR";
$ical = str_replace(["\r\n", "\r", "\n"], "\r\n", $ical); //fix linebreaks

function dateToCal($timestamp){
    return gmdate("Ymd",$timestamp)."T". gmdate("His",$timestamp) . "Z";
}
header("Content-type: text/calendar; charset=utf-8");
header("Content-Disposition: attachment; filename=calendar.ics");
echo $ical;