zoukankan      html  css  js  c++  java
  • 基于PHP——简单的WSDL的创建(WSDL篇)

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

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

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

    [html] view plaincopy
     
    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

    [html] view plaincopy
     
    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

    [html] view plaincopy
     
    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. ?>  

    详情请查看:

     基于PHP——简单的WSDL的创建(WSDL篇)

  • 相关阅读:
    记一次x87 FPU寄存器栈溢出
    从Unit Bezier的实现学习两种经典数值求解算法
    OI Memory 后记(1):IOI2021 漫谈
    Solution -「BZOJ #3786」星系探索
    Solution -「UOJ #46」玄学
    Solution -「多校联训」神
    Solution -「多校联训」自动机
    Solution -「多校联训」取石子游戏
    Solution -「HDU 6875」Yajilin
    Solution -「多校联训」最小点覆盖
  • 原文地址:https://www.cnblogs.com/bsyblog/p/6002051.html
Copyright © 2011-2022 走看看