首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php或jquery将ICAL文件与google日历同步?

如何使用php或jquery将ICAL文件与google日历同步?
EN

Stack Overflow用户
提问于 2018-02-06 13:08:40
回答 2查看 973关注 0票数 0

我必须使用php或jquery将ical文件与google日历和iCalender onclick进行同步,我已经创建了ical文件,但我不知道如何同步。请帮帮忙,谢谢。

代码,我使用它来生成下面给出的ical文件,所以我需要传递这个ical文件来与google日历同步该文件

代码语言:javascript
复制
<?php
$con = mysqli_connect("localhost","root","","calendarevent");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  // Create connection

// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
} 

$sql = "SELECT id,title,dates FROM events";
$result = $con->query($sql);

/*if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}*/


$ics_contents  = "BEGIN:VCALENDAR\n";
$ics_contents .= "VERSION:2.0\n";
$ics_contents .= "PRODID:PHP\n";
$ics_contents .= "METHOD:PUBLISH\n";
$ics_contents .= "X-WR-CALNAME:Schedule\n";

# Change the timezone as well daylight settings if need be
$ics_contents .= "X-WR-TIMEZONE:America/New_York\n";
$ics_contents .= "BEGIN:VTIMEZONE\n";
$ics_contents .= "TZID:America/New_York\n";
$ics_contents .= "BEGIN:DAYLIGHT\n";
$ics_contents .= "TZOFFSETFROM:-0500\n";
$ics_contents .= "TZOFFSETTO:-0400\n";
$ics_contents .= "DTSTART:20070311T020000\n";
$ics_contents .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\n";
$ics_contents .= "TZNAME:EDT\n";
$ics_contents .= "END:DAYLIGHT\n";
$ics_contents .= "BEGIN:STANDARD\n";
$ics_contents .= "TZOFFSETFROM:-0400\n";
$ics_contents .= "TZOFFSETTO:-0500\n";
$ics_contents .= "DTSTART:20071104T020000\n";
$ics_contents .= "RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\n";
$ics_contents .= "TZNAME:EST\n";
$ics_contents .= "END:STANDARD\n";
$ics_contents .= "END:VTIMEZONE\n";

while ($schedule_details = $result->fetch_assoc()) {
  $id            = $schedule_details['id'];
  $title         = $schedule_details['title'];
  $dates         = $schedule_details['dates'];


  $estart_date   = str_replace("-", "", $dates);
 $eend_date   = str_replace("-", "", $dates);



  # Replace some HTML tags
  $title          = str_replace("<br>", "\\n",   $title);
  $title          = str_replace("&amp;", "&",    $title);
  $title          = str_replace("&rarr;", "-->", $title);
  $title          = str_replace("&larr;", "<--", $title);
  $title          = str_replace(",", "\\,",      $title);
  $title          = str_replace(";", "\\;",      $title);


  # Change TZID if need be
  $ics_contents .= "BEGIN:VEVENT\n";
  $ics_contents .= "DTSTART;TZID=America/New_York"     . $estart_date . "\n";
  $ics_contents .= "DTEND:"       . $eend_date . "\n";
  $ics_contents .= "DTSTAMP:"     . date('Ymd') . "T". date('His') . "Z\n";
  $ics_contents .= "SUMMARY:"     . $title . "\n";
  $ics_contents .= "UID:"         . $id . "\n";
  $ics_contents .= "SEQUENCE:0\n";
  $ics_contents .= "END:VEVENT\n";
}

$ics_contents .= "END:VCALENDAR\n";
/* print_r($ics_contents);exit();*/
# File to write the contents

$ics_file   = 'schedule.ics';

if (is_writable($ics_file)) {
  if (!$handle = fopen($ics_file, 'w')) {
     echo "Cannot open file ($ics_file)\n\n";
     exit;
  }

  # Write $ics_contents to opened file
  if (fwrite($handle, $ics_contents) === FALSE) {
    echo "Cannot write to file ($ics_file)\n\n";
    exit;
  }

  # echo "Success, wrote to <b>schedule.ics</b><br>\n\n";

  fclose($handle);

} else {
  echo "The file <b>$ics_file</b> is not writables\n\n";
}
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-10 12:14:34

我知道同步google日历的编程方法是使用google api进行同步,所以我使用google api完成了这项工作,谢谢大家的支持

票数 0
EN

Stack Overflow用户

发布于 2018-02-09 11:09:41

“同步”由拥有google日历或icalendar的用户决定。这是一个单向同步,更新从您的源到目标应用程序。

您需要提供ics文件/提要的url。用户使用url订阅订阅源。谷歌和icalendar将检查提要的更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48635858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档