webservice(二)简单实例

小编 2026-06-18 阅读:767 评论:0
1、建立WSDL文件     建立WSDL的工具很多,eclipse、zendstudio、vs...

1、建立WSDL文件
     建立WSDL的工具很多,eclipse、zendstudio、vs都可以,我个人建议自己写,熟悉结构,另外自动工具对xml schame类型支持在类型中可能会报错。
下面是我自己写的模板:

  1. <?xml version ='1.0' encoding ='UTF-8' ?>   
  2. <definitions name='自定义名称'   
  3.   targetNamespace='目标命名空间(WSDL所在地址)'   
  4.     <!--tns自定义目标空间,下面会用到-->  
  5.   xmlns:tns='目标命名空间(WSDL所在地址)'  
  6.   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'   
  7.   xmlns:xsd='http://www.w3.org/2001/XMLSchema'   
  8.   xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'   
  9.   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'   
  10.   xmlns='http://schemas.xmlsoap.org/wsdl/'>  
  11.    
  12. <!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,这里可以定义一些Schema不支持的类型-->  
  13.  <types>  
  14.     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  15.         targetNamespace="目标命名空间(WSDL所在地址)">   
  16.     </xsd:schema>  
  17.  </types>   
  18.   
  19. <!--  
  20. <message> 元素可定义每个消息的部件,以及相关联的数据类型。  
  21. 请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:  
  22. One-way 此操作可接受消息,但不会返回响应。  
  23. Request-response    此操作可接受一个请求并会返回一个响应。(常用)  
  24. Solicit-response    此操作可发送一个请求,并会等待一个响应。  
  25. Notification    此操作可发送一条消息,但不会等待响应。  
  26. -->    
  27. <message name='请求消息名称(方法名+Request)'>   
  28.     <part name="term" type="xsd:string"/>  
  29. </message>   
  30.   
  31. <message name='响应消息名称(方法名+Response)'>   
  32.     <part name="value" type="xsd:string"/>  
  33. </message>  
  34.   
  35. <!--  
  36. <portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。  
  37. 它告诉你去哪个WebService的连接点,扮演了一个控制者。  
  38. -->  
  39. <portType name='执行的操作名称(binding的type与其对应)'>   
  40.   <operation name='执行操作的方法'>   
  41.     <input message='tns:*Request'/>   
  42.     <output message='tns:*response'/>   
  43.   </operation>   
  44. </portType>  
  45.   
  46. <!--<binding> 元素为每个端口定义消息格式和协议细节-->  
  47. <binding name='Binding的名称,与service的port名称对应' type='指向用于Binding的端口(tns(前缀):PortType名称)'>   
  48. <!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP-->  
  49.   <soap:binding style='rpc'   
  50.     transport='http://schemas.xmlsoap.org/soap/http'/>   
  51.     <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->  
  52.   <operation name='GetCallDetailRecords'>   
  53.     <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>   
  54.     <input>   
  55.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  56.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  57.     </input>   
  58.     <output>   
  59.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  60.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  61.     </output>   
  62.   </operation>   
  63. </binding>  
  64.   
  65. <!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->  
  66. <service name='服务名称'>   
  67.   <port name='Binding名称' binding='tns:Binding名称'>   
  68.     <soap:address location='http://目标命名空间(WSDL所在地址)/service.php'/>   
  69.   </port>   
  70. </service>   
  71. </definitions>  


2、在service目录下建立myphone.wsdl

  1. <?xml version ='1.0' encoding ='UTF-8' ?>   
  2. <definitions name='phonebook'   
  3.   targetNamespace='http://www.mysoapservice.cn/'   
  4.   xmlns:tns='http://www.mysoapservice.cn/'  
  5.   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'   
  6.   xmlns:xsd='http://www.w3.org/2001/XMLSchema'   
  7.   xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'   
  8.   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'   
  9.   xmlns='http://schemas.xmlsoap.org/wsdl/'>  
  10.     
  11.  <types>  
  12.     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  13.         targetNamespace="http://www.mysoapservice.cn/">   
  14.     </xsd:schema>  
  15.  </types>   
  16.   
  17. <message name='GetPhoneBookRequest'>   
  18.     <part name="name" type="xsd:string"/>  
  19. </message>   
  20.   
  21. <message name='GetPhoneBookResponse'>   
  22.     <part name="phonebookInfo" type="xsd:string"/>  
  23. </message>  
  24.   
  25. <portType name='PhoneBookToEveryOneProt'>   
  26.   <operation name='GetPhoneBook'>   
  27.     <input message='tns:GetPhoneBookRequest'/>   
  28.     <output message='tns:GetPhoneBookResponse'/>   
  29.   </operation>  
  30. </portType>  
  31.   
  32. <binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'>   
  33.   <soap:binding style='rpc'   
  34.     transport='http://schemas.xmlsoap.org/soap/http'/>   
  35.   <operation name='GetPhoneBook'>   
  36.     <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>   
  37.     <input>   
  38.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  39.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  40.     </input>   
  41.     <output>   
  42.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  43.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  44.     </output>   
  45.   </operation>   
  46. </binding>  
  47.   
  48. <service name='PhoneBookService'>   
  49.   <port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'>   
  50.     <soap:address location='http://www.mysoapservice.cn/service.php'/>   
  51.   </port>   
  52. </service>   
  53. </definitions>  

3、修改service.php

  1. <?php  
  2. function GetPhoneBook($name){  
  3.     $pbook="Zhangsan,13888888888,friend,8888@163.com";  
  4.     return $pbook;  
  5. }  
  6. $server = new SoapServer("myphone.wsdl");  
  7. $server->addFunction("GetPhoneBook");  
  8. $server->handle ();   
  9. ?>  

4、修改client.php

  1. <?php  
  2. header('Content-Type:text/html;charset=utf-8');  
  3. $client = new SoapClient("http://www.mysoapservice.cn/service.php?WSDL" , array('trace'=>true));  
  4. var_dump($client->__getTypes());  
  5. try {  
  6.  $response = $client->GetPhoneBook('zhang');  
  7.  var_dump($response);  
  8. }catch (SoapFault $sf){  
  9.  var_dump($sf);  
  10.  print ($client->__getLastRequest());  
  11.  print ($client->__getLastResponse());  
  12. }  
  13. ?>  

测试:
http://www.mysoapclient.cn/client.php

webservice(二)简单实例

 

转载:http://blog.csdn.net/cwt0408/article/details/6952936

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表