现在的位置: 首页 > 综合 > 正文

php webservice soap简单实例

2013年08月01日 ⁄ 综合 ⁄ 共 2081字 ⁄ 字号 评论关闭

接触PHP已经多年,可是对webService一直不太了解,最近公司要做要用PHP做一个接口,是在考虑用webService还是直接用HTTP请求,所以这两天也看了不少有关webService方面的资料

下面是一个小实例

action.php文件内容

 

  1. <?php
  2. //action.php
  3. Class Action {
  4. /**
  5. * Enter description here...
  6. *
  7. * @param int $num1
  8. * @param int $num2
  9. * @return int
  10. */
  11. public function total($num1,$num2){
  12. return $num1 + $num2;
  13. }
  14. }
  15. ?>

 

service.php文件内容

 

  1. <?php
  2. //service.php
  3. require './action.php';
  4. $server = new SoapServer('./action.wsdl');
  5. $server->setClass('Action');
  6. $server->handle();
  7. ?>

 

action.wsdl文件内容

 

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!-- WSDL file generated by Zend Studio. -->
  3. <definitions name="action" targetNamespace="urn:action" xmlns:typens="urn:action"
  4.     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5.     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  6.     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  7.     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  8.     xmlns="http://schemas.xmlsoap.org/wsdl/">
  9. <message name="total">
  10. <part name="num1" type="xsd:integer"/>
  11. <part name="num2" type="xsd:integer"/>
  12. </message>
  13. <message name="totalResponse">
  14. <part name="totalReturn" type="xsd:integer"/>
  15. </message>
  16. <portType name="ActionPortType">
  17. <operation name="total">
  18. <documentation>
  19. Enter description here...
  20. </documentation>
  21. <input message="typens:total"/>
  22. <output message="typens:totalResponse"/>
  23. </operation>
  24. </portType>
  25. <binding name="ActionBinding" type="typens:ActionPortType">
  26. <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  27. <operation name="total">
  28. <soap:operation soapAction="urn:ActionAction"/>
  29. <input>
  30. <soap:body namespace="urn:action" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  31. </input>
  32. <output>
  33. <soap:body namespace="urn:action" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  34. </output>
  35. </operation>
  36. </binding>
  37. <service name="actionService">
  38. <port name="ActionPort" binding="typens:ActionBinding">
  39. <soap:address location="http://127.0.0.1/bbs/soap2/service.php"/> <!-- 访问service的路径 -->
  40. </port>
  41. </service>
  42. </definitions>

 

index.php文件内容

 

  1. <?php
  2. //index.php
  3. $test= new SoapClient("http://127.0.0.1/soap2/action.wsdl");
  4. $result = $test->total(22,1);
  5. echo $result;
  6. ?>

 

其中index.php为调用接口的文件,service.php为接口的服务文件,运行index.php就可以直接得到接口返回的值

抱歉!评论已关闭.