Heim  >  Artikel  >  Backend-Entwicklung  >  webservice——nusoap详解

webservice——nusoap详解

WBOY
WBOYOriginal
2016-07-30 13:30:121453Durchsuche

PHP SOAP服务器

用PHP和NuSoap来建立SOAP服务器非常容易。基本上,你只要写出你想要暴露给你的Web services的函数,然后用NuSoap去注册它们就可以了。OK,另外还需要两步才能完成PHP SOAP服务器的建立。首先你还要在你的PHP代码中创建NuSoap对象的一个实例,然后用HTTP POST方法将原始数据传给NuSoap进行处理

NuSOAP的使用比较简单,其中最常用到的类是soap_server和soapclient,其中soap_server用于创建 Webservice服务,类soapclient则用于调用Webservice.这两个类的定义都在lib/nusoap.php中,因此我们在创建 或调用Webservice接口程序时均需要引用该文件.  

NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/  )开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响。  

1.首先,去http://sourceforge.net/projects/nusoap/下载nusoap.zip
2.
服务端:建立nusoapService.php文件。

[php] view plaincopy

  1. require_once ("lib/nusoap.php");  
  2. $server = new soap_server ();  
  3. // 避免乱码  
  4. $server->soap_defencoding = 'UTF-8';  
  5. $server->decode_utf8 = false;  
  6. $server->xml_encoding = 'UTF-8';  
  7. $server->configureWSDL ('test'); // 打开 wsdl 支持  
  8. /* 
  9. 注册需要被客户端访问的程序 
  10. 类型对应值: bool->"xsd:boolean"    string->"xsd:string" 
  11. int->"xsd:int"     float->"xsd:float" 
  12. */  
  13. $server->register ( 'GetTestStr'// 方法名  
  14. array ("name" => "xsd:string" ), // 参数,默认为 "xsd:string"  
  15. array ("return" => "xsd:string" ) ); // 返回值,默认为 "xsd:string"  
  16. //isset  检测变量是否设置  
  17. $HTTP_RAW_POST_DATA = isset ( $HTTP_RAW_POST_DATA ) ? $HTTP_RAW_POST_DATA : '';  
  18. //service  处理客户端输入的数据  
  19. $server->service ( $HTTP_RAW_POST_DATA );  
  20. /** 
  21.  * 供调用的方法 
  22.  * @param $name 
  23.  */  
  24. function GetTestStr($name) {  
  25.     return "Hello,  { $name } !";  
  26. }  
  27. ?>  


3.客户端:建立nusoapClient.php文件。


[php] view plaincopy

  1. require_once ("lib/nusoap.php");  
  2. /* 
  3. 通过 WSDL 调用 WebService 
  4. 参数 1 WSDL 文件的地址 (问号后的wsdl不能为大写) 
  5. 参数 2  指定是否使用 WSDL 
  6. $client = new soapclient('http://localhost/nusoapService.php?wsdl',true); 
  7. */  
  8. $client = new soapclient ( 'http://localhost/nusoapService.php' );  
  9. $client->soap_defencoding = 'UTF-8';  
  10. $client->decode_utf8 = false;  
  11. $client->xml_encoding = 'UTF-8';  
  12. // 参数转为数组形式传递  
  13. $paras = array ('name' => 'Bruce Lee' );  
  14. // 目标方法没有参数时,可省略后面的参数  
  15. $result = $client->call ( 'GetTestStr'$paras );  
  16. // 检查错误,获取返回值  
  17. if (! $err = $client->getError ()) {  
  18.     echo " 返回结果: "$result;  
  19. else {  
  20.     echo " 调用出错: "$err;  
  21. }  
  22. ?>  

[php] view plaincopy

  1. require_once ("lib/nusoap.php");  
  2. /* 
  3. 通过 WSDL 调用 WebService 
  4. 参数 1 WSDL 文件的地址 ( 问号后的 wsdl 不能为大写 ) 
  5. 参数 2  指定是否使用 WSDL 
  6. $client = new soapclient('http://localhost/nusoapService.php?wsdl',true); 
  7. */  
  8. $client = new soapclient ( 'http://localhost/nusoapService.php?wsdl',true);  
  9. $client->soap_defencoding = 'UTF-8';  
  10. $client->decode_utf8 = false;  
  11. $client->xml_encoding = 'UTF-8';  
  12. // 参数转为数组形式传递  
  13. $paras = array ('name' => 'Bruce Lee' );  
  14. // 目标方法没有参数时,可省略后面的参数  
  15. $client->call ( 'GetTestStr'$paras );  
  16. $document = $client->document;  
  17. echo $document;   
  18. ?>  

注:返回结果: Hello, { Bruce Lee } !

WSDL
WSDL是一种用于描述Web Service的XML语言。它是一种机读格式,把所有的访问服务所必须的信息提供给Web Service客户端。NuSOAP专门提供一个类进行WDSL文件的解析,并且从中提取信息。soapclient对象使用wsdl类来减轻开发者调用服务的难度。通过WSDL信息的帮助来创建报文,程序员仅仅需要知道操作的名字和参数就能调用它。
通过NuSOAP使用WSDL提供以下几点优点:
所有的服务元文件,如命名空间(namespaces),endpoint URLs,参数名(parameter names)等等都可以直接从WSDL文件获得,这样就允许客户端动态的适应服务器端的变化。因为从服务器随时可以获得,所以这些数据不再需要在用户脚本中使用硬性编码。
它允许我们使用soap_proxy类。这个类派生自soapclient类,增加了WDSL文件中详细列出的操作所对应的方法。现在用户通过它可以直接调用这些方法。
soapclient 类包含一个getProxy()方 法,它返回一个soap_proxy类的一个对象。soap_proxy类派生自soapclient类,增加了对应于 WSDL文档中定义的操作的方法, 并且允许用户调用一个endpoint的远程方法。这仅仅适用于soapclient对象用WDSL文件初始化的情况。优点是易于用户使用,缺点是性能--PHP中创建对象是耗时的--且不为功利目的服务 (and this functionality serves no utilitarian purpose)。


[php] view plaincopy

  1. require_once ("lib/nusoap.php");  
  2. $client = new soapclient ( 'http://localhost/nusoapService.php?wsdl',true);  
  3. $client->soap_defencoding = 'UTF-8';  
  4. $client->decode_utf8 = false;  
  5. $client->xml_encoding = 'UTF-8';  
  6. //生成proxy类    
  7. $proxy = $client->getProxy();    
  8. //调用远程函数    
  9. $sq = $proxy->GetTestStr('Bruce Lee');   
  10. if (!$err=$proxy->getError()) {    
  11. print_r($sq);    
  12. else {    
  13. print "ERROR: $err";    
  14. }    
  15.     
  16. print 'REQUEST:<xmp>'.$p->request.'</xmp>';    
  17. print 'RESPONSE:<xmp>'.str_replace('>">\n$p->response).'</xmp>';    
  18. ?>    

第四步:运行server端文件页面:http://localhost/nusoapService.php生成的wsdl文件

点击方法名称。这样我们通过在service中增加了几行代码我们就通过使用NuSOAP为service提供了一个可视化的文档。但是,这还不是所有我们能做的。

webservice——nusoap详解

我们在service中通过使用NuSOAP增加一些WSDL的调用我们可以为service生成WSDL还有一些其他的文档。与此不同的是,在 client中我们能做的就有些少了,至少在我们的这个简单的例子中是这样。下面所示的这个client跟没有使用WSDL的client没有什么不同 的,唯一的不同就是解析soapclent class是通过提供WSDL的URL来完成的,而不是之前的通过service endpoint。

NuSoap调用WebService出现乱码的解决方法:

[php] view plaincopy

  1. $client->soap_defencoding = 'utf-8';    
  2. $client->decode_utf8 = false;    
  3. $client->xml_encoding = 'utf-8';    

文件代码不能有任何输出,否则调用时会报类似如下错误: 
XML error parsing SOAP payload on line x(
行号): Reserved XML Name

nusoap实现WebService,不要开启phpSOAP扩展,原因是nusoap的SoapClient类和php5内置的SOAP类有冲突。

解决方案

1. 修改php.ini不加载php5内置的soap扩展(windows下是php_soap.dll)。

2. 也有给nusoap的SoapClient类改名的。

身份认证

[php] view plaincopy

  1. header('content-type: text/xml; charset=UTF-8');  
  2. require_once('nusoap.php');  
  3. $params = array('AuthenticationHeader' => array(  
  4.     'Content-Type' => 'text/xml; charset=UTF-8',  
  5.     'SOAPAction' => 'YourFunstion',  
  6. )  
  7. );  
  8. $client = new nusoap_client('http://www.yourdomain.com/service.asmx?wsdl', true, '''''''');  
  9. $client->setHeaders('    
  10. "http://tempuri.org/webservice">  
  11.   username   
  12.   password     
  13.    
  14. ');  
  15. $err = $client->getError();  
  16. if ($err) {  
  17.     echo '

    Constructor error

    '
     . $err . '';  
  18. }  
  19. $result = $client->call('YourFunction'$params'''', false, true);  
  20. if ($client->fault) {  
  21.     echo '

    Fault

    '
    ;  
  22.     print_r($result);  
  23.     echo '';  
  24. else {  
  25.     $err = $client->getError();  
  26.     if ($err) {  
  27.         echo '

    Error

    '
     . $err . '';  
  28.     } else {  
  29.         echo '

    Result

    '
    ;  
  30. //print_r($result);  
  31.         echo '';  
  32.     }  
  33. }  
  34. echo '

    Request

    '
     . htmlspecialchars($client->request, ENT_QUOTES) . '';  
  35. echo '

    Response

    '
     . htmlspecialchars($client->response, ENT_QUOTES) . '';  
  36. ?> 

以上就介绍了webservice——nusoap详解,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn