How can I use PHP to dynamically publish an ical file to be read by Google Calendar?

前端 未结 7 643
名媛妹妹
名媛妹妹 2020-11-28 17:18

Any Google search on PHP ical just brings up phpicalendar and how to parse or read IN ical files. I just want to write a PHP file that pulls events from my database and writ

7条回答
  •  攒了一身酷
    2020-11-28 17:55

    There is an excellent eluceo/ical package that allows you to easily create ics files.

    Here is an example usage from docs:

    // 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();
    

提交回复
热议问题