2009-09-29 70 views
7

我建立了一個PHP 5和MySQL的網站,其中有一張表格,用於跟蹤預定的照片拍攝。我想將這些預定「事件」的提要推送到一個ical文件中。從數據庫不能正常工作的動態創建

我原本asked this question,並從S. Gehrig得到了很好的回答。我有一個示例文件正在工作,並且每當我在Dreamweaver中手動調整文件時,都會定期在Google日曆中進行更新。但是,現在我已經添加了從數據庫中動態獲取PHP的功能,它將無法工作。

這裏的PHP:

<?php 
require_once('../../_includes/initialize.php'); 

$ical = " BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN "; 

$slots = Slot::find_all(); 
foreach($slots as $slot) { 
    $job = Job::find_by_id($slot->job_id); 

    $start_stamp = strtotime($slot->start); 
    $end_stamp = strtotime($slot->endtime); 
    $dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time 
    $dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time 

    $summary = $job->title; 

    $ical .= " BEGIN:VEVENT 
    UID:" . $slot->id . "@homewoodphoto.jhu.edu 
    DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z 
    DTSTART:" . $dtstart . " 
    DTEND:" . $dtend . " 
    SUMMARY:" . $summary . " 
    END:VEVENT "; 
} 

$ical .= " END:VCALENDAR"; 

//set correct content-type-header 
header('Content-type: text/calendar; charset=utf-8'); 
header('Content-Disposition: inline; filename=homewoodphoto_master.ics'); 
echo $ical; 
exit; 

?> 

這個文件的輸出是完全一樣的說明書,硬編碼的版本,我有工作,據我可以告訴。任何人都可以看到爲什麼這不工作?

PS下面是正在工作的文件的代碼 - 我只是將它發佈到我的服務器上,並通過Google日曆中的URL進行了訂閱。當我在第二場比賽中進行硬編碼時,它很快出現在Google日曆中。

<?php 

$ical = "BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN 

BEGIN:VEVENT 
UID:" . md5(uniqid(mt_rand(), true)); . "@yourhost.test 
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z 
DTSTART:20090925T170000Z 
DTEND:20090928T035959Z 
SUMMARY:Bastille Day Party 
END:VEVENT 

BEGIN:VEVENT 
UID:" . md5(uniqid(mt_rand(), true)); . "@yourhost.test 
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z 
DTSTART:20090929T170000Z 
DTEND:20090930T035959Z 
SUMMARY:Camping Trip 
END:VEVENT 

END:VCALENDAR"; 

//set correct content-type-header 
header('Content-type: text/calendar; charset=utf-8'); 
header('Content-Disposition: inline; filename=calendar.ics'); 

echo $ical; 

exit; 

?> 

幫助!

評論者建議我通過刪除標題並回顯$ ical var進行測試。以下是該測試的結果,爲方便您添加換行符:

BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN 
BEGIN:VEVENT 
UID:[email protected] 
DTSTAMP:20090929T212141Z 
DTSTART:20091001T230000Z 
DTEND:20091001T230000Z 
SUMMARY:little title 
END:VEVENT 
BEGIN:VEVENT 
UID:[email protected] 
DTSTAMP:20090929T212141Z 
DTSTART:20090926T230000Z 
DTEND:20090927T010000Z 
SUMMARY:A big photo shoot 
END:VEVENT 
BEGIN:VEVENT 
UID:[email protected] 
DTSTAMP:20090929T212141Z 
DTSTART:20091003T230000Z 
DTEND:20091004T010000Z 
SUMMARY:A big photo shoot 
END:VEVENT 
END:VCALENDAR 

謝謝!

+0

在Google日曆中,我收到「設置錯誤」標題和文本錯誤:「我們無法在請求的網址解析日曆。」 – rhodesjason 2009-09-29 21:17:14

+0

Entourage根本無法打開它,iCal說:「這個日曆文件不可讀,沒有事件添加到您的iCal日曆中。」 – rhodesjason 2009-09-29 21:18:03

回答

1

感謝穆罕默德的幫助下,我們推斷,這是縮進代碼添加到空白是造成錯誤的ics文件。 M建議使用\ n換行符不起作用,但手動按Enter鍵來創建換行符,但不縮進下一行,似乎已經完成了。下面是工作代碼:

<?php 
require_once('../../_includes/initialize.php'); 

$ical = "BEGIN:VCALENDAR 
VERSION:2.0 
PRODID:-//hacksw/handcal//NONSGML v1.0//EN 
"; 

$slots = Slot::find_all(); 
foreach($slots as $slot) { 
$job = Job::find_by_id($slot->job_id); 

$start_stamp = strtotime($slot->start); 
$end_stamp = strtotime($slot->endtime); 
$dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time 
$dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time 

$summary = $job->title; 

$ical .= "BEGIN:VEVENT 
UID:" . $slot->id . "@homewoodphoto.jhu.edu 
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z 
DTSTART:" . $dtstart . " 
DTEND:" . $dtend . " 
SUMMARY:" . $summary . " 
END:VEVENT 
"; 
} 

$ical .= "END:VCALENDAR"; 

//set correct content-type-header 
header('Content-type: text/calendar; charset=utf-8'); 
header('Content-Disposition: inline; filename=homewoodphoto_master.ics'); 
echo $ical; 
exit; 
?> 
2

初步猜測會是你的數組沒有正確填充。所以要測試它,我將開始刪除

//set correct content-type-header 
header('Content-type: text/calendar; charset=utf-8'); 
header('Content-Disposition: inline; filename=homewoodphoto_master.ics'); 

並更改$ slots = Slot :: find_all();到

$slots = Slot::find_all(); 
print_r($slots); 

確保您的對象數組正在設置。

然後從命令行或瀏覽器運行它,以確保其在提交給Google之前按預期輸出。

嘗試下面的代碼,以避免空白:

<?php 
require_once('../../_includes/initialize.php'); 

$ical = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN"; 

$slots = Slot::find_all(); 
foreach($slots as $slot) { 
    $job = Job::find_by_id($slot->job_id); 

    $start_stamp = strtotime($slot->start); 
    $end_stamp = strtotime($slot->endtime); 
    $dtstart = gmdate('Ymd', $start_stamp).'T'. gmdate('His', $start_stamp) . "Z"; // converts to UTC time 
    $dtend = gmdate('Ymd', $end_stamp).'T'. gmdate('His', $end_stamp) . "Z"; // converts to UTC time 

    $summary = $job->title; 

    $ical .= "BEGIN:VEVENT\n"; 
    $ical .= "UID:" . $slot->id . "@homewoodphoto.jhu.edu\n"; 
    $ical .= "DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z\n"; 
    $ical .= "DTSTART:" . $dtstart . "\n"; 
    $ical .= "DTEND:" . $dtend . "\n"; 
    $ical .= "SUMMARY:" . $summary . "\n"; 
    $ical .= "END:VEVENT\n"; 
} 

$ical .= "\nEND:VCALENDAR"; 

//set correct content-type-header 
header('Content-type: text/calendar; charset=utf-8'); 
header('Content-Disposition: inline; filename=homewoodphoto_master.ics'); 
echo $ical; 
exit; 

?> 
+0

穆罕默德,謝謝。我實際上刪除了頭文件,只是運行它來回顯$ ical來查看輸出內容。它與硬編碼測試完全相同,只是日期和摘要不同。 ?? – rhodesjason 2009-09-29 21:20:12

+0

我會將該測試的輸出添加到上述問題的末尾...... – rhodesjason 2009-09-29 21:22:17

+0

好吧,它看起來很奇怪,所以我開始使用ics驗證器測試您的輸出: http://severinghaus.org/projects/icv/ 似乎像每個節點末尾的白色空間正在造成它! (在你的循環中,爲了格式化的原因,你已經縮進了你的代碼,它在開放的字符串中,它創建了空白) UID:「。$ slot-> id。」@ homewoodphoto.jhu.edu DTSTAMP:「.gmdate '。'T'。gmdate('His')。「Z DTSTART:」。$ dtstart。「 DTEND:」。$ dtend。「 摘要:」。$ summary。「 END:VEVENT」; 我將用正確的代碼更新原始答案 – Mohammad 2009-09-29 21:58:13

20

對於誰通過搜索這個絆倒人的問題是如何解決的,可能不是很清楚。基本上,iCal規範要求\ r \ n用於換行符,並且在行首沒有空格,這是腳本中修復的使其工作的原因。

當與iCal工作,我已經找到了以下3個驗證是最有幫助的:

最基本的(未命中空格):http://severinghaus.org/projects/icv/?url=

這個人會趕上空白:http://icalvalid.cloudapp.net/Default.aspx

這一個會抓住別人沒有的東西,但幾乎是太嚴格了:http://arnout.engelen.eu/icalendar-validator

此外,所有不同元素的最佳文檔:http://www.kanzaki.com/docs/ical/