编程语言
首页 > 编程语言> > PHP-EWS在多个附件上失败

PHP-EWS在多个附件上失败

作者:互联网

我用James Armes’s PHP-EWS library.

以下代码适用于单个附件,但与多个文件失败.

<?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.

我在这里想念什么?

解决方法:

在这里找到答案:

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

如果只有一个附件,Exchange基本上不使用数组,因此需要额外检查以确定从何处获取ID.

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

Exchange使用与收件人相同的结构.我发现这种情况不一致,但我确信这背后有一个原因.

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

标签:php,exchangewebservices,php-ews
来源: https://codeday.me/bug/20190830/1768130.html