代码之家  ›  专栏  ›  技术社区  ›  Yugaprashanthan

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

  •  0
  • Yugaprashanthan  · 技术社区  · 7 年前

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

    下面给出了生成ical文件的代码,因此我需要通过此ical文件将文件与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 回复  |  直到 7 年前
        1
  •  0
  •   Yugaprashanthan    7 年前

    我知道同步谷歌日历的编程方式是使用谷歌api进行同步,所以我使用谷歌api来完成这项工作,感谢大家的支持

        2
  •  0
  •   anmari    7 年前

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

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