我用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;
<<p> WSDL/strong>: <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>
我的问题:无论我请求什么操作,总是第一个,它在绑定部分声明,将被执行。例如,在这种情况下,即使我请求"sub",也会执行"add"操作。作为实验,我可以像这样改变顺序:
<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>
现在"sub"操作一直在执行。
有什么问题吗?装订部分我说错了吗?
解决方法很简单。Style属性必须出现在soap:binding元素中。这样的:
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />