SOAP WSDL只能运行一个操作

时间:2014-05-13 21:03:37

标签: php web-services soap wsdl

我用PHP编写的soap客户端和soap服务器:

Server.php

// Initializing code is omitted

$server = new SoapServer('http://wsdl.localhost:8888/calculator_service.wsdl');
$server->setClass('CalculatorGateway');
$server->handle();

Client.php

// Initializing code is omitted

$request = new Request();
$request->data = new DataObject();
$request->data->sourceValue = 10;
$request->data->modifyBy = 5;

$client = new SoapClient(
    'http://wsdl.localhost:8888/calculator_service.wsdl',
    ['soap_version' => SOAP_1_2, 'classmap' => [
        'Response' => 'Response'
    ]]
);

// $argv[1] must be one of "add" or "sub"
$response = $client->$argv[1]($request);
echo 'Result: ' . $response->result;

WSDL

<definitions ...>
<types>
    <xs:schema elementFormDefault="qualified"
               xmlns:tns="http://schemas.xmlsoap.org/wsdl/"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://wsdl.localhost:8888/">

        <xs:complexType name="DataObject">
            <xs:sequence>
                <xs:element name="sourceValue" type="xs:double" minOccurs="1" maxOccurs="1"/>
                <xs:element name="modifyBy" type="xs:double" minOccurs="1" maxOccurs="1" />
            </xs:sequence>
        </xs:complexType>

        <xs:element name="Request">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="data" type="DataObject" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>

        <xs:element name="Response">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="result" type="xs:double" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
</types>

<message name="addRequest">
    <part name="Request" element="tns:Request" />
</message>
<message name="addResponse">
    <part name="Response" element="tns:Response" />
</message>

<message name="subRequest">
    <part name="Request" element="tns:Request" />
</message>
<message name="subResponse">
    <part name="Response" element="tns:Response" />
</message>

<portType name="CalculatorServicePortType">
    <operation name="add">
        <input message="tns:addRequest" />
        <output message="tns:addResponse" />
    </operation>
    <operation name="sub">
        <input message="tns:subRequest" />
        <output message="tns:subResponse" />
    </operation>
</portType>

<binding name="CalculatorServiceBinding" type="tns:CalculatorServicePortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="add">
        <soap:operation soapAction="" />
        <input>
            <soap:body use="literal" />
        </input>
        <output>
            <soap:body use="literal" />
        </output>
    </operation>
    <operation name="sub">
        <soap:operation soapAction="" />
        <input>
            <soap:body use="literal" />
        </input>
        <output>
            <soap:body use="literal" />
        </output>
    </operation>
</binding>

<service name="CalculatorService">
    <port name="CalculatorServicePort" binding="tns:CalculatorServiceBinding">
        <soap:address location="http://wsdl.localhost:8888/CalculatorService.php" />
    </port>
</service>
</definitions>

我的问题:无论我要求的操作是什么,总是第一个在绑定部分声明的操作将被执行。例如。在这种情况下,即使我要求&#34; sub&#34;,&#34;添加&#34;执行操作。作为一个实践,我可以改变这样的顺序:

<binding name="CalculatorServiceBinding" type="tns:CalculatorServicePortType">
    <operation name="sub">
    <!-- sub operation used to be on the second place -->
    </operation>

    <operation name="add">
    <!-- add operation used to be on the first place -->
    </operation>
</binding>

现在&#34; sub&#34;操作一直在执行。

有什么问题?我错了绑定部分吗?

1 个答案:

答案 0 :(得分:0)

解决方案非常简单。 样式属性必须存在于 soap:binding 元素中。像这样:

<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />