Witam,

stworzyłem do celów testowych prosty web service "MathCalculator", który posiada dwie metody MathAdd i MathMulti (dodawanie i mnożenie). Metody przyjmują dwa parametry wejściowe NoA i NoB, wynik to MathResult. Poniżej plik WSDL serwisu:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://example.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" name="MathCalculator" targetNamespace="http://example.com/" xmlns="http://schemas.xmlsoap.org/wsdl/">
  3. <types>
  4. <xs:schema xmlns:tns="http://schemas.xmlsoap.org/wsdl/" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://example.com/">
  5. <element name="MathCalc">
  6. <complexType>
  7. <sequence>
  8. <element name="NoA" type="int" />
  9. <element name="NoB" type="int" />
  10. </sequence>
  11. </complexType>
  12. </element>
  13. <element name="MathCalcResponse">
  14. <complexType>
  15. <sequence>
  16. <element name="MathResult" type="int" />
  17. </sequence>
  18. </complexType>
  19. </element>
  20. <element name="string" type="string" />
  21. </xs:schema>
  22. </types>
  23. <message name="MathCalculatorSoapIn">
  24. <part name="parameters" element="tns:MathCalc" />
  25. </message>
  26. <message name="MathCalculatorSoapOut">
  27. <part name="parameters" element="tns:MathCalcResponse" />
  28. </message>
  29. <portType name="MathCalculatorSoap">
  30. <operation name="MathMulti">
  31. <input message="tns:MathCalculatorSoapIn" />
  32. <output message="tns:MathCalculatorSoapOut" />
  33. </operation>
  34. <operation name="MathAdd">
  35. <input message="tns:MathCalculatorSoapIn" />
  36. <output message="tns:MathCalculatorSoapOut" />
  37. </operation>
  38. </portType>
  39. <binding name="MathCalculatorSoap" type="tns:MathCalculatorSoap">
  40. <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
  41. <operation name="MathAdd">
  42. <soap12:operation soapAction="MathAdd" />
  43. <input>
  44. <soap12:body use="literal" />
  45. </input>
  46. <output>
  47. <soap12:body use="literal" />
  48. </output>
  49. </operation>
  50. <operation name="MathMulti">
  51. <soap12:operation soapAction="MathMulti" />
  52. <input>
  53. <soap12:body use="literal" />
  54. </input>
  55. <output>
  56. <soap12:body use="literal" />
  57. </output>
  58. </operation>
  59. </binding>
  60. <service name="MathCalculator">
  61. <port name="MathCalculatorSoap" binding="tns:MathCalculatorSoap">
  62. <soap12:address location="http://localhost/math/MathCalculator.php" />
  63. </port>
  64. </service>
  65. </definitions>



Web service wygląda następująco (plik MathCalculator.php):


  1. <?php
  2.  
  3. define("WSDL_PATH","MathCalcService.wsdl");
  4.  
  5.  
  6. if (isset($_GET['WSDL'])) {
  7. $wsdl_file=implode("",file(WSDL_PATH));
  8. header('Content-Type: text/xml');
  9. echo $wsdl_file;
  10. }
  11. else {
  12. $server = new SoapServer(WSDL_PATH);
  13.  
  14.  
  15. function MathMulti($MathCalc) {
  16. $rr=$MathCalc->NoA * $MathCalc->NoB;
  17. return array("MathResult"=>$rr);
  18. }
  19. function MathAdd($MathCalc) {
  20. $rr=$MathCalc->NoA + $MathCalc->NoB;
  21. return array("MathResult"=>$rr);
  22. }
  23.  
  24. $server->AddFunction(array("MathMulti","MathAdd"));
  25. $server->handle();
  26. }
  27.  
  28. ?>



Klient testujący web service (odałem wywołanie __getFunctions() i __getTypes() żeby sprawdzić so się dzieje):


  1. <?php
  2.  
  3. $client = new SoapClient("http://localhost/math/MathCalculator.php?WSDL");
  4. var_dump($client->__getFunctions());
  5. var_dump($client->__getTypes());
  6.  
  7. $params = array(
  8. "NoA" => 5,
  9. "NoB" => 5
  10. );
  11.  
  12. $response = $client->__soapCall("MathAdd", array($params));
  13. echo '<p>Result : ' . $params['NoA'] . '+' . $params['NoB'] . ' = ' . $response->MathResult . '</p>';
  14.  
  15. $response = $client->__soapCall("MathMulti", array($params));
  16. echo '<p>Result : ' . $params['NoA'] . '*' . $params['NoB'] . ' = ' . $response->MathResult . '</p>';
  17. ?>



Wynik wywołania klienta w przeglądarce:


Cytat
array (size=2)
0 => string 'MathCalcResponse MathAdd(MathCalc $parameters)' (length=46)
1 => string 'MathCalcResponse MathMulti(MathCalc $parameters)' (length=48)
array (size=2)
0 => string 'struct MathCalc {
int NoA;
int NoB;
}' (length=39)
1 => string 'struct MathCalcResponse {
int MathResult;
}' (length=44)
Result : 5+5 = 10

Result : 5*5 = 10


Jak widać serwis wykonuje tylko jedną metodę, w tym przypadku MathAdd, ponieważ jest ona pierwsza w definicji WSDL. Jeśli zmienię kolejność deklaracji metod w pliku WSDL, serwer będzie zawsze wykonywał tylko mnożenie. Czy mogę prosić o pomoc w zdiagnozowaniu problemu?

Z góry dziękuję smile.gif