WebService学习系列(二)------构造自己的WebService服务器

一、配置PHP的支持SOAP环境

   修改调用客户端。也就是说修改php.ini,修改一下配置,改完之后重启Apache服务器。

 

二、WSDL(文件说明)

   我们调用一下该服务的地址:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL

  

<?php
//客户端通过wsdl,即可以了解webservice的可调用方法及参数的细节
$soapclient=new soapclient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL');
print_r($soapclient->__getFunctions());
?>

结果:

   

<?php

//客户端通过wsdl,即可以了解webservice的可调用方法及参数的细节

$soapclient=new soapclient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL');

//print_r($soapclient->__getFunctions());//分析webservice有哪些方法调用

print_r($soapclient->getMobileCodeInfo(array('mobileCode'=>13121859603)));

 
?>

三、构造自己的webservice服务器

1.新建wsdl.xml

<?xml version='1.0' encoding='UTF-8'?>
<definitions name='zixue.it'
         targetNamespace='http://localhost/webservice/'
                 xmlns:tns='http://localhost/webservice/'
		 xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
		 xmlns:xsd='http://www.w3.org/2001/XMLSchema'
		 xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
		 xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
		 xmlns='http://schemas.xmlsoap.org/wsdl/'>
<!--<types>元素定义webservice 使用的数据类型,WSDL使用XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型 -->
<types>
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
				targetNamespace="http://localhost/webservice/">
		</xsd:schema>
</types>
<!--<message>元素可以定义每个消息的部件,以及相关的数据类型-->
<message name='sumRequest'>
	<part name="term" type="xsd:string"/>
</message>
<message name='sumResponse'>
	<part name="value" type="xsd:string"/>
</message>
<!--<portType>元素是最重要的WSDL元素。它可描述一个web service、可被执行的操作,以及相关的消息。他告诉你去哪个webservice的连接点,扮演了一个控制者-->
<portType name='oplist'>
		<operation name='sum'>
			<input message='tns:sumRequest'/>
			<output message='tns:sumResponse'/>
		</operation>
</portType>
<!--<binding>元素为每个端口定义消息格式和协议细节-->
<binding name='cartSoap' type='tns:oplist'>
<!--style:属性可取值"rpc"或"document",ransport:属性定义了要使用的SOAP协议.在这个例子中我们使用HTTP-->
	<soap:binding style='rpc'
		transport='http://schemas.xmlsoap.org/soap/http'/>
		<!--operation元素定义了每个端口提供的操作符,相应的SOAP行为都需要被定义-->
	<operation name='sum'>
		<soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
		<input>
			<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
				encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
		</input>
		<output>
			<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
				encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
		</output>
	</operation>
</binding>
<!--<service>包含一个或多个port元素,每个port元素表示一个不同的web服务-->
<service name='shopWS'>
	<port name='cartSoap' binding='tns:cartSoap'>
		<soap:address location='http://localhost/webservice/server.php'/>
	</port>
</service>
</definitions>
<!---->
<!--style:-->



新建server.php
<?php
function sum($str){
	return 'webservice'.$str;
}
$server=new soapserver('./wsdl.xml');//按照wsdl的说明来创建服务器
$server->addFunction('sum');
$server->handle();
?>

新建客户端client.php

<?php
$soapclient=new soapclient('http://localhost/webservice/wsdl.xml');
//print_r($soapclient->__getFunctions());
?>

打印出来:

Array

(

    [0] => string sum(string $term)

)

在client.php中加入以下内容:

/**
这说明,可以调用服务端的sum方法,并传字符串参数叫term,且返回字符串参数	

 * */

 echo $soapclient->sum('haha');
显示:



我们传的参数是字符串类型,我们想传一个数组的参数该怎么办呢?

如果修改参数为数组,修改wsdl.xml:

修改server.php


修改client.php

预览client.php


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章