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


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

[html] view plain copy
  1. xml version ='1.0' encoding ='UTF-8' ?>   
  2. <definitions name='自定义名称'   
  3.   targetNamespace='目标命名空间(WSDL所在地址)'   
  4.       
  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.   
  13.  <types>  
  14.     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  15.         targetNamespace="目标命名空间(WSDL所在地址)">   
  16.     xsd:schema>  
  17.  types>   
  18.   
  19.   
  20. <binding name='Binding的名称,与service的port名称对应' type='指向用于Binding的端口(tns(前缀):PortType名称)'>   
  21.   
  22.   <soap:binding style='rpc'   
  23.     transport='http://schemas.xmlsoap.org/soap/http'/>   
  24.       
  25.   <operation name='GetCallDetailRecords'>   
  26.     <soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>   
  27.     <input>   
  28.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  29.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  30.     input>   
  31.     <output>   
  32.       <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'   
  33.         encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>   
  34.     output>   
  35.   operation>   
  36. binding>  
  37.   
  38.   
  39. <service name='服务名称'>   
  40.   <port name='Binding名称' binding='tns:Binding名称'>   
  41.     <soap:address location='http://目标命名空间(WSDL所在地址)/service.php'/>   
  42.   port>   
  43. service>   
  44. definitions>  


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

[html] view plain copy
  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 plain copy
  1. php  
  2. function GetPhoneBook($name){  
  3.     $pbook="Zhangsan,13888888888,friend,[email protected]";  
  4.     return $pbook;  
  5. }  
  6. $server = new SoapServer("myphone.wsdl");  
  7. $server->addFunction("GetPhoneBook");  
  8. $server->handle ();   
  9. ?>  
4、修改client.php
[html] view plain copy
  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

你可能感兴趣的:(wsdl)