DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   php (http://www.daniweb.com/code/php.html)
-   -   PHP WebDAV for Microsoft Exchange Server (http://www.daniweb.com/code/snippet471.html)

Troy php syntax
Mar 17th, 2006
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.

  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. ?>