2

因此,我使用php-ews库连接到我的 Microsoft Office 365 Exchange 电子邮件帐户来阅读电子邮件。我已成功连接到它,并设法检索到我需要的电子邮件列表。

现在的问题是我无法获取消息正文。阅读有关 Exchange Web 服务的文档,它说无法使用 FindItem() 获取正文,只能使用 GetItem(),这没关系。

现在我看到的问题如下:我尝试了我能找到的所有可能的例子,并且代码没有任何错误,它只是说“Soap客户端返回404状态”。

如果有人知道在哪里寻找解决方案,请告诉我。

编辑:

$ews = new Client('outlook.office365.com/EWS/OData/Me/Inbox/Messages', '###', '###', ClientEWS::VERSION_2010_SP2);

    $request = new FindItemType();

    $request->ItemShape = new ItemResponseShapeType();
    $request->ItemShape->BaseShape = DefaultShapeNamesType::DEFAULT_PROPERTIES;
    $request->ItemShape->BodyType = BodyTypeResponseType::BEST;

    $request->Traversal = ItemQueryTraversalType::SHALLOW;

    $request->ParentFolderIds = new NonEmptyArrayOfBaseFolderIdsType();
    $request->ParentFolderIds->DistinguishedFolderId = new DistinguishedFolderIdType();
    $request->ParentFolderIds->DistinguishedFolderId->Id = DistinguishedFolderIdNameType::INBOX;

    // sort order
    $request->SortOrder = new NonEmptyArrayOfFieldOrdersType();
    $request->SortOrder->FieldOrder = array();
    $order = new FieldOrderType();
    // sorts mails so that oldest appear first
    // more field uri definitions can be found from types.xsd (look for UnindexedFieldURIType)
    $order->FieldURI = new PathToUnindexedFieldType();
    $order->FieldURI->FieldURI = 'item:DateTimeReceived';
    $order->Order = 'Ascending';
    $request->SortOrder->FieldOrder[] = $order;

    try{
        //getting list of all emails - works perfectly
        $result = $ews->FindItem($request);

        if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success') {
            $count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
            $request = new GetItemType();
            $request->ItemShape = new ItemResponseShapeType();
            $request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;
            for ($i = 0; $i < $count; $i++){
                $message_id = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message[$i]->ItemId->Id;

                $messageItem = new ItemIdType();
                $messageItem->Id = $message_id;
                $request->ItemIds->ItemId[] = $messageItem;
            }

            // Here is your response
            // It throws an error here with the message "Soap client returned status of 404"
            $response = $ews->GetItem($request);

            print_r($response);
        }
        //print_r($result);
    } catch(\Exception $e) {
        echo $e->getMessage();
    }
4

2 回答 2

3

您似乎正在尝试为 Office365 使用新的 REST 端点

'outlook.office365.com/EWS/OData/Me/Inbox/Messages'

但是您尝试发出 EWS SOAP 请求,您应该用于 EWS SOAP 的端点是

https://outlook.office365.com/EWS/Exchange.asmx

您可能需要考虑使用新的 REST 接口作为 EWS/SOAP 的替代方案,但随后您需要按照https://dev.outlook.com/restapi使用 REST library.ouauth 等。

于 2016-10-28T01:17:33.147 回答
2

我建议你使用这个库的更新版本,它维护得更多并且具有更多功能(在这种情况下,它支持 Office 365 的 OAuth 登录)garethp/php-ews,. 使用时,您可以使用 Glen Scales 提供的端点,也可以只使用outlook.office365.com.

于 2016-10-30T14:48:53.063 回答