有没有人创建了适用于Google日历的ics iCalendar生成器功能?

时间:2011-12-06 14:43:51

标签: php google-calendar-api icalendar

我正在尝试生成一个成功导入Google日历的.ics文件。谷歌日历证明特别困难(我已经让Outlook和Apple iCal完美运行)。

有没有人有一个php函数或类来创建正确的标题并插入已被证明可以与Google日历一起使用的事件?

3 个答案:

答案 0 :(得分:2)

我还看到这是一篇较旧的文章,但没有被接受的答案。

最近,我需要在我的Web应用程序中实现ical生成...但是我的还需要使用给定公式重复发生事件。为此,我使用了以下软件包的组合:

  1. 对于一代人:https://github.com/markuspoerschke/iCal
  2. 对于重复发生的事件:https://github.com/simshaun/recurr

我使用recurr根据重复公式生成日期列表:

public function generate_dates_array(\DateTime $end_date = null) : array
{
    if(! $end_date) {
        $end_date = new \DateTime('midnight');
    }

    $rule = new \Recurr\Rule($this->recurrence_rule, $this->created_at->setTime(0,0));

    $rule->setUntil($end_date);

    $transformer = new ArrayTransformer();

    $dates = $transformer->transform($rule);

    $dateArray = [];

    foreach($dates as $date) {
        $dateArray[] = $date->getStart();
    }

    return $dateArray;
}

然后设置我的日历:

$vCalendar = new \Eluceo\iCal\Component\Calendar('YourUrlHere');
$vCalendar->setName('YOUR NAME HERE');
$vCalendar->setDescription('YOUR DESCRIPTION HERE');

然后在这些日期上循环,将事件添加到我的日历对象中……确保从用户选择的时区将我的时间调整为UTC。

$today = new Carbon();
$end_date = $today->addMonth();

foreach($events as $event) {
  $dates = $event->generate_dates_array($end_date); // function shown in above code snippet
  $duration = $event->duration ? $event->duration : 30;

  // Each Occurrence for event
  foreach($dates as $date) {
    $vEvent = new \Eluceo\iCal\Component\Event();

    $date_string = "{$date->format('Y-m-d')} {$event->time}";

    $start_time = new Carbon($date_string, $user->time_zone);
    $start_time->setTimezone('UTC');
    $vEvent->setDtStart($start_time);

    $end_time = new Carbon($date_string, $user->time_zone);
    $end_time = $end_time->addMinutes($duration);
    $end_time->setTimezone('UTC');
    $vEvent->setDtEnd($end_time);

    $vEvent->setNoTime(false);
    $vEvent->setSummary($event->name);
    $vEvent->setDescription("$event->description");
    $vCalendar->addComponent($vEvent);
  }
}

一切设置正确后,我输出日历。这意味着我可以使用生成该文件的URL将日历导入Google日历(或任何其他日历程序),并每天对其进行ping操作以进行更新。 (URL必须是可公开访问的,顺便说一下,这要求我使用guid而不是user_id作为标识符)

header('Content-Type: text/calendar; charset=utf-8');
echo $vCalendar->render();

希望这可以帮助其他人降落在这里!

答案 1 :(得分:0)

我做了类似的事情但只创建了一个URL而不是iCal。

查看https://support.google.com/calendar/answer/3033039

填写表单并生成HTML然后在我用来创建ical的php文件中我刚创建了一个单独的东西来用变量替换细节。

EG:

if($calType === "iCal"){
    //set correct content-type-header
    header('Content-Disposition: attachment; filename=AddToCalendar.ics');
    header('Content-Type: text/calendar; charset=utf-8');

    $desc = str_replace("\n", '\n', $desc);
    $desc = str_replace(array("\r","\n","\r"), '', $desc);

    $ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Smartershows.com//TheBatteryShow//EN
X-WR-CALNAME;CHARSET=utf-8:".$subj."
METHOD:PUBLISH
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
SUMMARY;CHARSET=utf-8:".$subj."
LOCATION;CHARSET=utf-8:".$loc."
URL:".$url."
UID:30fc985de98ed0dabfeb13722e3c82259fcd33e3 
DESCRIPTION:".$desc."
DTSTART:".$sDate."T".$sTime."
DTEND:".$eDate."T".$eTime."
END:VEVENT
END:VCALENDAR";

return $ical;
exit;
}else if($calType === "gCal"){

    $href = "http://www.google.com/calendar/event?";
    $href .= "action=TEMPLATE";
    $href .= "&text=".urlencode($subj);
    $href .= "&dates=".$sDate."T".$sTime."/".$eDate."T".$eTime;
    $href .= "&details=".urlencode($desc);
    $href .= "&location=".urlencode($loc);
    $href .= "&trp=false";
    $href .= "&sprop=".urlencode($subj);
    $href .= "&sprop=name:".urlencode($url);

    return '<a href="'.$href.'" target="_blank"><strong>Download for Gmail</strong></a>';

}

因此,if中的第一个块正在制作ical,第二个块正在使用相同的信息构建一个google将采取并填充日历页面的URL。

这方面的缺点是,你只能将基本的东西放入描述...不是很多花哨的HTML或任何东西......除非你htmlencode我想的一切但即便如此我不知道什么是字符限制网址是......

Hotmail和Yahoo邮件也可以这种方式填充日历,但不幸的是,它们都没有一个可以预先生成链接的好工具(我可以找到)。

希望这有帮助!

答案 2 :(得分:0)

这是一个旧的,唯一的答案是不被接受,因此不确定以下代码是否解决了您的问题。但是,我在我自己的搜索中遇到了这个尝试修复我的错误,现在我有一个解决方案,我想我会分享它。以下代码已使用最新的Outlook和Gmail进行了测试。

使用Outlook导致我的错误的原因是我使用\n而不是\r\n来查看事件详情。因此,正如您将在下面看到的那样,我使用\r\n作为事件,并使用\ n作为其他每个标记,以便PHP正确处理它。也许类似的问题导致了gmail的问题?

  

警告,此代码不会阻止标头注入,请   负责任地使用; - )

<?php
    date_default_timezone_set('America/New_York');
    //CONFIGURE HERE
    $fromName           = "John Doe";
    $fromEmail          = "john.doe@example.com";
    $toName             = "Your Name";
    $toEmail            = 'yourname@example.com';
    $start              = new DateTime('2017-08-15 15:00');
    $end                = new DateTime('2017-08-15 16:00');
    $summary            = "Hello World Event";
    //END CONFIGURATION

    $uid                = "0123456789";
    $headers            = array();
    $boundary           = "_CAL_" . uniqid("B",true) . "_B_";
    $headers[]          = "MIME-Version: 1.0";
    $headers[]          = "Content-Type: multipart/alternative; boundary=\"".$boundary."\"";
    $headers[]          = "To: \"{$toName}\" <{$toEmail}>";
    $headers[]          = "From: \"{$fromName}\" <{$fromEmail}>";

    $calendarLines      = array(
        "BEGIN:VCALENDAR",
        "METHOD:REQUEST",
        "PRODID:-//PHP//MeetingRequest//EN",
        "VERSION:2.0",
        "BEGIN:VEVENT",
        "ORGANIZER;CN={$fromName}:MAILTO:{$fromEmail}",
        "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN={$toName}:MAILTO:{$toEmail}",
        "DESCRIPTION:{$summary}",
        "SUMMARY:{$summary}",
        "DTSTART:".$start->setTimezone(new DateTimeZone('UTC'))->format('Ymd\THis\Z'),
        "DTEND:".$end->setTimezone(new DateTimeZone('UTC'))->format('Ymd\THis\Z'),
        "UID:{$uid}",
        "CLASS:PUBLIC",
        "PRIORITY:5",
        "DTSTAMP:".gmdate('Ymd\THis\Z'),
        "TRANSP:OPAQUE",
        "STATUS:CONFIRMED",
        "SEQUENCE:0",
        "LOCATION:123 Any Street",
        "BEGIN:VALARM",
        "ACTION:DISPLAY",
        "DESCRIPTION:REMINDER",
        "TRIGGER;RELATED=START:-PT15M",
        "END:VALARM",
        "END:VEVENT",
        "END:VCALENDAR"
    );


    $calendarBase64     = base64_encode(implode("\r\n",$calendarLines));
    //ensure we don't have lines longer than 70 characters for older computers:
    $calendarResult     = wordwrap($calendarBase64,68,"\n",true);

    $emailLines = array(
        "--{$boundary}",
        "Content-Type: text/html; charset=\"iso - 8859 - 1\"",
        "Content-Transfer-Encoding: quoted-printable",
        "",
        "<html><body>",
        "<h1>Hello World</h1>",
        "<p>This is a calendar event test</p>",
        "</body></html>",
        "",
        "--{$boundary}",
        "Content-Type: text/calendar; charset=\"utf - 8\"; method=REQUEST",
        "Content-Transfer-Encoding: base64",
        "",
        $calendarResult,
        "",
        "--{$boundary}--"
    );
    $emailContent   = implode("\n",$emailLines);

    $headersResult      = implode("\n",$headers);
    mail($toEmail, $summary, $emailContent, $headersResult );
    echo("<pre>".htmlentities($headersResult)."\n\n".htmlentities($emailContent)."</pre>");
    echo("<br /><br />");
    echo("<pre>".base64_decode($calendarResult)."</pre>");