PHP WebDAV for Microsoft Exchange Server

Troy Troy is offline Offline Mar 17th, 2006, 4:10 pm |
0
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.
Quick reply to this message  
PHP Syntax
  1. <?php
  2.  
  3. // Modify the paths to these class files as needed.
  4. require_once("class_http.php");
  5. require_once("class_xml.php");
  6.  
  7. // Change these values for your Exchange Server.
  8. $exchange_server = "http://NameOfYourExchangeServer";
  9. $exchange_username = "YourExchangeUsername";
  10. $exchange_password = "YourExchangePassword";
  11.  
  12. // We use Troy's http class object to send the XML-formatted WebDAV request
  13. // to the Exchange Server and to receive the response from the Exchange Server.
  14. // The response is also XML-formatted.
  15. $h = new http();
  16.  
  17. $h->headers["Content-Type"] = 'text/xml; charset="UTF-8"';
  18.  
  19. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_depth_header.asp
  20. $h->headers["Depth"] = "0";
  21.  
  22. $h->headers["Translate"] = "f";
  23.  
  24. // The trickiest part is forming your WebDAV query. This example shows how to
  25. // find all the folders in the inbox for a user named 'twolf'.
  26. $h->xmlrequest = '<?xml version="1.0"?>';
  27. $h->xmlrequest .= <<<END
  28. <a:searchrequest xmlns:a="DAV:" xmlns:s="http://schemas.microsoft.com/exchange/security/">
  29. <a:sql>
  30. SELECT "DAV:displayname"
  31. FROM SCOPE('hierarchical traversal of "$exchange_server/Exchange/twolf/inbox"')
  32. </a:sql>
  33. </a:searchrequest>
  34. END;
  35. // IMPORTANT -- the END line above must be completely left aligned -- no white space at all.
  36.  
  37. // The 'fetch' method does the work of sending and receiving the request.
  38. // NOTICE the last parameter passed--'SEARCH' in this example. That is the
  39. // HTTP verb that you must correctly set according to the type of WebDAV request
  40. // you are making. The examples on this page use either 'PROPFIND' or 'SEARCH'.
  41. if (!$h->fetch($exchange_server."/Exchange/twolf/inbox", 0, null, $exchange_username, $exchange_password, "SEARCH")) {
  42. echo "<h2>There is a problem with the http request!</h2>";
  43. echo $h->log;
  44. exit();
  45. }
  46.  
  47. // Note: The following lines can be uncommented to aid in debugging.
  48. #echo "<pre>".$h->log."</pre><hr />\n";
  49. #echo "<pre>".$h->header."</pre><hr />\n";
  50. #echo "<pre>".$h->body."</pre><hr />\n";
  51. #exit();
  52. // Or, these next lines will display the result as an XML doc in the browser.
  53. #header('Content-type: text/xml');
  54. #echo $h->body;
  55. #exit();
  56.  
  57. // The assumption now is that we've got an XML result back from the Exchange
  58. // Server, so let's parse the XML into an object we can more easily access.
  59. // For this task, we'll use Troy's xml class object.
  60. $x = new xml();
  61. if (!$x->fetch($h->body)) {
  62. echo "<h2>There was a problem parsing your XML!</h2>";
  63. echo "<pre>".$h->log."</pre><hr />\n";
  64. echo "<pre>".$h->header."</pre><hr />\n";
  65. echo "<pre>".$h->body."</pre><hr />\n";
  66. echo "<pre>".$x->log."</pre><hr />\n";
  67. exit();
  68. }
  69.  
  70. // You should now have an object that is an array of objects and arrays that
  71. // makes it easy to access the parts you need. These next lines can be
  72. // uncommented to make a raw display of the data object.
  73. #echo "<pre>\n";
  74. #print_r($x->data);
  75. #echo "</pre>\n";
  76. #exit();
  77.  
  78. // And finally, an example of iterating the inbox folder names and url's to
  79. // display in the browser. I also show you 2 methods to link to the folders.
  80. // One uses the href provided in the response which opens the folder using OWA.
  81. // The other is an Outlook style link to open the folder in the Outlook desktop
  82. // client.
  83. echo '<table border="1">';
  84. foreach($x->data->A_MULTISTATUS[0]->A_RESPONSE as $idx=>$item) {
  85. echo '<tr>'
  86. .'<td>'.$item->A_PROPSTAT[0]->A_PROP[0]->A_DISPLAYNAME[0]->_text.'</td>'
  87. .'<td><a href="'.$item->A_HREF[0]->_text.'">Click to open via OWA</a></td>'
  88. .'<td><a href="Outlook:Inbox/'.$item->A_PROPSTAT[0]->A_PROP[0]->A_DISPLAYNAME[0]->_text.'">Click to open via Outlook</a></td>'
  89. ."</tr>\n";
  90. }
  91. echo "<table>\n";
  92.  
  93. ?>
0
HybridDK HybridDK is offline Offline | Jul 13th, 2006
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
 
0
smatter.tv smatter.tv is offline Offline | Jul 18th, 2006
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
 
0
chakermed chakermed is offline Offline | Oct 16th, 2006
Hi,
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?
 
0
greenie2600 greenie2600 is offline Offline | Feb 27th, 2007
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?
 
0
drdal drdal is offline Offline | Mar 9th, 2007
I have got the example to work... almost that is!

the only error I get now is:

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

any suggestions?
 
0
vzent vzent is offline Offline | May 25th, 2007
Hi,

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:">
<a:sql>
SELECT "urn:schemas:httpmail:subject" FROM "$exchange_server/Exchange/vincent/inbox"
</a:sql>
</a:searchrequest>
END;
 
0
sujinss sujinss is offline Offline | Sep 10th, 2007
Hi
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/"


Sujin
 
0
ahoffman ahoffman is offline Offline | May 1st, 2008
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.

Thanks,
Aaron
 
0
rj_mayer rj_mayer is offline Offline | Nov 21st, 2008
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!

Richard
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC