如何通过来自Exchange的SOAP递归地从“root”获取所有子文件夹?

时间:2012-09-12 16:37:05

标签: php soap exchange-server

在网上搜索了几个小时后,你是我最后的希望:

我必须构建一个从Microsoft Exchange Server读取已发送和传入邮件的系统。我找到了以下脚本: 用于PHP的Exchange-Web-Services(Heartspring) https://github.com/Heartspring/Exchange-Web-Services-for-PHP

现有的get_messages()函数返回文件夹的所有消息,例如“inbox”。到目前为止一切都很清楚。当我想从“发送”中获取所有消息时,我的问题就出现了 - 文件夹 - 我尝试了很多单词,从“发送”到“已发送的项目”;没有任何结果(邮箱不可用)

我的想法是获取文件夹“root”的所有子文件夹并写下:

include "init.php";

$ec = new ExchangeClient();
$ec->init("bambullis@123.de", "", NULL, "https://amxprd3610.outlook.com/EWS/Services.wsdl");

$folders = $ec->get_subfolders("root"); 

foreach($folders as $folder) {

    print_r($folder); 

}

这就是我得到的:

stdClass Object
(
    [FolderId] => stdClass Object
        (
            [Id] => AAAeAGJhbWJ1bGxpc0BzdHVrZSbi5kZQAuAAAAAABw352p5E4yS5voYF9ELBmiAQBXYPdO6NZAQ6T9C3xviT7xAAAAC1iXAAA=
            [ChangeKey] => AQAAABYAAABXYPdO6NZAQ6T9C3xviAALNCey
        )

    [DisplayName] => Oberste Ebene des Informationsspeichers
    [TotalCount] => 0
    [ChildFolderCount] => 16
    [UnreadCount] => 0
)

(我知道FolderId-> Id是base64编码的,我出于安全原因修改了上面的字符串; o))

现在我尝试列出这个目录的子文件夹(我添加了一个邮箱,看看,如果值“ChildFolderCount”会改变,它确实如此):

...
print_r($folder); 

print_r($ec->get_subfolders($folder->FolderId->Id)); 
...

这是我得到的错误: 请求未通过架构验证:“Id”属性无效

我做错了什么?如何递归地从“root”获取所有子文件夹?感谢这个可以帮助我的可爱的家伙!

1 个答案:

答案 0 :(得分:2)

EWS-PHP get_subfolders方法默认使用TraversalType”,因此仅搜索标识的文件夹并仅返回尚未删除的项目的文件夹ID 要搜索已识别的父文件夹的所有子文件夹并仅返回尚未删除的项目的文件夹ID,您应使用“”TraversalType。 例如:

<?php

include "init.php";

class myExchangeClient extends ExchangeClient {
    public function get_subfolders_deep($ParentFolderId = "inbox", $Distinguished = TRUE) {
        $this->setup();

        $FolderItem = new stdClass();
        $FolderItem->FolderShape = new stdClass();
        $FolderItem->ParentFolderIds = new stdClass();

        $FolderItem->FolderShape->BaseShape = "Default";
        /* 
        **  See http://msdn.microsoft.com/en-us/library/exchange/exchangewebservices.folderquerytraversaltype(v=exchg.140).aspx 
        **  Deep Traversal: Searches in all subfolders of the identified parent folder and returns only the folder IDs for items that
        **  have not been deleted.
        */
        $FolderItem->Traversal = "Deep";

        if ($Distinguished) {
          $FolderItem->ParentFolderIds->DistinguishedFolderId = new stdClass();
          $FolderItem->ParentFolderIds->DistinguishedFolderId->Id = $ParentFolderId;
        } else {
          $FolderItem->ParentFolderIds->FolderId = new stdClass();
          $FolderItem->ParentFolderIds->FolderId->Id = $ParentFolderId;
        }

        $response = $this->client->FindFolder($FolderItem);

        if ($response->ResponseMessages->FindFolderResponseMessage->ResponseCode == "NoError") {
          $folders = array();

          if (!is_array($response->ResponseMessages->FindFolderResponseMessage->RootFolder->Folders->Folder)) {
              $folders[] = $response->ResponseMessages->FindFolderResponseMessage->RootFolder->Folders->Folder;
          } else {
              $folders = $response->ResponseMessages->FindFolderResponseMessage->RootFolder->Folders->Folder;
          }

          return $folders;
        } else {
          $this->lastError = $response->ResponseMessages->FindFolderResponseMessage->ResponseCode;
        }
    }
}

$ec = new myExchangeClient();
$ec->init("bambullis@123.de", "", NULL, "https://amxprd3610.outlook.com/EWS/Services.wsdl");

$folders = $ec->get_subfolders_deep("root");
echo "<pre>".print_r($folders,true)."</pre>\n";

?>

无论如何,查看ExchangeClient类源代码,已发送项目的FolderID应为“ sentitems ”。