2014-10-06 211 views
3

我使用James Armes's PHP-EWS libraryPHP-EWS在多个附件上失败

下面的代码可以很好地处理单个附件,但会失败并导致多个文件。

<?php 
$msgRequest->MessageDisposition = 'SaveOnly'; 

$msgResponse = $ews->CreateItem($msgRequest); 
$msgResponseItems = $msgResponse->ResponseMessages->CreateItemResponseMessage->Items; 

// Create attachment(s) 
$attachments = array(); 
$i = 0; 
foreach ($message_details['attachment'] as $attachment) { 
    $attachments[$i] = new EWSType_FileAttachmentType(); 
    $attachments[$i]->Content = file_get_contents($attachment['path'] . '/' . $attachment['file']); 
    $attachments[$i]->Name = $attachment['file']; 
    $i++; 
} 
// 
// Attach files to message 
$attRequest = new EWSType_CreateAttachmentType(); 
$attRequest->ParentItemId = $msgResponseItems->Message->ItemId; 
$attRequest->Attachments = new EWSType_NonEmptyArrayOfAttachmentsType(); 
$attRequest->Attachments->FileAttachment = $attachments; 

$attResponse = $ews->CreateAttachment($attRequest); 
$attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId; 

// Save message id from create attachment response 
$msgItemId = new EWSType_ItemIdType(); 
$msgItemId->ChangeKey = $attResponseId->RootItemChangeKey; 
$msgItemId->Id = $attResponseId->RootItemId; 

// Send and save message 
$msgSendRequest = new EWSType_SendItemType(); 
$msgSendRequest->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType(); 
$msgSendRequest->ItemIds->ItemId = $msgItemId; 
$msgSendRequest->SaveItemToFolder = true; 
$msgSendResponse = $ews->SendItem($msgSendRequest); 
$response = $msgSendResponse->ResponseMessages->SendItemResponseMessage; 
?> 

$ ews-> SendItem()返回此错误:

Uncaught SoapFault exception: [a:ErrorSchemaValidation] The request failed schema validation: The required attribute 'Id' is missing.

我怎么会错过吗?

回答

4

找到答案在这里:

https://github.com/jamesiarmes/php-ews/issues/132

基本上,如果只有一个附件,所以需要额外的检查,以确定从哪里得到的ID Exchange不使用数组。

if(!is_array($attResponse->ResponseMessages->CreateAttachmentResponseMessage)) 
    $attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId; 
else { 
    $attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage[0]->Attachments->FileAttachment->AttachmentId; 
} 

Exchange使用与收件人相同的结构。我觉得这个不一致,但我相信这背后有一个原因。

我希望有人会从提高这一点中受益。