Adding Events to Microsoft Outlook from PHP using ICAL

David Carr

2 min read - 1st Mar, 2012

I've recently been trying to find the best way of creating a calendar event outlook from a php driven application.

At first I looked into way of communication with the exchange server directly I soon realized this wasn't going to be an easy route, I would need to use another protocol to do this something like CalDEV or similar would be needed.

For my needs I didn't need the calendar to by synced only to add events when created so I decided to issue a request from php to outlook via an ics file that would be generated when requested. This approach is really simple to do. When the file is created I could either attach it to an email or server it directly from the browser, I chose the latter option.

As I will be creating the file dynamically only when its requested I would need to send a few parameters to a php script which will create the file.

The parameters are as follows:

date         = date of event
startTime = time of event only the hour and minutes
endTime  = time event finished only the hour and minutes
subject    = subject line of the event
desc        = option for more details

Using these parameters I simply wrap them with their values in a link to a script that will create the ics file.

<a href="ical.php?date=20120302&amp;amp;startTime=1300&amp;amp;endTime=1400&amp;amp;subject=Meeting&amp;amp;desc=Meeting to discuss processes.">Add Appointment to your Outlook Calendar</a>

Here's the contents of my icap.php file:

<?php
  $date      = $_GET['date'];
  $startTime = $_GET['startTime'];
  $endTime   = $_GET['endTime'];
  $subject   = $_GET['subject'];
  $desc      = $_GET['desc'];

  $ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "example.com
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:".$date."T".$startTime."00Z
DTEND:".$date."T".$endTime."00Z
SUMMARY:".$subject."
DESCRIPTION:".$desc."
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;
?>

The date needs to be in the format of YYMMDD so remove any spaces or - from the date also I chose not to use seconds in the time and have them in the script, you can easily have them passed in the link as well just remove the 00 from DTSTART and DTEND.

When this script is called it collected the parameters and outputs a newly generated calendar.ics file open that with outlook and you'll have the option to save the event.

This route is perfect for letting the end user decide if they want to add the event or not.

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.