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

时间:2018-02-06 05:08:40

标签: php jquery google-calendar-api icalendar

我必须使用php或jquery同步ogle文件和iCalender onclick我已经创建了文件,但我不知道如何同步。请帮助,谢谢。

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

<?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";
}
?>

2 个答案:

答案 0 :(得分:0)

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

您提供了ics文件/ Feed的网址。用户使用URL订阅了订阅源。 Google和icalendar会检查Feed以获取更新。

答案 1 :(得分:0)

我知道同步谷歌日历的程序化方法是使用google api进行同步,所以我使用google api来完成此操作,感谢大家的支持