Does your organization use Microsoft Exchange?
Do you have a need to incorporate your Exchange data with your PHP web applications?

You can use the WebDAV methods to query your Microsoft Exchange Server using the PHP scripting language. Once you wrap your brain around these techniques, the sky is the limit for developing custom Outlook-integrated web applications.

The code below only shows one example of how you can use WebDAV with Exchange. It shows how to iterate over all the subfolders in a user's inbox and display them.

The code below makes use of 2 of my most popular PHP classes:

  1. class_http
  2. class_xml

You can read more about those 2 classes and see more PHP WebDAV examples at www.troywolf.com/articles.


// Modify the paths to these class files as needed.

// Change these values for your Exchange Server.
$exchange_server = "http://NameOfYourExchangeServer";
$exchange_username = "YourExchangeUsername";
$exchange_password = "YourExchangePassword";

// We use Troy's http class object to send the XML-formatted WebDAV request
// to the Exchange Server and to receive the response from the Exchange Server.
// The response is also XML-formatted.
$h = new http();

$h->headers["Content-Type"] = 'text/xml; charset="UTF-8"';

// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_depth_header.asp
$h->headers["Depth"] = "0";

$h->headers["Translate"] = "f";

// The trickiest part is forming your WebDAV query. This example shows how to
// find all the folders in the inbox for a user named 'twolf'.
$h->xmlrequest = '<?xml version="1.0"?>';
$h->xmlrequest .= <<<END
<a:searchrequest xmlns:a="DAV:" xmlns:s="http://schemas.microsoft.com/exchange/security/">
       SELECT "DAV:displayname"
       FROM SCOPE('hierarchical traversal of "$exchange_server/Exchange/twolf/inbox"')
// IMPORTANT -- the END line above must be completely left aligned -- no white space at all.

// The 'fetch' method does the work of sending and receiving the request.
// NOTICE the last parameter passed--'SEARCH' in this example. That is the
// HTTP verb that you must correctly set according to the type of WebDAV request
// you are making.  The examples on this page use either 'PROPFIND' or 'SEARCH'.
if (!$h->fetch($exchange_server."/Exchange/twolf/inbox", 0, null, $exchange_username, $exchange_password, "SEARCH")) {
  echo "<h2>There is a problem with the http request!</h2>";
  echo $h->log;

// Note: The following lines can be uncommented to aid in debugging.
#echo "<pre>".$h->log."</pre><hr />\n";
#echo "<pre>".$h->header."</pre><hr />\n";
#echo "<pre>".$h->body."</pre><hr />\n";
// Or, these next lines will display the result as an XML doc in the browser.
#header('Content-type: text/xml');
#echo $h->body;

// The assumption now is that we've got an XML result back from the Exchange
// Server, so let's parse the XML into an object we can more easily access.
// For this task, we'll use Troy's xml class object.
$x = new xml();
if (!$x->fetch($h->body)) {
    echo "<h2>There was a problem parsing your XML!</h2>";
    echo "<pre>".$h->log."</pre><hr />\n";
    echo "<pre>".$h->header."</pre><hr />\n";
    echo "<pre>".$h->body."</pre><hr />\n";
    echo "<pre>".$x->log."</pre><hr />\n";

// You should now have an object that is an array of objects and arrays that
// makes it easy to access the parts you need. These next lines can be
// uncommented to make a raw display of the data object.
#echo "<pre>\n";
#echo "</pre>\n";

// And finally, an example of iterating the inbox folder names and url's to
// display in the browser. I also show you 2 methods to link to the folders.
// One uses the href provided in the response which opens the folder using OWA.
// The other is an Outlook style link to open the folder in the Outlook desktop
// client.
echo '<table border="1">';
foreach($x->data->A_MULTISTATUS[0]->A_RESPONSE as $idx=>$item) {
    echo '<tr>'
        .'<td><a href="'.$item->A_HREF[0]->_text.'">Click to open via OWA</a></td>'
        .'<td><a href="Outlook:Inbox/'.$item->A_PROPSTAT[0]->A_PROP[0]->A_DISPLAYNAME[0]->_text.'">Click to open via Outlook</a></td>'
echo "<table>\n";

10 Years
Discussion Span
Last Post by dskanth

Very interesting script! Just about the only PHP script I've been able to find on this topic.

Im trying to write a script to access the public calendar and publish this to our intranet, but we're using Forms Based Authentication on your exchange server. This means Im not able to use this script because you have to query /exchweb/bin/auth/owaauth.dll with the username and password, save the incoming cookie and further use this to gain access to the goods. Is it possible to make a FBA compatible version of this script?

- HybridDK


Like HybridDK I am having the same issue. It would be valuable to know how to use this script with FBA compatability.

Thank you to anyone who knows.

- Smatter.tv


I have to extract contacts from an Exchange server over SSL authentification.

$exchange_server = "https://mail.justexchange.com/exchange/";
$exchange_username = "demo1@JustExchange.net";
$exchange_password = "Password1!";

Using this code I could not connect.
This is the error message I've got:Warning: fgets() [function.fgets]: SSL: fatal protocol error in C:\wamp\www\asterisk\class_http.php on line 127

Can someone help me PLZ?

Edited by Dani: Formatting fixed


This looks like it's just what I need for my current project, but I can't get the example to work.

After modifying the connection parameters for my situation, I get the following strange error:

There is a problem with the http request:

New http() object instantiated.
fetch() called
url: https://exchange.mydomain.net/Exchange/myusername/inbox
getFromUrl() called
Could not open connection. Error 0: The operation completed successfully.

I've twiddled around with this for a while, but I can't get it to work. Anyone have any clues?


I have got the example to work... almost that is!

the only error I get now is:

: Invalid argument supplied for foreach() in G:...

any suggestions?



I have downloaded both:

  1. class_http
  2. class_xml

I can run the script the the problem now im facing is how to retrieve the particular email property (i.e the email body text), i know the URN is
"urn:schemas:httpmail:textdescription" but i just dont know how to create a proper WebDAV query, keep having "400 Bad Request" error.

I tried using the following code to retrieve:
$h->xmlrequest = '<?xml version="1.0"?>';
$h->xmlrequest .= <<<END
<a:searchrequest xmlns:a="DAV:">
SELECT "urn:schemas:httpmail:subject" FROM "$exchange_server/Exchange/vincent/inbox"


to get the subject and content of a mail you can use

SELECT "urn:schemas:httpmail:textdescription" , "urn:schemas:httpmail:subject", "urn:schemas:httpmail:datereceived" FROM "$exchange_server/pillsburylawmail/mailboxnow/Inbox/"



I know someone posted the question regarding Forms Based Authentication (FBA) awhile back but it does not look like there was any conclusion. Has anyone been able to develop a version that can be implemented with FBA in place on the Exchange Server? Any assistance would be greatly appreciated.



There seem to be a few people here with the same issue as me.

I am trying to connect to Exchange 2007 with PHP and Forms Based Authentication is on.

It's been a couple of years since some of these posts. Did anyone find a solution?

I've got several scripts that are successfully interfacing and have been for ages with an on-site Exchange 2003, but a new solution we're using is remotely hosted and set up with FBA. Any help that anyone can offer would be greatly appreciated!



How can we delete the Messages from Server?
I can retrieve the message from exchange server but how can I delete those messages through script once mail retrived.


I have the SAME question, how do we delete Appointments or Email messages??? Please answer!

Best regards!


How can i add a calendar event to the exchange server with this code snippet?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.