Programing

PHP를 사용하여 Google 캘린더에서 읽을 ical 파일을 동적으로 게시하려면 어떻게해야합니까?

lottogame 2020. 8. 16. 21:43
반응형

PHP를 사용하여 Google 캘린더에서 읽을 ical 파일을 동적으로 게시하려면 어떻게해야합니까?


PHP ical에 대한 모든 Google 검색은 phpicalendar와 IN ical 파일을 구문 분석하거나 읽는 방법을 표시합니다. 데이터베이스에서 이벤트를 가져 와서 ical 형식으로 쓰는 PHP 파일을 작성하고 싶습니다.

내 문제는 두 가지 질문에 답할 수있는 곳을 찾을 수 없다는 것입니다.

  1. 머리글, 파일 형식, 바닥 글 등을 포함한 정확한 ical 형식 은 무엇입니까 ? 즉, Google 캘린더 등에서 제대로 읽기 위해 파일에 정확히 무엇이 있어야합니까?
  2. .php 확장자를 사용하여이 파일을 빌드하는 경우 어떻게 ical로 게시합니까? 새 .ics 파일에 써야합니까? 아니면 내용이 올바른 형식이면 Google 캘린더 등이 .php 파일을 ical로 읽습니까? (내용이 실제로 CSS 인 경우 style.css.php 파일은 CSS 파일로 읽히는 것과 매우 유사합니다.)

여러분 모두가 저에게 줄 수있는 어떤 도움이라도 대단히 감사하겠습니다 !!!


Google 캘린더에 *.ics-확장자 가 필요하지 않은 경우 이는 매우 간단합니다 (서버에서 URL을 다시 작성해야 함).

$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:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
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;

캐싱, 텍스트 인코딩 등에 관한 몇 가지 문제가있을 수 있지만 클라이언트가 iCalendar 파일을 제공하고 있다고 생각하게 만드는 데 필요한 전부입니다. 그러나이 간단한 코드로 실험을 시작할 수 있습니다.


Stefan Gehrig의 답변과 Dave None의 답변 (및 mmmshuddup의 답변) 외에 개인적인 경험에 대한 메모 :

http://severinghaus.org/projects/icv/ 에서 ICS 유효성 검사기를 사용할 때 \ n과 PHP_EOL을 모두 사용하여 유효성 검사 문제가 발생했습니다 .

제대로 유효성을 검사하기 위해 \ r \ n을 사용해야한다는 것을 배웠으므로 이것이 내 솔루션이었습니다.

function dateToCal($timestamp) {
  return date('Ymd\Tgis\Z', $timestamp);
}

function escapeString($string) {
  return preg_replace('/([\,;])/','\\\$1', $string);
}    

    $eol = "\r\n";
    $load = "BEGIN:VCALENDAR" . $eol .
    "VERSION:2.0" . $eol .
    "PRODID:-//project/author//NONSGML v1.0//EN" . $eol .
    "CALSCALE:GREGORIAN" . $eol .
    "BEGIN:VEVENT" . $eol .
    "DTEND:" . dateToCal($end) . $eol .
    "UID:" . $id . $eol .
    "DTSTAMP:" . dateToCal(time()) . $eol .
    "DESCRIPTION:" . htmlspecialchars($title) . $eol .
    "URL;VALUE=URI:" . htmlspecialchars($url) . $eol .
    "SUMMARY:" . htmlspecialchars($description) . $eol .
    "DTSTART:" . dateToCal($start) . $eol .
    "END:VEVENT" . $eol .
    "END:VCALENDAR";

    $filename="Event-".$id;

    // Set the headers
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $filename);

    // Dump load
    echo $load;

이로 인해 구문 분석 오류가 중지되고 ICS 파일이 제대로 검증되었습니다.


ics 파일을 쉽게 만들 수 있는 훌륭한 eluceo / ical 패키지가 있습니다.

다음은 문서의 사용 예입니다.

// 1. Create new calendar
$vCalendar = new \Eluceo\iCal\Component\Calendar('www.example.com');

// 2. Create an event
$vEvent = new \Eluceo\iCal\Component\Event();
$vEvent->setDtStart(new \DateTime('2012-12-24'));
$vEvent->setDtEnd(new \DateTime('2012-12-24'));
$vEvent->setNoTime(true);
$vEvent->setSummary('Christmas');

// Adding Timezone (optional)
$vEvent->setUseTimezone(true);

// 3. Add event to calendar
$vCalendar->addComponent($vEvent);

// 4. Set headers
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="cal.ics"');

// 5. Output
echo $vCalendar->render();

조금 늦었을 수도 있지만 여기 실제 사양에 대한 링크가 있습니다. http://tools.ietf.org/html/rfc5545 1


http://www.kanzaki.com/docs/ical/ has a slightly more readable version of the older spec. It helps as a starting point - many things are still the same.

Also on my site, I have

  1. Some lists of useful resources (see sidebar bottom right) on
    • ical Spec RFC 5545
    • ical Testing Resources
  2. Some notes recorded on my journey working with .ics over the last few years. In particular, you may find this repeating events 'cheatsheet' to be useful.

.ics areas that need careful handling:

  • 'all day' events
  • types of dates (timezone, UTC, or local 'floating') - nb to understand distinction
  • interoperability of recurrence rules

  1. Exact ical format: http://www.ietf.org/rfc/rfc2445.txt
  2. According to the spec, it has to end in .ics

Edit: actually I'm not sure - line 6186 gives an example in .ics naming format, but it also states you can use url parameters. I don't think it matters, so long as the MIME type is correct.

Edit: Example from wikipedia: http://en.wikipedia.org/wiki/ICalendar

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR

MIME type is configured on the server.


Make sure you format the string like this or it wont work

 $content = "BEGIN:VCALENDAR\n".
            "VERSION:2.0\n".
            "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n".
            "BEGIN:VEVENT\n".
            "UID:".uniqid()."\n".
            "DTSTAMP:".$time."\n".
            "DTSTART:".$time."\n".
            "DTEND:".$time."\n".
            "SUMMARY:".$summary."\n".
            "END:VEVENT\n".
            "END:VCALENDAR";

참고URL : https://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calen

반응형