Search code examples
php-ews

Exception handling ExchangeWebServices php-ews


I use https://github.com/jamesiarmes/php-ews library to access my exchange account.

If I used correct credentials to create a ExchangeWebServices object, I get accurate response.

$ews = new ExchangeWebServices("outlook.office365.com", "tes@abc.com", "test123");

$request = new EWSType_FindItemType();

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

But If the credentials are wrong it breaks the site by throwing an exception as

EWS_Exception: SOAP client returned status of 401 in ExchangeWebServices->processResponse() 

Is there any way to get the response as "failed" or some boolean value instead of the error message?


Solution

  • There's no way to get the response as a boolean, but you can do something like

    $ews = new ExchangeWebServices("outlook.office365.com", "tes@abc.com", "test123");
    
    $request = new EWSType_FindItemType();
    
    try {
        $response = $ews->FindItem($request);
    } catch (\Exception $e) {
        //The response failed.
    }
    

    Also, that version of php-ews is out of date and unmaintained. Might I suggest you try https://github.com/Garethp/php-ews