我必须使用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("&", "&", $title);
$title = str_replace("→", "-->", $title);
$title = str_replace("←", "<--", $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";
}
?>