soapclient

SOAP是基于XML和HTTP通信协议,xml各种平台,各种语言都支持的一个种语言。http呢它得到了所有的因特网浏览器及服务器的支持。

WSDL 指网络服务描述语言 (Web Services Description Language),是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作。

我是做php的,你是java的,他是做.net,如果我们三个之间要进行通信,要进行数据交换,怎么办呢?我们需要一个能和我们都能通信的工具。soap,wsdl被创造出来,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。


如果php要使用soap的话,通常做法是,添加了一下php的soap模块,在php.ini里面加上soap.so,下面介绍一个不要添加soap.so文件,也可以实现soap的方法

nusoap是php写的一个功能文件,包涵进来就可以用了,网上很多自己去搜一下吧。

1,不使用wsd

a,服务端helloworld2.php

  1. <?php  
  2. //包函nusoap.php  
  3. require_once('./lib/nusoap.php');  
  4.   
  5. //创建服务端  
  6. $server = new soap_server;  
  7.   
  8. //定义客户端调用方法  
  9. $server->register('hello');  
  10.   
  11. //调用方法以及参数  
  12. function hello($name) {  
  13.  return 'Hello, ' . $name;  
  14. }  
  15.   
  16. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';  
  17. $server->service($HTTP_RAW_POST_DATA);  
  18. ?>  

b,客户端hello.php

  1. <?php  
  2. //包函nusoap.php  
  3. require_once('./lib/nusoap.php');  
  4. //新建一个soap客户端,调用服务端提供的wsdl  
  5. //$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);  
  6. $client = new soapclient('http://localhost/test/helloworld2.php');  
  7. //查看一下是不是报错  
  8. $err = $client->getError();  
  9. if ($err) {  
  10.  //显示错误  
  11.  echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';  
  12. }  
  13.   
  14. //调用服务端的方法  
  15. $result = $client->call('hello'array('person' => "this is a test"));  
  16.   
  17. echo '<h2>Result</h2><pre>';  
  18. print_r($result);  
  19. echo '</pre>';  
  20. ?>  

2,使用wsld

a,服务器端

  1. <?php  
  2. //包函nusoap.php  
  3. require_once('./lib/nusoap.php');  
  4. //新建一个soap服务  
  5. $server = new soap_server();  
  6. //初始化支持wsdl  
  7. $server->configureWSDL('hellowsdl2''urn:hellowsdl2');  
  8. //定义数据结构来接收数据  
  9. $server->wsdl->addComplexType(  
  10.  'Person',  
  11.  'complexType',  
  12.  'struct',  
  13.  'all',  
  14.  '',  
  15.  array(  
  16.  'firstname' => array('name' => 'firstname''type' => 'xsd:string'),//后面的type定义数据的类型,这个是string  
  17.  'age' => array('name' => 'age''type' => 'xsd:int'),//后面的type定义数据的类型,这个是int  
  18.  'gender' => array('name' => 'gender''type' => 'xsd:string')//后面的type定义数据的类型,这个是string  
  19.  )  
  20. );  
  21. $server->wsdl->addComplexType(  
  22.  'SweepstakesGreeting',  
  23.  'complexType',  
  24.  'struct',  
  25.  'all',  
  26.  '',  
  27.  array(  
  28.  'greeting' => array('name' => 'greeting''type' => 'xsd:string'),  
  29.  'winner' => array('name' => 'winner''type' => 'xsd:string')  
  30.  )  
  31. );  
  32. //服务器定义的soap调用方法  
  33. $server->register('hello',                    // 方法名字hello,方法就在下面  
  34.  array('person' => 'tns:Person'),          // 客户端传来的变量  
  35.  array('return' => 'tns:SweepstakesGreeting'),    //返回参数  
  36.  'urn:hellowsdl2',                         // soap名  
  37.  'urn:hellowsdl2#hello',                   // soap的方法名  
  38.  'rpc',                                    // 使用的方式  
  39.  'encoded',                                // 编码  
  40.  'test'                                    // 存档  
  41. );  
  42. //定义上面注册过的函数hello  
  43. function hello($person) {  
  44.  $greeting = 'Hello, ' . $person['firstname'] .  
  45.  '. It is nice to meet a ' . $person['age'] .  
  46.  ' year old ' . $person['gender'] . '.';  
  47.   
  48.  $winner =  'Scott';  
  49. //要返回的数据  
  50.  return array(  
  51.  'greeting' => $greeting,  
  52.  'winner' => $winner  
  53.  );  
  54. }  
  55. // 请求时(试图)调用服务  
  56. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';  
  57. $server->service($HTTP_RAW_POST_DATA);  
  58. ?>  

b,客户端

  1. <?php  
  2. //包函nusoap.php  
  3. require_once('./lib/nusoap.php');  
  4. //新建一个soap客户端,调用服务端提供的wsdl  
  5. //$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);  
  6. $client = new soapclient('http://localhost/test/helloworld2.php');  
  7. //查看一下是不是报错  
  8. $err = $client->getError();  
  9. if ($err) {  
  10.  //显示错误  
  11.  echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';  
  12. }  
  13. //要向服务端要传的参数  
  14. $person = array('firstname' => 'Willi''age' => 22, 'gender' => 'male');  
  15.   
  16. //调用服务端的方法  
  17. $result = $client->call('hello'array('person' => $person));  
  18. //错误审核  
  19. if ($client->fault) {  
  20.  echo '<h2>Fault</h2><pre>';  
  21.  print_r($result);  
  22.  echo '</pre>';  
  23. else {  
  24.  $err = $client->getError();  
  25.  if ($err) {  
  26.  echo '<h2>Error</h2><pre>' . $err . '</pre>';  
  27.  } else {  
  28.  echo '<h2>Result</h2><pre>';  
  29.  print_r($result);  
  30.  echo '</pre>';  
  31.  }  
  32. }  
  33. //显示请求信息  
  34. echo '<h2>Request</h2>';  
  35. echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';  
  36. //显示返回信息  
  37. echo '<h2>Response</h2>';  
  38. echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';  
  39. //显示调试信息  
  40. echo '<h2>Debug</h2>';  
  41. echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';  
  42. ?>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章