PHP SoapServer formatting


PHP SoapServer formatting

我是SOAP的新手,正在尝试使用PHP的SoapServer类格式化SOAP响应。

知道这是针对EWS推送订阅回调服务的,并且我正在使用php-ews库可能会很有用。我已经成功订阅了EWS推送订阅,但是在格式化EWS期望从我的回调服务收到的回复时遇到了问题。

我需要PHP SOAP服务返回以下SOAP响应:

<s:Envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <SendNotificationResult xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
            <SubscriptionStatus>OK</SubscriptionStatus>
        </SendNotificationResult>
    </s:Body>
</s:Envelope>
相反,我得到了以下输出:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/ioc/ebooking_v4/services/ews/notification.php" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:SendNotificationResponse>
            <return xsi:type="SOAP-ENC:Struct">
                <SubscriptionStatus xsi:type="xsd:string">OK</SubscriptionStatus>
            </return>
        </ns1:SendNotificationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我的PHP服务代码:
class ewsService {
    public function SendNotification( $arg ) {
        $result = new EWSType_SendNotificationResultType();
        $result->SubscriptionStatus = 'OK';
        return $result;
    }
}
$server = new soapServer( null, array(
    'uri' => $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],
));
$server->setObject( new ewsService() );
$server->handle();

对我服务的SOAP调用如下:

<soap11:Envelope xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/">
    <soap11:Header>
        <t:RequestServerVersion xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" Version="Exchange2010_SP2" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" />
    </soap11:Header>
    <soap11:Body>
        <m:SendNotification xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
            <m:ResponseMessages>
                <m:SendNotificationResponseMessage ResponseClass="Success">
                    <m:ResponseCode>NoError</m:ResponseCode>
                    <m:Notification>
                        <t:SubscriptionId>GQBjaGNpb2ExODEuY2lvLm9seW1waWMub3JnEAAAAFgvEpJcS3JDkpvHOMgnX60FhmhpPInRCA==</t:SubscriptionId>
                        <t:PreviousWatermark>AQAAAIiCiXu/q1ZPvYPyvRVVh5cajSoKAAAAAAA=</t:PreviousWatermark>
                        <t:MoreEvents>false</t:MoreEvents>
                        <t:StatusEvent>
                            <t:Watermark>AQAAAIiCiXu/q1ZPvYPyvRVVh5cajSoKAAAAAAA=</t:Watermark>
                        </t:StatusEvent>
                    </m:Notification>
                </m:SendNotificationResponseMessage>
            </m:ResponseMessages>
        </m:SendNotification>
    </soap11:Body>
</soap11:Envelope>

所以我想我的问题是我如何改变SOAP响应的格式,使用PHP SoapServer类?我是否需要添加WSDL或将类映射传递给SoapServer构造函数?

如果使用SoapServer类没有办法做到这一点,除了创建字符串缓冲区并手动传递之外,还有其他方法可以做到这一点吗?

我发现了这个问题,并在这里发布了一个更普遍的问题的解决方案

为了完整,我在这里也包含了答案

看起来我很接近,但需要包括NotificationService。实例化SoapServer类时使用。然后WSDL允许SoapServer类相应地格式化响应。

$server = new SoapServer( PHPEWS_PATH.'/wsdl/NotificationService.wsdl', array(

WSDL没有包含在php-ews库下载中,但是它包含在Exchange Server安装中。如果您像我一样没有访问Exchange Server安装的权限,您可以在这里找到该文件。我还必须在WSDL的末尾添加以下内容,因为我将WSDL存储在本地并且没有使用自动发现:

<wsdl:service name="NotificationServices">
    <wsdl:port name="NotificationServicePort" binding="tns:NotificationServiceBinding">
        <soap:address location="" />
    </wsdl:port>
</wsdl:service>
所以完整的PHP代码如下:
class ewsService {
    public function SendNotification( $arg ) {
        $result = new EWSType_SendNotificationResultType();
        $result->SubscriptionStatus = 'OK';
        //$result->SubscriptionStatus = 'Unsubscribe';
        return $result;
  }
}
$server = new SoapServer( PHPEWS_PATH.'/wsdl/NotificationService.wsdl', array(
    'uri' => $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],
));
$server->setObject( $service = new ewsService() );
$server->handle();

输出如下:

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

希望这对其他人有帮助,因为这花了我一段时间才弄清楚!