Access MS Exchange Outlook Calendar using PHP

Posted: April 9, 2013 in web

<?php

function __autoload($class_name)
{
    // Start from the base path and determine the location from the class name,
    $base_path = 'php-ews-master';
    $include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';

    return (file_exists($include_file) ? require_once $include_file : false);
}

$ews = new ExchangeWebServices("webmail.someserver.com", "username", "password");

// Set init class
$request = new EWSType_FindItemType();

// Use this to search only the items in the parent directory in question or use ::SOFT_DELETED
// to identify "soft deleted" items, i.e. not visible and not in the trash can.
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
// This identifies the set of properties to return in an item or folder response
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;

// Define the timeframe to load calendar items
$request->CalendarView = new EWSType_CalendarViewType();

$request->CalendarView->StartDate = '2013-04-01T15:18:34+03:00'; // an ISO8601 date e.g. 2012-06-12T15:18:34+03:00
$request->CalendarView->EndDate = '2013-04-25T15:18:34+03:00' ;// an ISO8601 date later than the above

// Only look in the "calendars folder"
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::CALENDAR;

// Send request
$response = $ews->FindItem($request);

// Loop through each item if event(s) were found in the timeframe specified
if ($response->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView > 0){
    $events = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
    foreach ($events as $event){
        $id = $event->ItemId->Id;
        $change_key = $event->ItemId->ChangeKey;
        $start = $event->Start;
        $end = $event->End;
        $subject = $event->Subject;
        $location = $event->Location;
		
		echo ' Start=' . $start. ' End=' . $end. 'Subject =' . $subject . 'Location =' . $location . '<br />';
    }
}
else {
    // No items returned
}
?>

from:

https://github.com/jamesiarmes/php-ews

Advertisements
Comments
  1. irakli says:

    what i need to use this code? just paste it and that is all??

  2. rajesh says:

    i used this code but i got following error please explain why this is happening.

    my code is below :

    <?php
    function __autoload($class_name)
    {
    // Start from the base path and determine the location from the class name,
    $base_path = 'php-ews-master';
    echo $include_file = str_replace('_', '/', $class_name) . '.php';echo '’;

    return (file_exists($include_file) ? require_once $include_file : false);
    }

    //$server = “https://webmail.brinkster.com”;
    $username = “emailid”;
    $password = “password”;

    $ews = new ExchangeWebServices(“webmail.brinkster.com”, $username, $password);

    // Set init class
    $request = new EWSType_FindItemType();

    // Use this to search only the items in the parent directory in question or use ::SOFT_DELETED
    // to identify “soft deleted” items, i.e. not visible and not in the trash can.
    $request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
    // This identifies the set of properties to return in an item or folder response
    $request->ItemShape = new EWSType_ItemResponseShapeType();
    $request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;

    // Define the timeframe to load calendar items
    $request->CalendarView = new EWSType_CalendarViewType();

    $request->CalendarView->StartDate = ‘2014-04-18T15:18:34+03:00’; // an ISO8601 date e.g. 2012-06-12T15:18:34+03:00
    $request->CalendarView->EndDate = ‘2014-04-21T15:18:34+03:00’ ;// an ISO8601 date later than the above

    // Only look in the “calendars folder”
    $request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
    $request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
    $request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::CALENDAR;

    // Send request
    $response = $ews->FindItem($request);

    // Loop through each item if event(s) were found in the timeframe specified
    if ($response->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView > 0){
    $events = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
    foreach ($events as $event){
    $id = $event->ItemId->Id;
    $change_key = $event->ItemId->ChangeKey;
    $start = $event->Start;
    $end = $event->End;
    $subject = $event->Subject;
    $location = $event->Location;
    echo ‘ Start=’ . $start. ‘ End=’ . $end. ‘Subject =’ . $subject . ‘Location =’ . $location . ”;
    }
    }
    else {
    // No items returned
    }

    ?>

    Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in D:\xampp\htdocs\php-ews-master\ExchangeWebServices.php:472 Stack trace: #0 [internal function]: SoapClient->__call(‘GetItem’, Array) #1 D:\xampp\htdocs\php-ews-master\ExchangeWebServices.php(472): NTLMSoapClient_Exchange->GetItem(Object(EWSType_GetItemType)) #2 D:\xampp\htdocs\php-ews-master\msexchange.php(53): ExchangeWebServices->GetItem(Object(EWSType_GetItemType)) #3 {main} thrown in D:\xampp\htdocs\php-ews-master\ExchangeWebServices.php on line 472

  3. kosinus says:

    Hi! It works fine for me =)
    I have pasted a copy of the github project and this code on my project respository.
    I ‘ve got my events just modifying the server name and user/pass and modifying the startdate and enddate.
    THank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s