我使用php生成ics
文件,只要ics文件中只包含一个事件,一切正常。
但是,我试图将多个事件放在同一个ics文件中。
我使用存储在MYSQL中的信息来生成ics文件。
我遇到的问题是,当我运行我的代码时,我没有任何错误,但是没有生成任何ics文件!
这是我的全部代码:
<?php
class ICS {
var $data;
var $name;
function ICS($start,$end,$name,$description,$location) {
$this->name = $name;
$this->data = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nDTSTART:".date("Ymd\THis\Z",strtotime($start))."\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="'.$this->name.'.ics"');
Header('Content-Length: '.strlen($this->data));
Header('Connection: close');
echo $this->data;
}
}
?>
<?php
$sql = "SELECT * FROM mytable" ;
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event .= new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
$event->show();
}
}
?>
如果我删除这行代码并将其放在我的while循环之外,那么everrything会再次起作用,但它会生成一个只有1个事件的ics文件!
$event = new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
$event->show();
有人可以就此问题提出建议吗?
提前致谢。
编辑:
我现在已经创建了ics文件,但它不会导入到日历中,当我尝试在日历中添加事件时出现失败错误:
<?php
class ICS {
var $data;
var $name;
function ICS($start,$end,$name,$description,$location) {
$this->name = $name;
$this->data = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nDTSTART:".date("Ymd\THis\Z",strtotime($start))."\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
echo $this->data;
}
}
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="test.ics"');
Header('Content-Length: '.strlen($this->data));
?>
<?php
$sql = "SELECT * FROM mytable";
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event .= new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
}
}
$event->show();
Header('Connection: close');
?>
第二次编辑:
根据下面给出的答案,我已经将我的代码更改为这样,但这根本不会创建任何ics文件,也没有错误:
<?php
date_default_timezone_set("Europe/London");
class ICS {
private $event;
private $name;
function addEvent($start, $end, $name, $description, $location) {
$this->name = $name;
$this->event .= "BEGIN:VEVENT\nDTSTART:".date("Ymd\THis\Z",strtotime($start))."\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\n";
}
function output() {
if ($this->event) {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="'.$this->name.'.ics"');
Header('Content-Length: '.strlen($this->event));
echo "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\n".$this->event."END:VCALENDAR\n";
Header('Connection: close');
}
}
}
?>
<?php
$event = new ICS;
$sql = "SELECT * FROM mytable";
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event->addEvent($start_date,$end_date,$title,$details,$location);
}
}
$event->output();
?>
第三次编辑
好吧,看起来我到了某个地方......我已经将我的代码更改为以下内容,当我运行我的php页面时,我得到了test.ics文件,但是当我打开它时,它是空的。它什么都没有!
<?php
class ICS {
var $data;
var $name ='test';
function addEvent($start, $end, $name, $description, $location) {
$this->name = $name;
$this->event .= "BEGIN:VEVENT\nDTSTART:".date("Ymd\THis\Z",strtotime($start))."\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\n";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename=test.ics');
Header('Content-Length: '.strlen($this->data));
//echo $this->data;
echo "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\n".$this->data."END:VCALENDAR\n";
Header('Connection: close');
}
}
?>
<?php
$event = new ICS;
$sql = "SELECT * FROM mytable" ;
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event->addEvent("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
}
}
$event->show();
?>