PHP SoapServer:如何禁用“响应”追加

时间:2011-06-03 18:31:52

标签: php xml soap wsdl soapserver

我正在创建一个SOAP服务器(在Symfony中使用ckWebservicePlugin),它需要遵循以下请求和响应结构(我已经将其作为已经商定的规范)。

请求:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.net/schemas/USSD">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <USSDMessageRequest>
      <id>43</id>
      <msid>1234567890</msid>
      <data>1*2</data>
    </USSDMessageRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>  

的回复:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <USSDMessageResponse>
       <id>43</id>
       <msid>1234567890</msid>
       <data>
          RESPONSE DATA
       </data>
    </USSDMessageResponse>
</SOAP-ENV:Body>

我遇到的麻烦是SoapServer总是生成响应作为所请求方法的名称+'Response'附加。

因此发送的响应XML 如下所示:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://bw.petr.appsdev/bw/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>
      <ns1:USSDMessageRequestResponse>
         <result>
            <id>2</id>
            <msid>1234565789</msid>
            <data>RESPONSE DATA</data>
         </result>
      </ns1:USSDMessageRequestResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我希望输出为<ns1:USSDMessageResponse>而不是<ns1:USSDMessageRequestResponse>

我使用的WSDL 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="webservices" targetNamespace="http://bw.petr.appsdev/bw/" xmlns:tns="http://bw.petr.appsdev/bw/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
  <wsdl:types xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://bw.petr.appsdev/bw/">
      <xsd:complexType name="userCredentials">
        <xsd:sequence>
          <xsd:element name="msisdn" type="xsd:string"/>
          <xsd:element name="status" type="xsd:string"/>
        </xsd:sequence>
      </xsd:complexType>
      <xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="userCredentialsElement" type="tns:userCredentials"/>
      <xsd:complexType name="USSDResponse">
        <xsd:sequence>
          <xsd:element name="id" type="xsd:string"/>
          <xsd:element name="msid" type="xsd:string"/>
          <xsd:element name="data" type="xsd:string"/>
        </xsd:sequence>
      </xsd:complexType>
      <xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="USSDResponseElement" type="tns:USSDResponse"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:portType name="webservicesPortType">
    <wsdl:operation name="USSDMessageRequest" parameterOrder="userCredentials id msid data">
      <wsdl:input message="tns:USSDMessageRequestRequest"/>
      <wsdl:output message="tns:USSDMessageResponse" name="USSDMessageResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="webservicesBinding" type="tns:webservicesPortType">
    <soap:binding xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="USSDMessageRequest">
      <soap:operation xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" soapAction="http://bw.petr.appsdev/bw/USSDMessageRequest" style="rpc"/>
      <wsdl:input xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
        <soap:body xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" parts="id msid data" use="literal" namespace="http://bw.petr.appsdev/bw/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
        <soap:header xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" message="tns:USSDMessageRequestRequest" part="userCredentials" use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:input>
      <wsdl:output xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
        <soap:body xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" parts="result" use="literal" namespace="http://bw.petr.appsdev/bw/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
        <soap:header xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" message="tns:USSDMessageResponse" part="userCredentials" use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:message name="USSDMessageRequestRequest">
    <wsdl:part name="userCredentials" element="tns:userCredentialsElement"/>
    <wsdl:part name="id" type="xsd:string"/>
    <wsdl:part name="msid" type="xsd:string"/>
    <wsdl:part name="data" type="xsd:string"/>
  </wsdl:message>
  <wsdl:message name="USSDMessageResponse">
    <wsdl:part name="userCredentials" element="tns:userCredentialsElement"/>
    <wsdl:part name="result" type="tns:USSDResponse"/>
  </wsdl:message>
  <wsdl:service xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="webservicesService">
    <wsdl:port xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="webservicesPort" binding="tns:webservicesBinding">
      <soap:address xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" location="http://bw.petr.appsdev/bw/webservices.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

已经检查了SoapServer文档,浏览了R. Richards的Pro Xml和Web服务,并且无法弄清楚如何告诉SoapServer响应名称应该是什么。

谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个脏的SoapVar黑客:

public function USSDMessageRequest() {
        $result = '<ns1:USSDMessageResponse> ... </ns1:USSDMessageResponse>'; // ns1 is dirty, depends on the SoapServer implementation
        $result = new SoapVar($result, XSD_ANYXML);
        return $result;
    }

它在类似的问题上对我有用(但我没有用你的网络服务测试过它)。

答案 1 :(得分:1)

您是否在WSDL模式下创建SoapServer?我的意思是你像这样构建SoapServer:

new SoapServer('path/to/wsdl', $options); 

第一个参数不能为null。然后SoapServer应该根据WSDL创建响应。如果您遇到麻烦,请使用最新版本的PHP进行尝试。