---
title: "基于PHP——简单的WSDL的创建(WSDL篇) "
author: "sun123start"
date: "2014-12-19T09:53:09Z"
tags: []
categories: ["编程语言"]
slug: "php-wsdl-wsdl"
draft: false
lastmod: "2014-12-19T09:53:09Z"
id: 2168375
url: "https://sun123start.iteye.com/blog/2168375"
---

原文：[基于PHP——简单的WSDL的创建(WSDL篇)](http://blog.csdn.net/rrr4578/article/details/24451943)

 

 

# [1、建立WSDL文件 建立WSDL的工具很多，eclipse、zendstudio、vs都可以，我个人建议自己写，熟悉结构，另外自动工具对xml schame类型支持在类型中可能会报错。 下面是我自己写的模板：](http://blog.csdn.net/cwt0408/article/details/6954897)

**\[html]** [view plain](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "view plain")[copy](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "copy")

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

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

**\[html]** [view plain](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "view plain")[copy](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "copy")

01. &lt;?xml version ='1.0' encoding ='UTF-8' ?&gt;
02. &lt;definitions name='phonebook'
03. targetNamespace='http://www.mysoapservice.cn/'
04. xmlns:tns='http://www.mysoapservice.cn/'
05. xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
06. xmlns:xsd='http://www.w3.org/2001/XMLSchema'
07. xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
08. xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
09. xmlns='http://schemas.xmlsoap.org/wsdl/'&gt;
10. &lt;types&gt;
11. &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
12. targetNamespace="http://www.mysoapservice.cn/"&gt;
13. &lt;/xsd:schema&gt;
14. &lt;/types&gt;
15. &lt;message name='GetPhoneBookRequest'&gt;
16. &lt;part name="name" type="xsd:string"/&gt;
17. &lt;/message&gt;
18. &lt;message name='GetPhoneBookResponse'&gt;
19. &lt;part name="phonebookInfo" type="xsd:string"/&gt;
20. &lt;/message&gt;
21. &lt;portType name='PhoneBookToEveryOneProt'&gt;
22. &lt;operation name='GetPhoneBook'&gt;
23. &lt;input message='tns:GetPhoneBookRequest'/&gt;
24. &lt;output message='tns:GetPhoneBookResponse'/&gt;
25. &lt;/operation&gt;
26. &lt;/portType&gt;
27. &lt;binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'&gt;
28. &lt;soap:binding style='rpc'
29. transport='http://schemas.xmlsoap.org/soap/http'/&gt;
30. &lt;operation name='GetPhoneBook'&gt;
31. &lt;soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/&gt;
32. &lt;input&gt;
33. &lt;soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
34. encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/&gt;
35. &lt;/input&gt;
36. &lt;output&gt;
37. &lt;soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
38. encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/&gt;
39. &lt;/output&gt;
40. &lt;/operation&gt;
41. &lt;/binding&gt;
42. &lt;service name='PhoneBookService'&gt;
43. &lt;port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'&gt;
44. &lt;soap:address location='http://www.mysoapservice.cn/service.php'/&gt;
45. &lt;/port&gt;
46. &lt;/service&gt;
47. &lt;/definitions&gt;

# 3、修改service.php

**\[html]** [view plain](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "view plain")[copy](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "copy")

1. &lt;?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-&gt;addFunction("GetPhoneBook");
8. $server-&gt;handle ();
9. ?&gt;

# 4、修改client.php

**\[html]** [view plain](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "view plain")[copy](http://blog.csdn.net/cwt0408/article/details/6954897?userName=rrr4578&userInfo=m9vInujG62WB7BRZYBfNdKGdUBZNV7mGfR1C6IzOWH8sSY0DKU2uRDtNusJjMiSuRNZrl79fCvMbopUrZ18KImf%2F1ySBNHOk83pfIOOBCHOAg5LUdIueAmQwpzXqWRBd "copy")

01. &lt;?php
02. header('Content-Type:text/html;charset=utf-8');
03. $client = new SoapClient("http://www.mysoapservice.cn/service.php?WSDL" , array('trace'=&gt;true));
04. var\_dump($client-&gt;\_\_getTypes());
05. try {
06. $response = $client-&gt;GetPhoneBook('zhang');
07. var\_dump($response);
08. }catch (SoapFault $sf){
09. var\_dump($sf);
10. print ($client-&gt;\_\_getLastRequest());
11. print ($client-&gt;\_\_getLastResponse());
12. }
13. ?&gt;

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