2009-09-29 12 views
7

Ho costruito un sito in PHP 5 e MySQL con una tabella che tiene traccia dei servizi fotografici programmati. Voglio spingere un feed di quegli "eventi" programmati in un file ical.Dynamic ical creato dal database non funzionante

Originariamente asked this question e ho ottenuto una buona risposta da S. Gehrig. Ho ottenuto un file ical di esempio e sto aggiornando regolarmente in Google Calendar ogni volta che ho regolato manualmente il file in Dreamweaver. Tuttavia, ora che ho aggiunto il PHP dinamico che tira dal database, non funzionerà.

Ecco il 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; 

?> 

L'output di questo file è esattamente lo stesso come il manuale, versione hard-coded che ho a lavorare, per quanto posso dire. Qualcuno può vedere perché questo non funziona ????

PS Ecco il codice del file che funziona: l'ho appena pubblicato sul mio server e sottoscritto tramite URL in Google Calendar. Quando ero codificato nel secondo evento, si presentava a breve in Google Calendar da solo.

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

?> 

AIUTO!

Un commentatore mi ha suggerito di provare rimuovendo le intestazioni e facendo eco alla variabile $ ical. Ecco i risultati di questo test, con le interruzioni di riga aggiunte per comodità:

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 

Grazie!

+0

In Google Calendar, viene visualizzato un errore con il titolo "Errore impostazioni" e il testo: "Non è stato possibile analizzare il calendario all'URL richiesto." – rhodesjason

+0

Entourage non lo aprirà affatto e iCal dice "Questo file di calendario è illeggibile. Nessun evento è stato aggiunto al tuo calendario iCal." – rhodesjason

risposta

1

Grazie all'aiuto di Mohammad, abbiamo dedotto che era il codice rientrato l'aggiunta di spazi bianchi al file ics che stava causando gli errori. Il suggerimento di M di usare \ n interruzioni di riga non ha funzionato, ma premendo manualmente invio per creare interruzioni di riga, ma senza rientrare nella riga successiva, sembra averlo fatto. Ecco il codice che funziona:

<?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

L'ipotesi iniziale è che l'array non è popolato correttamente. Quindi per testarlo vorrei iniziare con la rimozione di

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

e la modifica di $ slot = Slot :: find_all(); a

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

per assicurarsi che l'array di oggetto sia impostato.

Quindi eseguilo dalla riga di comando o dal browser per assicurarti che venga visualizzato come previsto prima di inviarlo a google.

provare il seguente codice per evitare lo spazio bianco:

<?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

Mohammad, grazie. Ho effettivamente rimosso le intestazioni e l'ho appena fatto scorrere per echo $ ical per vedere cosa veniva emesso. Era esattamente lo stesso del test hardcoded, solo con date e riepiloghi diversi. ?? – rhodesjason

+0

Aggiungerò l'output di quel test alla fine della domanda sopra ... – rhodesjason

+0

Ok, sembra strano, così ho iniziato a testare il tuo output con un validatore ics: http://severinghaus.org/projects/icv/ e sembra che lo spazio bianco alla fine di ogni nodo lo stia causando! (nel tuo ciclo, per motivi di formattazione hai indentato il tuo codice che in stringhe aperte crea spazio bianco) UID: ". $ slot-> id." @ homewoodphoto.jhu.edu DTSTAMP: ". gmdate (' Ymd ').' T '. Gmdate (' His '). "DTDSTART:". $ Dtstart. " DTEND:". $ Dtend. " SOMMARIO:". $ Summary. " END: VEVENT"; aggiornerò la risposta originale con il codice corretto – Mohammad

20

Per le persone che inciampano su questo tramite una ricerca potrebbe non essere chiaro come il problema è stato risolto. Fondamentalmente, la specifica iCal richiede \ r \ n per le interruzioni di riga e non lo spazio bianco all'inizio delle righe, che è ciò che è stato corretto nello script per farlo funzionare.

Quando si lavora con iCal ho trovato i seguenti 3 validatori per essere il più utile:

più elementari (sig.na spazi bianchi): http://severinghaus.org/projects/icv/?url=

questo prenderà spazio bianco: http://icalvalid.cloudapp.net/Default.aspx

questo si prenderanno cose che gli altri non hanno ma è quasi troppo rigoroso: http://arnout.engelen.eu/icalendar-validator

Inoltre, la migliore documentazione su tutti i diversi elementi: http://www.kanzaki.com/docs/ical/