HowTo: Publish Exchange Public Calendar with PHP

My goal was to extract appointment info from an Exchange Public Calendar to be displayed on an intranet site. This can be accomplished using the Exchange Web Services. Microsoft has examples using C# and the EWS API to access the data. Also you can use Perl or PHP module code contributed by the communities to extract this. I chose to go the PHP route vs. trying to install all the wonderful perl modules/dependencies and/or learning C#.

Using the code found here:
http://code.google.com/p/php-ews/

Here is a really simple / stripped example of what I did to extract the data. I have used this to create an HTML page for an event list for the current day, a breakdown of events for X number of upcoming days, and even created a detailed day/time calendar grid extracting color categories from the calendar. Note times come back in UTC from Exchange. Also I dump the entire response into the HTML for debugging info.

Tested in Exchange 2007.


$today = Date("Y-m-d");
$host = 'mail.domain.com';
$username = 'restricted-user';
$password = 'password';
$localTZ = 'America/New_York';
$daysahead = '1'; // Today Only
$EWScalendar = 'SharedCalendar@domain.com';
$ews = new ExchangeWebServices ($host, $username, $password);
$request = new EWSType_FindItemType();
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
$request->CalendarView = new EWSType_CalendarViewType();
$request->CalendarView->StartDate = date ('c',strtotime("$today"));
$request->CalendarView->EndDate = date ('c',strtotime("$today + $daysahead days"));
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::CALENDAR;
$mailBox = new EWSType_EmailAddressType();
$mailBox->EmailAddress = "$EWScalendar";
$request->ParentFolderIds->DistinguishedFolderId->Mailbox = $mailBox;
$response = $ews->FindItem($request);
$TotalItemCount = $response->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
// LOOP through the Event Data and Parse each item
for ( $ItemID = 0; $ItemID < $TotalItemCount; $ItemID++) {
--do something with item data--
$subject = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem[$ItemID]->Subject;
}
echo '<!--';
echo 'Raw Data Captured<br><pre>';
print_r($response);
echo '</pre> ';
echo '-->'