首先,web service 三要素是
1.xml格式的soap資訊,頭部以http協議發送,其發送格式類似以下
<!--http 頭資訊-->
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 1024
<?xml version="1.0"?>
<!-- 根項目,表明此xml格式資料為soap資訊 -->
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
//可有soap:Header進行驗證,如請求地址無驗證需要,則可不用添加頭部資訊-->
主體,標明請求soap服務端的方法,以及參數-->
<soap:Body>
<!--方法名-->
<methodname>
<!--參數名以及值-->
<param>hello world<param>
<methodname>
</soap:Body>
</soap:Envelope>
response格式則是對應的的http回應標頭,以及類似發送的xml格式的soap返回資訊(參閱http://www.w3school.com.cn/soap/soap_example.asp)
2.描述client端如何訪問server端方法的wsdl(web service description language),是xml格式。
3.還有個uddi
因為要在本地實現簡單的web service,故忽略uddi.
一個完整的web service請求是這樣的:
========================================================================
client->request(http head+soap xml)->wsdl->server->handle function->response(http head+soap xml)->client
=========================================================================
wsdl起著通道的作用,接收到請求資訊,引導至服務端相應的操作。
而請求端與服務響應端可為不同平台,不同語言,只要同樣都讀懂wsdl檔案.
說白了,web service就是根據各語言都可解析xml的情況,利用xml作為各不同的開發語言之間的資訊橋樑。
以下是簡單的本地web service php實現,3個檔案均位於http://localhost/lake/soap/目錄下
1.wsdl檔案:test.wsdl
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='lake'
targetNamespace='http://localhost/lake/soap'
xmlns:tns='http://localhost/lake/soap'
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/'>
<message name='getRequest'>
<part name='name' type='xsd:string'/>
<part name='pass' type='xsd:string'/>
</message>
<message name='getResponse'>
<part name='Result' type='xsd:string'/>
</message>
<portType name='class'>
<operation name='helloworld'>
<input message='tns:getRequest'/>
<output message='tns:getResponse'/>
</operation>
</portType>
<binding name='class' type='tns:class'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='helloworld'>
<soap:operation soapAction=''/>
<input>
<soap:body use='encoded' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<service name='class'>
<port name='goforsoap' binding='class'>
<soap:address location='http://localhost/lake/soap/soapserver.php'/>
</port>
</service>
</definitions>
<portType>是整個wsdl檔案的核心,相當於wsdl的類,下面定義類的方法,即可認為<portType>就是定義web service所提供的服務,類似以上
<portType name='class'>:我有一個叫class的類
<opration name='helloword'>:我class類下有一方法名叫helloworld
<input message='...'> : 用戶端請求呼叫伺服器端helloworld方法時的參數描述名,對應上面的message標籤的name
<output message='...'>:服務端返執行helloworld方法傳回值的描述,對應上面的message標籤的name
然後往上看,有兩個<message>標籤,就是對input,output參數的描述,類型,參數名等。
<binding>即綁定soap協議,具體參考http://www.w3school.com.cn/上對wsdl格式的詳解
最後當然要定義web service所在目標地址了,我將在http://localhost/lake/soap/下建立個soapserver.php作為web service檔案,so...
注意<port>裡面的binding屬性一定要設定成之前定義的名為class的類
定義完wsdl,剩下的就簡單了
2.soapclent.php
<?php
$soapClient = new SoapClient("test.wsdl");
echo $soapClient->helloworld('lake','123456');
?>
3.soapserver.php
<?php
class serverClass
{
function helloworld($name,$pass)
{
return "i got that you are {$name},and your password is {$pass}";
}
}
$soapServer = new SoapServer("test.wsdl");
$soapServer->setClass('serverClass');
$soapServer->handle();
?>
瀏覽器開啟http://localhost/lake/soap/soapclient.php,不出意外就出現i got that you are lake,and your password is 1234567這樣的返回串了。
記得關閉wsdl緩衝,以及開啟openssl擴充,在php.ini裡可設定。