异常处理 ExchangeWebServices php-ews

Exception handling ExchangeWebServices php-ews

我使用 https://github.com/jamesiarmes/php-ews 图书馆访问我的交易账户。

如果我使用正确的凭据创建 ExchangeWebServices 对象,我会得到准确的响应。

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

$request = new EWSType_FindItemType();

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

但是如果凭据错误,它会通过抛出异常来破坏站点

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

是否有任何方法可以将响应作为 "failed" 或某个布尔值而不是错误消息?

无法将响应作为布尔值获取,但您可以这样做

$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.
}

此外,php-ews 的那个版本已经过时且无人维护。我可以建议你试试 https://github.com/Garethp/php-ews