设为首页 收藏本站
查看: 783|回复: 0

[经验分享] 基于PHP——简单的WSDL的创建(WSDL篇)

[复制链接]

尚未签到

发表于 2017-4-8 13:07:02 | 显示全部楼层 |阅读模式
  原文:基于PHP——简单的WSDL的创建(WSDL篇)

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

[html]  view plaincopy


  • <?xml version ='1.0' encoding ='UTF-8' ?>   
  • <definitions name='自定义名称'   
  •   targetNamespace='目标命名空间(WSDL所在地址)'   
  •     <!--tns自定义目标空间,下面会用到-->  
  •   xmlns:tns='目标命名空间(WSDL所在地址)'  
  •   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'   
  •   xmlns:xsd='http://www.w3.org/2001/XMLSchema'   
  •   xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'   
  •   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'   
  •   xmlns='http://schemas.xmlsoap.org/wsdl/'>  
  •    
  • <!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,这里可以定义一些Schema不支持的类型-->  
  •  <types>  
  •     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  •         targetNamespace="目标命名空间(WSDL所在地址)">   
  •     </xsd:schema>  
  •  </types>   
  •   
  • <!--  
  • <message> 元素可定义每个消息的部件,以及相关联的数据类型。  
  • 请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:  
  • One-way 此操作可接受消息,但不会返回响应。  
  • Request-response    此操作可接受一个请求并会返回一个响应。(常用)  
  • Solicit-response    此操作可发送一个请求,并会等待一个响应。  
  • Notification    此操作可发送一条消息,但不会等待响应。  
  • -->    
  • <message name='请求消息名称(方法名+Request)'>   
  •     <part name="term" type="xsd:string"/>  
  • </message>   
  •   
  • <message name='响应消息名称(方法名+Response)'>   
  •     <part name="value" type="xsd:string"/>  
  • </message>  
  •   
  • <!--  
  • <portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。  
  • 它告诉你去哪个WebService的连接点,扮演了一个控制者。  
  • -->  
  • <portType name='执行的操作名称(binding的type与其对应)'>   
  •   <operation name='执行操作的方法'>   
  •     <input message='tns:*Request'/>   
  •     <output message='tns:*response'/>   
  •   </operation>   
  • </portType>  
  •   
  • <!--<binding> 元素为每个端口定义消息格式和协议细节-->  
  • <binding name='Binding的名称,与service的port名称对应' type='指向用于Binding的端口(tns(前缀):PortType名称)'>   
  • <!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP-->  
  •   <soap:binding style='rpc'   
  •     transport='http://schemas.xmlsoap.org/soap/http'/>   
  •     <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->  
  •   <operation name='GetCallDetailRecords'>   
  •     <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>   
  •     <input>   
  •       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  •         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  •     </input>   
  •     <output>   
  •       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  •         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  •     </output>   
  •   </operation>   
  • </binding>  
  •   
  • <!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->  
  • <service name='服务名称'>   
  •   <port name='Binding名称' binding='tns:Binding名称'>   
  •     <soap:address location='http://目标命名空间(WSDL所在地址)/service.php'/>   
  •   </port>   
  • </service>   
  • </definitions>  

  
2、在service目录下建立myphone.wsdl
[html]  view plaincopy


  • <?xml version ='1.0' encoding ='UTF-8' ?>   
  • <definitions name='phonebook'   
  •   targetNamespace='http://www.mysoapservice.cn/'   
  •   xmlns:tns='http://www.mysoapservice.cn/'  
  •   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'   
  •   xmlns:xsd='http://www.w3.org/2001/XMLSchema'   
  •   xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'   
  •   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'   
  •   xmlns='http://schemas.xmlsoap.org/wsdl/'>  
  •     
  •  <types>  
  •     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  •         targetNamespace="http://www.mysoapservice.cn/">   
  •     </xsd:schema>  
  •  </types>   
  •   
  • <message name='GetPhoneBookRequest'>   
  •     <part name="name" type="xsd:string"/>  
  • </message>   
  •   
  • <message name='GetPhoneBookResponse'>   
  •     <part name="phonebookInfo" type="xsd:string"/>  
  • </message>  
  •   
  • <portType name='PhoneBookToEveryOneProt'>   
  •   <operation name='GetPhoneBook'>   
  •     <input message='tns:GetPhoneBookRequest'/>   
  •     <output message='tns:GetPhoneBookResponse'/>   
  •   </operation>  
  • </portType>  
  •   
  • <binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'>   
  •   <soap:binding style='rpc'   
  •     transport='http://schemas.xmlsoap.org/soap/http'/>   
  •   <operation name='GetPhoneBook'>   
  •     <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>   
  •     <input>   
  •       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  •         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  •     </input>   
  •     <output>   
  •       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  •         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  •     </output>   
  •   </operation>   
  • </binding>  
  •   
  • <service name='PhoneBookService'>   
  •   <port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'>   
  •     <soap:address location='http://www.mysoapservice.cn/service.php'/>   
  •   </port>   
  • </service>   
  • </definitions>  

3、修改service.php
[html]  view plaincopy


  • <?php  
  • function GetPhoneBook($name){  
  •     $pbook="Zhangsan,13888888888,friend,8888@163.com";  
  •     return $pbook;  
  • }  
  • $server = new SoapServer("myphone.wsdl");  
  • $server->addFunction("GetPhoneBook");  
  • $server->handle ();   
  • ?>  

4、修改client.php
[html]  view plaincopy


  • <?php  
  • header('Content-Type:text/html;charset=utf-8');  
  • $client = new SoapClient("http://www.mysoapservice.cn/service.php?WSDL" , array('trace'=>true));  
  • var_dump($client->__getTypes());  
  • try {  
  •  $response = $client->GetPhoneBook('zhang');  
  •  var_dump($response);  
  • }catch (SoapFault $sf){  
  •  var_dump($sf);  
  •  print ($client->__getLastRequest());  
  •  print ($client->__getLastResponse());  
  • }  
  • ?>  

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362003-1-1.html 上篇帖子: [转]解决PHP截取中文字符串问题 下篇帖子: PHP类实例教程(八):类的继承
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表