PHP-EWS SOAP 响应是一个对象 - 如何获取特定部分?
我使用此代码与 PHP-EWS 在 Exchange 2010 邮箱上创建日历项目。
<?php
include ("ExchangeWebServices.php");
include ("EWS_Exception.php");
include ("EWSType.php");
include ("NTLMSoapClient.php");
include ("NTLMStream.php");
include ("NTLMStream/Exchange.php");
include ("NTLMSoapClient/Exchange.php");
$host = "email.domain.com";
$username = "asdfasdf";
$password = "123";
$ews = new ExchangeWebServices($host, $username, $password);
$request->SendMeetingInvitations = 'SendToNone';
$request->SavedItemFolderId->DistinguishedFolderId->Id = 'calendar';
$request->Items->CalendarItem->Subject = 'this is the subject of the email';
$request->Items->CalendarItem->Start = date('c', strtotime('today'));
$request->Items->CalendarItem->End = date('c', strtotime('today + 1 day'));
$request->Items->CalendarItem->IsAllDayEvent = true;
$request->Items->CalendarItem->LegacyFreeBusyStatus = 'Free';
$request->Items->CalendarItem->Categories->String = $category;
$request->Items->CalendarItem->Body->BodyType = 'Text';
$request->Items->CalendarItem->Body->_ = $body;
$response = $ews->CreateItem($request);
print_r($response);
?>
这工作正常,输出到浏览器的响应如下:
stdClass Object ( [ResponseMessages] => stdClass Object ( [CreateItemResponseMessage] => stdClass Object ( [ResponseCode] => NoError [ResponseClass] => Success [Items] => stdClass Object ( [CalendarItem] => stdClass Object ( [ItemId] => stdClass Object ( [Id] => AAASAHJydXVza2FAc3VicmFkLmNvbQBGAAAAAACyAesJgtiMR71+s/kuiAieBwBrnLmGR6RGTakOvWxNjsbfAAAAQwN1AABrnLmGR6RGTakOvWxNjsbfAABcYtppAAA= [ChangeKey] => DwAAABYAAABrnLmGR6RGTakOvWxNjsbfAABcYx9q ) ) ) ) ) )
我想要的唯一部分是 [Id] =>
后面的 128 个字符的字符串。如果我将此对象转换为字符串,我可以像这样解析该字符串:
$var = 'stdClass Object ( [ResponseMessages] => stdClass Object ( [CreateItemResponseMessage] => stdClass Object ( [ResponseCode] => NoError [ResponseClass] => Success [Items] => stdClass Object ( [CalendarItem] => stdClass Object ( [ItemId] => stdClass Object ( [Id] => AAASAHJydXVza2FAc3VicmFkLmNvbQBGAAAAAACyAesJgtiMR71+s/kuiAieBwBrnLmGR6RGTakOvWxNjsbfAAAAQwN1AABrnLmGR6RGTakOvWxNjsbfAABcYtpzAAA= [ChangeKey] => DwAAABYAAABrnLmGR6RGTakOvWxNjsbfAABcYzc4 ) ) ) ) ) )';
$regex_pattern = "/ /";
$matches = preg_split($regex_pattern,$var);
print_r($matches[36]);
但是,我很难将对象转换为字符串。另外,如果响应稍有变化,我将不会得到正确的返回值。查看对象并返回 [Id] =>
之后的值的最佳方法是什么?
I am using this code along with PHP-EWS to create a Calendar Item on an Exchange 2010 mailbox.
<?php
include ("ExchangeWebServices.php");
include ("EWS_Exception.php");
include ("EWSType.php");
include ("NTLMSoapClient.php");
include ("NTLMStream.php");
include ("NTLMStream/Exchange.php");
include ("NTLMSoapClient/Exchange.php");
$host = "email.domain.com";
$username = "asdfasdf";
$password = "123";
$ews = new ExchangeWebServices($host, $username, $password);
$request->SendMeetingInvitations = 'SendToNone';
$request->SavedItemFolderId->DistinguishedFolderId->Id = 'calendar';
$request->Items->CalendarItem->Subject = 'this is the subject of the email';
$request->Items->CalendarItem->Start = date('c', strtotime('today'));
$request->Items->CalendarItem->End = date('c', strtotime('today + 1 day'));
$request->Items->CalendarItem->IsAllDayEvent = true;
$request->Items->CalendarItem->LegacyFreeBusyStatus = 'Free';
$request->Items->CalendarItem->Categories->String = $category;
$request->Items->CalendarItem->Body->BodyType = 'Text';
$request->Items->CalendarItem->Body->_ = $body;
$response = $ews->CreateItem($request);
print_r($response);
?>
This works fine, the response outputted to the browser is like:
stdClass Object ( [ResponseMessages] => stdClass Object ( [CreateItemResponseMessage] => stdClass Object ( [ResponseCode] => NoError [ResponseClass] => Success [Items] => stdClass Object ( [CalendarItem] => stdClass Object ( [ItemId] => stdClass Object ( [Id] => AAASAHJydXVza2FAc3VicmFkLmNvbQBGAAAAAACyAesJgtiMR71+s/kuiAieBwBrnLmGR6RGTakOvWxNjsbfAAAAQwN1AABrnLmGR6RGTakOvWxNjsbfAABcYtppAAA= [ChangeKey] => DwAAABYAAABrnLmGR6RGTakOvWxNjsbfAABcYx9q ) ) ) ) ) )
The only piece I want is the 128 character string following [Id] =>
. If I convert this object to a string, I can parse the string like this:
$var = 'stdClass Object ( [ResponseMessages] => stdClass Object ( [CreateItemResponseMessage] => stdClass Object ( [ResponseCode] => NoError [ResponseClass] => Success [Items] => stdClass Object ( [CalendarItem] => stdClass Object ( [ItemId] => stdClass Object ( [Id] => AAASAHJydXVza2FAc3VicmFkLmNvbQBGAAAAAACyAesJgtiMR71+s/kuiAieBwBrnLmGR6RGTakOvWxNjsbfAAAAQwN1AABrnLmGR6RGTakOvWxNjsbfAABcYtpzAAA= [ChangeKey] => DwAAABYAAABrnLmGR6RGTakOvWxNjsbfAABcYzc4 ) ) ) ) ) )';
$regex_pattern = "/ /";
$matches = preg_split($regex_pattern,$var);
print_r($matches[36]);
However, I am having difficulty converting the object into a string. Also, if the response ever changes slightly I will not get the proper value returned. What is the best way to look at the Object, and return the value following [Id] =>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么你不能这样做:
优点是不需要将对象转换为字符串,只需以通常的方式获取所需的内容即可。另一个优点是,即使 M$ 更改了某些内容,返回 ID 的方式也应该保持不变以实现向后兼容性,因此这将起作用。
当您编辑日历项目时,响应几乎相同,唯一的区别是响应消息类型为 UpdateItemResponseMessage
Why can't you just do:
The advantage is that there's no need to cast objects to string, just get what you need the usual way. Another advantage is that even if M$ changes something, the way ID is returned should stay the same for backward compatibility, hence this will work.
When you edit calendar item the response is nearly the same, only difference is the response message type which is UpdateItemResponseMessage