Search code examples
wsdloffice365exchangewebservicesphp-ews

PHP library for Office365 EWS with GetRooms


Whole last week i struggled with Php libraries for EWS. I tried Package365Ews and Php-ews but both of them are missing core feature for me, or it's not documented - GetRooms. Do anyone know how to handle it, or know another library implementing this?


Solution

  • I personally would suggest my own library, garethp/php-ews.

    It's got simple usage, but not everything is covered under simpler API's. EWS is a large thing, and documenting everything would be intense. That being said, I can certainly help you translate existing documentation by Microsoft to using this code. And, if you find yourself with more issues after this post, I check my Github daily, so logging an issue against my repository will get more help in a better place for a back and forth.

    But first, let me outlay how to perform functions that aren't directly documented. Like GetRooms. My API wraps around EWS, it doesn't block your access to it. So even though I've made no obvious way to do a GetRooms, it's still there. Like this

    <?php
    
    use garethp\ews\API;
    use garethp\ews\API\Type;
    
    $api = API::fromUsernameAndPassword($server, $username, $password);
    
    //Build Request
    
    $result = $api->getClient()->GetRooms($request);
    
    var_dump($result);
    

    So, the question becomes, how do we build the request? Well, thankfully EWS is very well documented in XML. First, find the article that describes what you're trying to do, then look for the XML. I'm not 100% what you want to do, but I'll use this article as a base. The XML that we're going to try to replicate is

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
        <t:RequestServerVersion Version="Exchange2010" />
      </soap:Header>
      <soap:Body>
        <m:GetRooms>
          <m:RoomList>
            <t:EmailAddress>bldg3rooms@contoso.com</t:EmailAddress>
          </m:RoomList>
        </m:GetRooms>
      </soap:Body>
    </soap:Envelope>
    

    You can skip the header, and the <m:GetRooms> part, those are built for you. What we're focused on is the payload you want to send, which is

    <m:RoomList>
        <t:EmailAddress>bldg3rooms@contoso.com</t:EmailAddress>
    </m:RoomList>
    

    We want to make our request look like that. So, in our code, our request will look like:

    $request = array (
        'RoomsList' => array (
            'EmailAddress' => 'bldg3rooms@contoso.com'
        )
    );
    
    $request = Type::buildFromArray($request);
    

    And this will be translated to XML for you for the SOAP call. Using this method, for any functions that aren't documented or outright supported, you can easily still use them and just refer to the official Microsoft documentation for any request you need to make