添加到网站的gCal按钮

时间:2015-10-28 19:49:30

标签: php google-calendar-api

我正在寻找一种方法来在我的网站上显示一个按钮,以便用户可以自动将事件添加到他们的谷歌日历中,类似于http://www.addthisevent.com/所执行的操作(目前我正在做什么)。我正在使用)。

到目前为止,我已经在线查看,但我只能通过将其添加到我的日历并从中生成代码来找到一种手动方式。但是,我想用PHP自动完成。

我不想继续的理由是让自己控制网站上的所有资源。

编辑:我正在寻找一个用户可以点击的链接,新的标签会打开,添加日历约会打开并预先填写(由 addthisevent 完成)。 该解决方案应该只是PHP并接受谷歌日历所需的所有变量。

1 个答案:

答案 0 :(得分:1)

这里的一些代码可能会有所帮助:

Google日历:

<?php
function generate_calendar_button($name, $description, DateTime $start, DateTime $end, $location, $mysite_url, $mysite_name) {
   $url = 'http://www.google.com/calendar/event?action=TEMPLATE';
   $parts = array();
   $parts['text'] = urlencode($name);
   $parts['details'] = urlencode($description);
   $parts['dates'] = urlencode($start->format("Ymd\THis\Z")) . "/" . urlencode($end->format("Ymd\THis\Z"));
   $parts['location'] = urlencode($location);
   $parts['sprop'] = urlencode($mysite_url);
   $full_link = $url;
   foreach ($parts as $key => $value) {
      $full_link .= "&" . $key . "=" . $value;
   }
   $full_link .= "&sprop=name:" . urlencode($mysite_name);
   return '<a href="' . $full_link . '" target="_blank"><img src="http://www.google.com/calendar/images/ext/gc_button1.gif" border=0></a>';
}

print generate_calendar_button(
    "Boulevard Summer Show",
    "Starting Friday 15th June, the Boulevard Summer Show 2012 features new sets and musical numbers led by the legendary Betty Legs Diamond, performing alongside established favourites and some rather fetching new faces, all held together by the incomparable Miss Rory.",
    new DateTime("2012-07-15 8:00 GMT"),
    new DateTime("2012-07-15 10:00 GMT"),
    "123 Example Lane, Exampleville",
    "http://www.northernpink.co.uk/",
    "Northern Pink"
);

?>

Apple iCal:

<?php // Add a custom button after the event export options that display after the event content


$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;

?>