<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>soap &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/soap/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 13 Jan 2014 02:27:50 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>续PHP下webSerives的nusoap使用</title>
		<link>https://www.mudbest.com/%e7%bb%adphp%e4%b8%8bwebserives%e7%9a%84nusoap%e4%bd%bf%e7%94%a8/</link>
					<comments>https://www.mudbest.com/%e7%bb%adphp%e4%b8%8bwebserives%e7%9a%84nusoap%e4%bd%bf%e7%94%a8/#comments</comments>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Wed, 20 Nov 2013 07:03:01 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[nusoap]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[webservices]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1908</guid>

					<description><![CDATA[呼&#8230;才发现很久没发博客了，真的实在是太忙了。平时还要抽点时间维护http://bbs.mudbes ... <a title="续PHP下webSerives的nusoap使用" class="read-more" href="https://www.mudbest.com/%e7%bb%adphp%e4%b8%8bwebserives%e7%9a%84nusoap%e4%bd%bf%e7%94%a8/" aria-label="阅读 续PHP下webSerives的nusoap使用">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>呼&#8230;才发现很久没发博客了，真的实在是太忙了。平时还要抽点时间维护http://bbs.mudbest.com 。</p>
<p>平时很少写webService，一般都是需要给第三方提供接口的时候才去写。偶然的一个机会，打开了项目里现有的webService文件，发现前人所写的webService都是以简单的创建一个类，再将类里面的函数暴露给第三方调用，连WSDL都不支持，这就不是规范的webService接口定义了。</p>
<p>开发人员应该都知道，传统的webService是有wsdl支持的，就是介绍了，传入和返回，以及webService的一些基本结构介绍信息，这能很大限度的增加接口可读性和参考性。<strong>希望所有开发者能够自我养成良好的编码习惯，开发者最忌讳的就是怕麻烦，请尊重自己的职业。</strong><span id="more-1908"></span></p>
<p>服务端demo:<br />
<a href="http://help.mudbest.com/php/demo_nusoap/webService/RefundService.php" target="__blank">http://help.mudbest.com/php/demo_nusoap/webService/RefundService.php</a></p>
<p><a href="http://help.mudbest.com/php/demo_nusoap/webService/RefundService.php?wsdl" target="__blank">http://help.mudbest.com/php/demo_nusoap/webService/RefundService.php?wsdl</a></p>
<p>下面nusoap的服务端(webService)和客户端(client)介绍一下简单实例。</p>
<p>nusoap支持类库下载地址：http://www.mudbest.com/webservicephp%E7%9A%84soap%E4%B8%8Enusoap%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%B8%8E%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84%E9%80%9A%E4%BF%A1/nusoap-0-9-5/</p>
<p>1、首先创建一个服务端：</p>
<p>这里用的是注册类，而不是注册函数，建议创建webService的时候将服务结构整理清晰。</p>
<p>class.refundStatusService.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * 退票状态回显服务类库
 * @author hkshadow
 */
class refundStatusService{
	private $objDb = null;
	
	public function __construct(){
		
	}
	
	public function connect(){
		
	}
	
	public function hello($person, $interface){
		$greeting = &#039;Hello111, &#039; . $person &amp;#91;&#039;firstname&#039;&amp;#93; . &#039;. It is nice to meet a &#039; . $person &amp;#91;&#039;age&#039;&amp;#93; . &#039; year old &#039; . $person &amp;#91;&#039;gender&#039;&amp;#93; . &#039;.&#039; . $interface &amp;#91;&#039;type&#039;&amp;#93;;
		$winner = &#039;Scott&#039;;
		//要返回的数据
		return array (&#039;greeting&#039; =&gt; $greeting, &#039;winner&#039; =&gt; $winner );
	}
}
?&gt;
</pre>
<p>建立服务（例如地址http://www.mudbest.com/webService.php）</p>
<pre class="brush: php; title: ; notranslate">
require_once(&#039;comm/nusoap/nusoap.php&#039;);
require_once(&#039;webservice/RefundStatusService.php&#039;);
// 新建一个soap服务
$server = new soap_server ();
// 初始化支持wsdl
$server-&gt;configureWSDL ( &#039;hellowsdl2&#039;, &#039;urn:hellowsdl2&#039; );
//设置返回时的编码
$server-&gt;soap_defencoding = &#039;UTF-8&#039;;
//是否utf8_decode()
$server-&gt;decode_utf8 = false;
//设置传入的编码
$server-&gt;xml_encoding = &#039;UTF-8&#039;;
// 定义接收的数据结构
$server-&gt;wsdl-&gt;addComplexType ( &#039;Person&#039;, &#039;complexType&#039;, &#039;struct&#039;, &#039;all&#039;, &#039;&#039;, 
		//array (&#039;type&#039; =&gt; &#039;1==2&#039; );
		array (&#039;type&#039; =&gt; array (&#039;name&#039; =&gt; &#039;type&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) ),	// 后面的type定义数据的类型，这个是string
		array (&#039;firstname&#039; =&gt; array (&#039;name&#039; =&gt; &#039;firstname&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ), 	// 后面的type定义数据的类型，这个是string
				&#039;age&#039; =&gt; array (&#039;name&#039; =&gt; &#039;age&#039;, &#039;type&#039; =&gt; &#039;xsd:int&#039; ), 	// 后面的type定义数据的类型，这个是int
				&#039;gender&#039; =&gt; array (&#039;name&#039; =&gt; &#039;gender&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) )	// 后面的type定义数据的类型，这个是string
);

//定义返回数据结构
$server-&gt;wsdl-&gt;addComplexType ( &#039;SweepstakesGreeting&#039;, &#039;complexType&#039;, &#039;struct&#039;, &#039;all&#039;, &#039;&#039;, array (&#039;greeting&#039; =&gt; array (&#039;name&#039; =&gt; &#039;greeting&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ), &#039;winner&#039; =&gt; array (&#039;name&#039; =&gt; &#039;winner&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) ) );


/**
备注：
array (&#039;person&#039; =&gt; &#039;tns:Person&#039; )  这里声明是根据addComplexType (&#039;Person&#039;)方法定义属性，如果只需要传入字符串，则直接xsd:string即可
// 定义接收的数据结构
$server-&gt;wsdl-&gt;addComplexType ( &#039;Person&#039;, &#039;complexType&#039;, &#039;struct&#039;, &#039;all&#039;, &#039;&#039;, 
		//array (&#039;type&#039; =&gt; &#039;1==2&#039; );
		array (&#039;type&#039; =&gt; array (&#039;name&#039; =&gt; &#039;type&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) ),	// 后面的type定义数据的类型，这个是string
		array (&#039;firstname&#039; =&gt; array (&#039;name&#039; =&gt; &#039;firstname&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ), 	// 后面的type定义数据的类型，这个是string
				&#039;age&#039; =&gt; array (&#039;name&#039; =&gt; &#039;age&#039;, &#039;type&#039; =&gt; &#039;xsd:int&#039; ), 	// 后面的type定义数据的类型，这个是int
				&#039;gender&#039; =&gt; array (&#039;name&#039; =&gt; &#039;gender&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) )	// 后面的type定义数据的类型，这个是string
);
-----------------------------------------------------
array (&#039;return&#039; =&gt; &#039;tns:SweepstakesGreeting&#039; )  这里声明是根据addComplexType (&#039;SweepstakesGreeting&#039;)方法定义属性，如果只需要返回字符串，则直接xsd:string即可
//定义返回数据结构
$server-&gt;wsdl-&gt;addComplexType ( &#039;SweepstakesGreeting&#039;, &#039;complexType&#039;, &#039;struct&#039;, &#039;all&#039;, &#039;&#039;, array (&#039;greeting&#039; =&gt; array (&#039;name&#039; =&gt; &#039;greeting&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ), &#039;winner&#039; =&gt; array (&#039;name&#039; =&gt; &#039;winner&#039;, &#039;type&#039; =&gt; &#039;xsd:string&#039; ) ) );

*/
// 服务器定义的soap调用方法
$server-&gt;register ( &#039;refundStatusService.hello&#039;, 	// 这里注册的是类，类文件class.refundStatusService.php
		array (&#039;person&#039; =&gt; &#039;tns:Person&#039; ), 	// 客户端传来的变量
		array (&#039;return&#039; =&gt; &#039;tns:SweepstakesGreeting&#039; ), 	// 返回参数
		&#039;urn:hellowsdl2&#039;, 	// soap名
		&#039;urn:hellowsdl2#hello&#039;, 	// soap的方法名
		&#039;rpc&#039;, 	// 使用的方式
		&#039;encoded&#039;, 	// 编码
		&#039;test&#039;// 存档
		);	
		
		//请求时(试图)调用服务
		//$HTTP_RAW_POST_DATA = isset ( $HTTP_RAW_POST_DATA ) ? $HTTP_RAW_POST_DATA : &#039;&#039;;
		$HTTP_RAW_POST_DATA = isset($GLOBALS&#x5B;&#039;HTTP_RAW_POST_DATA&#039;]) ? $GLOBALS&#x5B;&#039;HTTP_RAW_POST_DATA&#039;] : &#039;&#039;;
		//处理请求并且返回响应结果(processes request and returns response)
		$server-&gt;service ( $HTTP_RAW_POST_DATA );
</pre>
<p>至此服务端已经创建完成，接下来要做的便是客户端(client)了。</p>
<p>demo.php</p>
<pre class="brush: php; title: ; notranslate">

// 包函nusoap.php
require_once(comm/nusoap/nusoap.php&#039;);
// 新建一个soap客户端，调用服务端提供的wsdl
// $client = new soapclient(&#039;http://www.mudbest.com/webService.php?wsdl&#039;, true);
$client = new nusoap_client ( &#039;http://www.mudbest.com/webService.php&#039; );

// 查看一下是不是报错
$err = $client-&gt;getError ();
if ($err) {
	// 显示错误
	echo &#039;&lt;h2&gt;Constructor error&lt;/h2&gt;&lt;pre&gt;&#039; . $err . &#039;&lt;/pre&gt;&#039;;
}
// 要向服务端要传的参数
$interface = array (&#039;type&#039; =&gt; &#039;1==2&#039; );
$person = array (&#039;firstname&#039; =&gt; &#039;Willi&#039;, &#039;age&#039; =&gt; 22, &#039;gender&#039; =&gt; &#039;male&#039; );

// 调用服务端的方法(这里注意call的名字与regisert名一样)
$result = $client-&gt;call ( &#039;refundStatusService.hello&#039;, array (&#039;userInfo&#039; =&gt; $person, &#039;interfaceType&#039; =&gt; $interface ) );

//WSDL调用服务端的方法(这里注意call的名字与regisert名一样)
//$result = $client-&gt;call ( &#039;refundStatusService.hello&#039;);
// 错误审核
if ($client-&gt;fault) {
	echo &#039;&lt;h2&gt;Fault&lt;/h2&gt;&lt;pre&gt;&#039;;
	print_r ( $result );
	echo &#039;&lt;/pre&gt;&#039;;
} else {
	$err = $client-&gt;getError ();
	if ($err) {
		echo &#039;&lt;h2&gt;Error&lt;/h2&gt;&lt;pre&gt;&#039; . $err . &#039;&lt;/pre&gt;&#039;;
	} else {
		echo &#039;&lt;h2&gt;Result&lt;/h2&gt;&lt;pre&gt;&#039;;
		print_r ( $result );
		echo &#039;&lt;/pre&gt;&#039;;
	}
}
// 显示请求信息
echo &#039;&lt;h2&gt;Request&lt;/h2&gt;&#039;;
echo &#039;&lt;pre&gt;&#039; . htmlspecialchars ( $client-&gt;request, ENT_QUOTES ) . &#039;&lt;/pre&gt;&#039;;
// 显示返回信息
echo &#039;&lt;h2&gt;Response&lt;/h2&gt;&#039;;
echo &#039;&lt;pre&gt;&#039; . htmlspecialchars ( $client-&gt;response, ENT_QUOTES ) . &#039;&lt;/pre&gt;&#039;;
// 显示调试信息
echo &#039;&lt;h2&gt;Debug&lt;/h2&gt;&#039;;
echo &#039;&lt;pre&gt;&#039; . htmlspecialchars ( $client-&gt;debug_str, ENT_QUOTES ) . &#039;&lt;/pre&gt;&#039;;
</pre>
<p>到这里，服务端以及客户端已经创建完成。</p>
<p>当在浏览器上浏览http://www.mudbest.com/webService.php时会出现结构图。<br />
<a href="http://www.mudbest.com/%e7%bb%adphp%e4%b8%8bwebserives%e7%9a%84nusoap%e4%bd%bf%e7%94%a8/soapwebservicesdemo1/" rel="attachment wp-att-1916"><img fetchpriority="high" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2013/11/soapWebServicesDemo1-540x370.jpg" alt="soapWebServicesDemo1" width="540" height="370" class="alignnone size-thumbnail wp-image-1916" /></a></p>
<p>当在浏览器上浏览http://www.mudbest.com/webService.php?wsdl时会出现xml格式。<br />
<a href="http://www.mudbest.com/wp-content/uploads/2013/11/soapWebServicesDemo2.jpg"><img decoding="async" src="http://www.mudbest.com/wp-content/uploads/2013/11/soapWebServicesDemo2-540x370.jpg" alt="soapWebServicesDemo2" width="540" height="370" class="alignnone size-thumbnail wp-image-1910" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mudbest.com/%e7%bb%adphp%e4%b8%8bwebserives%e7%9a%84nusoap%e4%bd%bf%e7%94%a8/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>webService(php)的soap与nusoap服务端与客户端的通信</title>
		<link>https://www.mudbest.com/webservicephp%e7%9a%84soap%e4%b8%8enusoap%e6%9c%8d%e5%8a%a1%e7%ab%af%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e9%80%9a%e4%bf%a1/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Tue, 26 Mar 2013 06:30:12 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[nusoap]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[webservice]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1827</guid>

					<description><![CDATA[PHP SOAP服务器 用PHP和Soap来建立SOAP服务器非常容易。基本上，你只要写出你想要暴露给你的We ... <a title="webService(php)的soap与nusoap服务端与客户端的通信" class="read-more" href="https://www.mudbest.com/webservicephp%e7%9a%84soap%e4%b8%8enusoap%e6%9c%8d%e5%8a%a1%e7%ab%af%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e9%80%9a%e4%bf%a1/" aria-label="阅读 webService(php)的soap与nusoap服务端与客户端的通信">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p><strong>PHP SOAP服务器</strong></p>
<p>用PHP和Soap来建立SOAP服务器非常容易。基本上，你只要写出你想要暴露给你的Web service的函数，然后用Soap去注册它们就可以了。另外还需要两步才能完成PHP SOAP服务器的建立。首先你还要在你的PHP代码中创建Soap对象的一个实例，然后用HTTP POST方法将原始数据传给Soap进行处理</p>
<p>在php5开始集成soap了，SOAP的使用比较简单，其中最常用到的类是SoapServer和SoapClient, 其中SoapServer用于创建Webservice服务端，类SoapClient则用于调用Webservice，也就是客户端。由于soap从php5便集成，只需要在php.ini里开启soap组件即可。<br />
<span id="more-1827"></span><br />
以windows下为例：</p>
<pre class="brush: plain; title: ; notranslate">
extension=php_soap.dll
</pre>
<p>Soap实例：</p>
<p>1、首先创建服务端，由于集成后的php5不需要引入任何soap支持文件，直接创建即可，例如访问地址设置为http://127.0.0.1/test.php。</p>
<pre class="brush: php; title: ; notranslate">
class test{
	function demo(){
		return 'hello word';
	}
}
//开始创建webservice
//null可传入ip，第二个参数类似于授权标识，调用时会用到
$webService = new SoapServer(null,array('uri'=&gt;'test.php'));
//设置需要提供的类，setClass不难理解吧？
$webService-&gt;setClass('test');
$webService-&gt;handle();
</pre>
<p>至此，soap服务端创建完成。</p>
<p>2、调用刚刚创建的soap服务，同理，由于集成后的php5不需要引入任何soap支持文件，直接调用即可。</p>
<pre class="brush: php; title: ; notranslate">
$client = new SoapClient(null,array(
	&quot;location&quot; =&gt; 'http://127.0.0.1/test.php',
	&quot;uri&quot;      =&gt; 'test.php',  //请求标识，服务器和客户端必须对应
));
//至此，便可调用类里面的方法了
$demo = $client-&gt;demo();
var_dump($demo);
</pre>
<p>然而对于Nusoap来说，灵活性强于soap，它们的操作过程基本一致，只是处理过程有少许的偏差，NuSOAP的使用也比较简单,其中最常用到的类是soap_server和nusoap_client, 其中soap_server用于创建 Webservice服务端，类nusoap_client则用于调用Webservice，也就是客户端。这两个类的定义都在lib/nusoap.php中,因此我们在创建 或调用Webservice接口程序时均需要引用该文件。</p>
<p>NuSoap是PHP环境下的WebService编程工具，用于创建或调用WebService。它是一个开源软件，是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类，由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/  )开发。NuSOAP的一个优势是不需要扩展库的支持，这种特性使得NuSoap可以用于所有的PHP环境，不受服务器安全设置的影响。　　</p>
<p><strong>提供nusoap下载</strong>：<a href="http://www.mudbest.com/webservicephp%e7%9a%84soap%e4%b8%8enusoap%e6%9c%8d%e5%8a%a1%e7%ab%af%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e9%80%9a%e4%bf%a1/nusoap-0-9-5/" rel="attachment wp-att-1828">nusoap-0.9.5</a></p>
<p>Nusoap的实例：</p>
<p>1、首先要做的依然是创建服务端，刚刚说到了有少许的偏差，就在服务端创建这了，首先引入nusoap的支持类库</p>
<pre class="brush: php; title: ; notranslate">
//引入nusoap支持类库
require_once('lib/nusoap.php');
$soap = new soap_server();
$soap-&gt;configureWSDL('test');
//这里要说明一下register这个函数，第一个参数是需要调用的方法，第二个参数是传入的数据，第三个参数是传出的数据。
$soap-&gt;register('GetTestStr',
		array (&quot;name&quot; =&gt; &quot;xsd:string&quot; ), // 参数，默认为 &quot;xsd:string&quot;
		array (&quot;return&quot; =&gt; &quot;xsd:string&quot; )
		);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soap-&gt;service($HTTP_RAW_POST_DATA);
</pre>
<p>至此，nusoap的服务端已经创建好，假设地址为http://127.0.0.1/test.php。</p>
<p>2、调用nusoap。在这之前我在这里封装了一下，文件为class.nuSoapApi.php，代码如下。</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * nusoap扩展类
 * Email:zhangyuan#tieyou.com
 * @author hkshadow
 */
//引入nusoap基类
require_once('pto/nusoap/nusoap_new/lib/nusoap.php');

class nuSoapApi extends nusoap_client{
	private $_strSoapUrl = '';//wsdl url
 	private $_strSoapDefenCoding = 'utf-8'; // 当前请求的HTTP内容类型的字符集。默认utf-8
 	private $_strXmlEnCoding = 'utf-8';//传入消息的字符集编码（响应）。默认utf-8
 	private $_arrParam = array(); //arrData
 	private $_objSoap = null; //初始化Nusoap对象
 	
 	/**
 	 * 构造函数
 	 * @param str $_strSoapUrl
 	 * @param 可选，默认为wsdl $_strSoapWsdl
 	 */
 	public function __construct($_strSoapUrl,$_strSoapWsdl = true){
 		if($this-&gt;_objSoap === null){
 			$this-&gt;_objSoap = new nusoap_client($_strSoapUrl,$_strSoapWsdl);
 		}
 	}
 	
 	/**
 	 * 设置消息数据
 	 * @param array $arrData
 	 */
 	public function setArrParam($arrData){
 		$this-&gt;_arrParam = $arrData;
 	}
 	
 	/**
 	 * 设置xml编码
 	 * @param true / false $bool
 	 */
 	public function setDeCodeUtf8($bool = false){
 		$this-&gt;_objSoap-&gt;decode_utf8 = $bool;
 	}
 	
 	/**
 	 * 设置http内容类型的字符编码
 	 * @param str $strCode
 	 */
 	public function setSoapDefenCoding($strCode){
 		if(!empty($strCode)){
 			$this-&gt;_objSoap-&gt;soap_defencoding = $strCode;
 		}else{
 			$this-&gt;_objSoap-&gt;soap_defencoding = $this-&gt;_strSoapDefenCoding;
 		}
 	}

 	public function setXmlEnCoding($strCode){
 		if(!empty($strCode)){
 			$this-&gt;_objSoap-&gt;xml_encoding = $strCode;
 		}else{
 			$this-&gt;_objSoap-&gt;xml_encoding = $this-&gt;_strXmlEnCoding;
 		}
 	}
 	
 	/**
 	 * 获取数据
 	 */
 	public function getRequestData($fun){
 		$arrData = array();
 		$arrData = $this-&gt;_objSoap-&gt;call($fun,$this-&gt;_arrParam);
 		return $arrData;
 	}

 	/**
 	 *  数组转对象
 	 * @param array $arrData
 	 */
 	public function arrDataObj($arrData){
 		//引用地址，而非引用拷贝
 		$objStdClass = new stdClass();
 		foreach ($arrData as $key=&gt;$value){
 			if(is_array($value)){
 				$objStdClass-&gt;$key = $this-&gt;arrDataObj($value);
 			}else{
 				$objStdClass-&gt;$key = $value;
 			}
 		}
 		return $objStdClass;
 	} 
}
?&gt;
</pre>
<p>从这里开始调用执行过程：</p>
<pre class="brush: php; title: ; notranslate">
//定义webservice的请求url
define('SERVICEDTS_WEBSERVER_URL', 'http://127.0.0.1/test.php?wsdl');
//引入刚刚封装的类
require_once('lib/class.nuSoapApi.php');
//调用nusoap扩展类
$client = new NusoapApi(SERVICEDTS_WEBSERVER_URL,true);
$client-&gt;setSoapDefenCoding('utf-8');
$client-&gt;setDeCodeUtf8(false);
$client-&gt;setXmlEnCoding('utf-8');
$paras = array ('name' =&gt; 'hkshadow' );
$client-&gt;setArrParam($paras);
$result = $client-&gt;getRequestData('GetTestStr');

if (! $err = $client-&gt;getError ()) {
	echo &quot; 返回结果： &quot;, $result;
} else {
	echo &quot; 调用出错： &quot;, $err;
}
//输出Hello, { hkshadow } !
</pre>
<p><strong>解说：</strong></p>
<p>WSDL<br />
WSDL是一种用于描述Web Service的XML语言。它是一种机读格式，把所有的访问服务所必须的信息提供给Web Service客户端。NuSOAP专门提供一个类进行WDSL文件的解析，并且从中提取信息。soapclient对象使用wsdl类来减轻开发者调用服务的难度。通过WSDL信息的帮助来创建报文，程序员仅仅需要知道操作的名字和参数就能调用它。</p>
<p>通过NuSOAP使用WSDL提供以下几点优点：<br />
所有的服务元文件，如命名空间(namespaces)，endpoint URLs，参数名(parameter names)等等都可以直接从WSDL文件获得，这样就允许客户端动态的适应服务器端的变化。因为从服务器随时可以获得，所以这些数据不再需要在用户脚本中使用硬性编码。<br />
它允许我们使用soap_proxy类。这个类派生自soapclient类，增加了WDSL文件中详细列出的操作所对应的方法。现在用户通过它可以直接调用这些方法。<br />
soapclient 类包含一个getProxy()方 法，它返回一个soap_proxy类的一个对象。soap_proxy类派生自soapclient类，增加了对应于 WSDL文档中定义的操作的方法， 并且允许用户调用一个endpoint的远程方法。这仅仅适用于soapclient对象用WDSL文件初始化的情况。优点是易于用户使用，缺点是性能&#8211;PHP中创建对象是耗时的&#8211;且不为功利目的服务 (and this functionality serves no utilitarian purpose)。</p>
<pre class="brush: php; title: ; notranslate">
//调用nusoap扩展类
$client = new NusoapApi(SERVICEDTS_WEBSERVER_URL,true);
$client-&gt;setSoapDefenCoding('utf-8');
$client-&gt;setDeCodeUtf8(false);
$client-&gt;setXmlEnCoding('utf-8');
$paras = array ('name' =&gt; 'hkshadow' );
$client-&gt;setArrParam($paras);
// 		$result = $client-&gt;getRequestData('GetTestStr');

//生成proxy类
$proxy = $client-&gt;getProxy();
//调用远程函数
$sq = $proxy-&gt;GetTestStr('Bruce Lee');
if (!$err=$proxy-&gt;getError()) {
	print_r($sq);
} else {
	print &quot;ERROR: $err&quot;;
}

print 'REQUEST:＜xmp＞'.$p-&gt;request.'＜/xmp＞';
print 'RESPONSE:＜xmp＞'.str_replace('&gt;&lt;', &quot;&gt;\n&lt;&quot;, $p-&gt;response).'＜/xmp＞';
</pre>
<p>运行刚刚创建的服务端url，http://127.0.0.1/test.php，执行后的结果如下。<br />
<a href="http://www.mudbest.com/webservicephp%e7%9a%84soap%e4%b8%8enusoap%e6%9c%8d%e5%8a%a1%e7%ab%af%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e9%80%9a%e4%bf%a1/1-17/" rel="attachment wp-att-1829"><img decoding="async" src="http://www.mudbest.com/wp-content/uploads/2013/03/1-540x324.jpg" alt="1" width="540" height="324" class="alignnone size-thumbnail wp-image-1829" /></a></p>
<p>点击方法名称。这样我们通过在service中增加了几行代码我们就通过使用NuSOAP为service提供了一个可视化的文档。但是，这还不是所有我们能做的。</p>
<p><a href="http://www.mudbest.com/webservicephp%e7%9a%84soap%e4%b8%8enusoap%e6%9c%8d%e5%8a%a1%e7%ab%af%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e9%80%9a%e4%bf%a1/2-12/" rel="attachment wp-att-1830"><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2013/03/2-540x370.jpg" alt="2" width="540" height="370" class="alignnone size-thumbnail wp-image-1830" /></a></p>
<p>我们在service中通过使用NuSOAP增加一些WSDL的调用我们可以为service生成WSDL还有一些其他的文档。与此不同的是，在 client中我们能做的就有些少了，至少在我们的这个简单的例子中是这样。下面所示的这个client跟没有使用WSDL的client没有什么不同 的，唯一的不同就是解析soapclent class是通过提供WSDL的URL来完成的，而不是之前的通过service endpoint。</p>
<p>NuSoap调用WebService时可以对编码进行设置，出现乱码的解决方法如下：</p>
<pre class="brush: php; title: ; notranslate">
$client = new nusoap_client(&quot;http://127.0.0.1/test.php?wsdl&quot;,true);    
$client-&gt;soap_defencoding = 'utf-8';
$client-&gt;decode_utf8 = false;
$client-&gt;xml_encoding = 'utf-8'; 
</pre>
<p>文件代码不能有任何输出 , 否则调用时会报类似如下错误：<br />
XML error parsing SOAP payload on line x( 行号 ): Reserved XML Name</p>
<p>如果在开启php5内置的soap时，nusoap的SoapClient类和php5内置的SOAP类有冲突的话(我这里没有这样的情况，2种同时开放)：<br />
解决方案<br />
1. 修改php.ini不加载php5内置的soap扩展（windows下是php_soap.dll）。<br />
2. 也有给nusoap的SoapClient类改名的。</p>
<p>至此，不论是php5内置的soap也好，还是nusoap扩展类也好，而对于webservice都是解决方案，从上面的部分例子可见，nusoap显得更灵活一些，而对于简单的webserice通信的话，php5内置的soap更快捷，不论是哪一种选择其一即可。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
