<?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>webservices &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/webservices/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>
	</channel>
</rss>
