草庐IT

PHP-EWS - 订阅/推送通知 - 完整示例

coder 2024-04-20 原文

经过令人沮丧的两天后,我放弃了。 我有一台带有 Win2012R2 + Exchange 2013 (Trailversion) 的虚拟机“admx”和 一台带有 IIS-Webserver+PHP 的虚拟机“webserver”。

创建一个新的订阅 - sub.php:

<?PHP
function __autoload($class_name)
{
    // Start from the base path and determine the location from the class name,
    $base_path = 'php-ews';
    $include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';
    //$include_file = str_replace('_', '/', $class_name) . '.php';

    return (file_exists($include_file) ? require_once $include_file : false);
}

$server = "admx";
$host = $server;
$username = "administrator@testdom.local";
$password = "secret123";
$version = "Exchange2013";

$url = "http://webserver/testexchange/log.php";
$keepAliveFrequency = 1;

$ews = new ExchangeWebServices($server, $username, $password, $version);
$subscribe_request = new EWSType_SubscribeType();
$pushSubscription = new EWSType_PushSubscriptionRequestType();
$pushSubscription->StatusFrequency = $keepAliveFrequency;
$pushSubscription->URL = $url;
$folderIDs = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$eventTypes = new EWSType_NonEmptyArrayOfNotificationEventTypesType();
$folderIDs->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$folderIDs->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
$eventTypes->EventType = "NewMailEvent";
$pushSubscription->FolderIds = $folderIDs;
$pushSubscription->EventTypes = $eventTypes;
$subscribe_request->PushSubscriptionRequest = $pushSubscription;
$response = $ews->Subscribe($subscribe_request);

var_dump($response);
?>

输出:

object(stdClass)#10 (1) { ["ResponseMessages"]=> object(stdClass)#11 (1) { ["SubscribeResponseMessage"]=> object(stdClass)#12 (4) { ["ResponseCode"]=> string(7) "NoError" ["ResponseClass"]=> string(7) "Success" ["SubscriptionId"]=> string(64) "EgBhZG14LnRlc3Rkb20ubG9jYWwQAAAA3keYE/U5Mkacz2FAg6DfHKdXyqelf9II" ["Watermark"]=> string(40) "AQAAAF57K7d1ihJLl9odwZ02gVahGgAAAAAAAAA=" } } } 

那么,订阅就注册成功了。 这是监听器 - log.php

<?PHP
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://schemas.microsoft.com/exchange/services/2006/messages\">
    <SOAP-ENV:Body>
        <ns1:SendNotificationResult>
            <ns1:SubscriptionStatus>OK</ns1:SubscriptionStatus>
        </ns1:SendNotificationResult>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>";

file_put_contents("C:\\exlog\\only_ok_".time().".txt", print_r($_REQUEST,1));
?>

现在我每分钟(因为 $keepAliveFrequency = 1;)都会收到来自 Exchange-Server 的新响应/StatusCheck。当我向 administrator@testdom.local 发送电子邮件时,我也会立即收到新通知(因为收件箱中有“NewMailEvent”)。 我用 OK 作为 XML 确认它,以保持订阅... 到目前为止一切顺利。

但是在日志文件(only_ok_1435488594.txt)中只有...

Array()

更新 log.php 以获取通知信息:

<?PHP
 class ewsService {
    public function SendNotification( $arg ) {
        file_put_contents("C:\\exlog\\logfile2_".time().".txt", print_r($arg,1));
        $result = new EWSType_SendNotificationResultType();
        $result->SubscriptionStatus = 'OK';
        //$result->SubscriptionStatus = 'Unsubscribe';
        return $result;
    }
}


$server = new SoapServer( 'php-ews/wsdl/NotificationService.wsdl', array(    'uri' => 'http://webserver/testexchange/log.php'));
$server->setObject( $service = new ewsService() );
$server->handle();
?>

现在日志文件的信息更多为“array()”:

stdClass Object
(
    [ResponseMessages] => stdClass Object
        (
            [SendNotificationResponseMessage] => stdClass Object
                (
                    [ResponseCode] => NoError
                    [ResponseClass] => Success
                    [Notification] => stdClass Object
                        (
                            [SubscriptionId] => EgBhZG14LnRlc3Rkb20ubG9jYWwQAAAAvfY+2ehqCEOXroWYNAsn+mD+ZBQYf9II
                            [PreviousWatermark] => AQAAAF57K7d1ihJLl9odwZ02gVZNGAAAAAAAAAA=
                            [MoreEvents] => 
                            [StatusEvent] => stdClass Object
                                (
                                    [Watermark] => AQAAAF57K7d1ihJLl9odwZ02gVZNGAAAAAAAAAA=
                                )

                        )

                )

        )
)

问题: 订阅在 3 个以上的通知丢失后得到。从我知道的 MSDN 文档中,Exchange 服务器重试状态消息 3 次以从监听器获得“OK”。

我已经从 GoogleSearch 下载了 NotificationService.wsdl。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 Microsoft Corporation. All rights reserved. -->
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:import namespace="http://schemas.microsoft.com/exchange/services/2006/messages" schemaLocation="messages.xsd"/>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="SendNotificationSoapIn">
        <wsdl:part name="request" element="tns:SendNotification" />
    </wsdl:message>
    <wsdl:message name="SendNotificationSoapOut">
        <wsdl:part name="SendNotificationResult" element="tns:SendNotificationResult" />
    </wsdl:message>
    <wsdl:portType name="NotificationServicePortType">
        <wsdl:operation name="SendNotification">
            <wsdl:input message="tns:SendNotificationSoapIn" />
            <wsdl:output message="tns:SendNotificationSoapOut" />
        </wsdl:operation>
    </wsdl:portType>


    <wsdl:binding name="NotificationServiceBinding" type="tns:NotificationServicePortType">
        <wsdl:documentation>
            <wsi:Claim conformsTo="http://ws-i.org/profiles/basic/1.0" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/" />
        </wsdl:documentation>
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />

        <wsdl:operation name="SendNotification">
            <soap:operation soapAction="http://schemas.microsoft.com/exchange/services/2006/messages/SendNotification" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>


    </wsdl:binding>

    <wsdl:binding name="NotificationServiceBinding12" type="tns:NotificationServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />

        <wsdl:operation name="SendNotification">
            <soap12:operation soapAction="http://schemas.microsoft.com/exchange/services/2006/messages/SendNotification" />
            <wsdl:input>
                <soap12:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal" />
            </wsdl:output>
        </wsdl:operation>

    </wsdl:binding>

        <wsdl:service name="NotificationServices">
    <wsdl:port name="NotificationServicePort" binding="tns:NotificationServiceBinding">
        <soap:address location="" />
    </wsdl:port>
</wsdl:service>


</wsdl:definitions>

我也添加了这个,因为没有它我会得到一个“绑定(bind)错误”

<wsdl:service name="NotificationServices">
    <wsdl:port name="NotificationServicePort" binding="tns:NotificationServiceBinding">
        <soap:address location="" />
    </wsdl:port>
</wsdl:service>

我不是 SOAP 专家...这里出了什么问题? 谢谢 奥利

最佳答案

这是一个完整的工作示例: https://github.com/jamesiarmes/php-ews/issues/280

关于PHP-EWS - 订阅/推送通知 - 完整示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099562/

有关PHP-EWS - 订阅/推送通知 - 完整示例的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby-on-rails - 如何在发布新的 Ruby 或 Rails 版本时收到通知? - 2

    有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:

  3. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  4. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  5. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  6. arrays - 如何在下面的示例中将两个值数组分组为 n 个值数组? - 2

    我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby​​库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_

  7. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  8. ruby - 是否有 SproutCore 或 Cappuccino 的现场演示/示例应用程序 - 2

    在他们的网站上找不到任何内容。我主要只是想看看哪个值得一试(当然是RIA)。谢谢 最佳答案 SproutCoredemos 关于ruby-是否有SproutCore或Cappuccino的现场演示/示例应用程序,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1419788/

  9. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  10. ruby - gem 推送结果为 "package metadata is missing" - 2

    我正在尝试将我更新的gem推送到ruby​​gems.com并得到以下结果。~/dev/V2/V2GPTI(master)$gembuildv2gpti.gemspecSuccessfullybuiltRubyGemName:v2gptiVersion:0.2File:v2gpti-0.2-universal-darwin-13.gem~/dev/V2/V2GPTI(master)$gempushv2gpti.gemspecERROR:Whileexecutinggem...(Gem::Package::FormatError)packagemetadataismissinginv2g

随机推荐