(三)基于PHP——复杂的WSDL的创建(WSDL篇)




版权声明:本文为博主原创文章,未经博主允许不得转载。



(谢谢合作!)
调用数组内容:
1、修改service.php


1. <?php  
2. class PhoneBookInfo{  
3.     public $name,$number,$relationship,$email;  
4.     public function __construct($pname,$pnumber,$prelationship,$pemail){  
5. >name=$pname;  
6. >number=$pnumber;  
7. >relationship=$prelationship;  
8. >email=$pemail;  
9.     }     
10. }  
11. /*可以在这里调用数据库,下面是测试数据*/  
12. function GetPhoneBook($inname)  
13. {  
14. pinfo=array();  
15.     $pinfo[]=new PhoneBookInfo(  
16.         'zhangsan',  
17.         '13333333333',  
18.         'friend',  
19.         '3333@163.com'  
20.     );  
21.       
22.     $pinfo[]=new PhoneBookInfo(  
23.       'lisi',  
24.       '13444444444',  
25.       'friend',  
26.       '4444@163.com'  
27.      );  
28.     
29.   $pinfo[]=new PhoneBookInfo(  
30.       'wangwu',  
31.       '135555555555',  
32.       'friend',  
33.       '5555@163.com'  
34.      );     
35.     return $pinfo;  
36. }  
37.   
38. $server = new SoapServer("myphone.wsdl");  
39. $server->addFunction("GetPhoneBook");  
40. $server->handle ();   
41. ?>


2、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. >__getLastRequest());  
11. >__getLastResponse());  
12. }  
13. ?>

3、关键修改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. xmlns:Q1="soapenc">   
15. <xsd:complexType name="PhoneBookInfoArray">  
16. <xsd:complexContent>  
17. <xsd:restriction base="soapenc:Array">  
18. <xsd:attribute ref="sopenc:arrayType" wsdl:arrayType="tns:PhoneBookInfo[]"/>  
19. </xsd:restriction>  
20. </xsd:complexContent>   
21. </xsd:complexType>  
22.           
23. <xsd:complexType name="PhoneBookInfo">  
24. <xsd:all>  
25. <xsd:element name="name" type="xsd:string"></xsd:element>  
26. <xsd:element name="number" type="xsd:string"></xsd:element>  
27. <xsd:element name="relationship" type="xsd:string"></xsd:element>  
28. <xsd:element name="email" type="xsd:string"></xsd:element>  
29. </xsd:all>  
30. </xsd:complexType>  
31. </xsd:schema>  
32. </types>   
33.   
34. <message name='GetPhoneBookRequest'>   
35. <part name="inname" type="xsd:string"/>  
36. </message>   
37.   
38. <message name='GetPhoneBookResponse'>   
39. <part name="phonebookInfo" type="xsd:PhoneBookInfoArray"/>  
40. </message>  
41.   
42. <portType name='PhoneBookToEveryOneProt'>   
43. <operation name='GetPhoneBook'>   
44. <input message='tns:GetPhoneBookRequest'/>   
45. <output message='tns:GetPhoneBookResponse'/>   
46. </operation>  
47. </portType>  
48.   
49. <binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'>   
50. <soap:binding style='rpc'   
51. transport='http://schemas.xmlsoap.org/soap/http'/>   
52. <operation name='GetPhoneBook'>   
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 name='PhoneBookService'>   
66. <port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'>   
67. <soap:address location='http://www.mysoapservice.cn/service.php'/>   
68. </port>   
69. </service>   
70. </definitions>

对比修改地方:


测试结果:


另外需要注意的地方:

如果遇到I/O错误:

1、在php.ini中打开extension=php_curl.dll

2、copy: libeay32.dll、ssleay32.dll到system32下

这两个dll网上都有下载!我就不传了!


(谢谢合作!)
调用数组内容:
1、修改service.php


    1. <?php  
    2. class PhoneBookInfo{  
    3.     public $name,$number,$relationship,$email;  
    4.     public function __construct($pname,$pnumber,$prelationship,$pemail){  
    5. >name=$pname;  
    6. >number=$pnumber;  
    7. >relationship=$prelationship;  
    8. >email=$pemail;  
    9.     }     
    10. }  
    11. /*可以在这里调用数据库,下面是测试数据*/  
    12. function GetPhoneBook($inname)  
    13. {  
    14. pinfo=array();  
    15.     $pinfo[]=new PhoneBookInfo(  
    16.         'zhangsan',  
    17.         '13333333333',  
    18.         'friend',  
    19.         '3333@163.com'  
    20.     );  
    21.       
    22.     $pinfo[]=new PhoneBookInfo(  
    23.       'lisi',  
    24.       '13444444444',  
    25.       'friend',  
    26.       '4444@163.com'  
    27.      );  
    28.     
    29.   $pinfo[]=new PhoneBookInfo(  
    30.       'wangwu',  
    31.       '135555555555',  
    32.       'friend',  
    33.       '5555@163.com'  
    34.      );     
    35.     return $pinfo;  
    36. }  
    37.   
    38. $server = new SoapServer("myphone.wsdl");  
    39. $server->addFunction("GetPhoneBook");  
    40. $server->handle ();   
    41. ?>


    2、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. >__getLastRequest());  
    11. >__getLastResponse());  
    12. }  
    13. ?>

    3、关键修改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. xmlns:Q1="soapenc">   
    15. <xsd:complexType name="PhoneBookInfoArray">  
    16. <xsd:complexContent>  
    17. <xsd:restriction base="soapenc:Array">  
    18. <xsd:attribute ref="sopenc:arrayType" wsdl:arrayType="tns:PhoneBookInfo[]"/>  
    19. </xsd:restriction>  
    20. </xsd:complexContent>   
    21. </xsd:complexType>  
    22.           
    23. <xsd:complexType name="PhoneBookInfo">  
    24. <xsd:all>  
    25. <xsd:element name="name" type="xsd:string"></xsd:element>  
    26. <xsd:element name="number" type="xsd:string"></xsd:element>  
    27. <xsd:element name="relationship" type="xsd:string"></xsd:element>  
    28. <xsd:element name="email" type="xsd:string"></xsd:element>  
    29. </xsd:all>  
    30. </xsd:complexType>  
    31. </xsd:schema>  
    32. </types>   
    33.   
    34. <message name='GetPhoneBookRequest'>   
    35. <part name="inname" type="xsd:string"/>  
    36. </message>   
    37.   
    38. <message name='GetPhoneBookResponse'>   
    39. <part name="phonebookInfo" type="xsd:PhoneBookInfoArray"/>  
    40. </message>  
    41.   
    42. <portType name='PhoneBookToEveryOneProt'>   
    43. <operation name='GetPhoneBook'>   
    44. <input message='tns:GetPhoneBookRequest'/>   
    45. <output message='tns:GetPhoneBookResponse'/>   
    46. </operation>  
    47. </portType>  
    48.   
    49. <binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'>   
    50. <soap:binding style='rpc'   
    51. transport='http://schemas.xmlsoap.org/soap/http'/>   
    52. <operation name='GetPhoneBook'>   
    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 name='PhoneBookService'>   
    66. <port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'>   
    67. <soap:address location='http://www.mysoapservice.cn/service.php'/>   
    68. </port>   
    69. </service>   
    70. </definitions>

    对比修改地方:


    测试结果:


    另外需要注意的地方:

    如果遇到I/O错误:

    1、在php.ini中打开extension=php_curl.dll

    2、copy: libeay32.dll、ssleay32.dll到system32下

    这两个dll网上都有下载!我就不传了!