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


 

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