簡單介紹
我總結了一下我自己的了解,webservice就是一種web服務,那麼他提供什麼樣的服務呢?舉個例子,我想寫個程式,每天告訴我天氣情況,我難道要發射個氣象衛星上天,每天監測,然後處理那海量的資料?noway,I just want know 明天出門要不要帶傘,那麼怎麼辦?這裡就可以利用WebService來解決。是以,webservice是一組網絡上的應用程式元件,想想我們平時怎麼通路我們本地類庫的,webservice提供了一種允許我們通路遠端類庫的方式。WebService是基于XML和HTTP的,可以說WebService并沒有什麼新的技術。XML可以描述消息,HTTP可以傳輸消息,兩者一結合,就可以在應用程式之間進行消息傳輸了。
既然知道了WebService是很什麼。那麼我們怎麼使用它呢?
其實在這個問題之前,還有個問題,How it works? 在webService中涉及到三個角色,一個是服務釋出者(就像是氣象局,如果不把天氣情況釋出出來,天知道明天是不是晴天),一個是服務中介者(這個很容易了解,氣象局把資訊釋出到哪裡?總不會直接釋出到你這裡吧?)還有一個就是服務請求者(也就是我們這些人)
在webservice中有三個基本要素,SOAP,WSDL,UDDI
SOAP是簡單對象傳輸協定
WSDL是Web服務描述語言,這裡包含三個資訊,服務提供哪些操作,如何通路服務,服務位于哪裡
UDDI是通用描述、發現、整合,是幫助web服務提供上釋出自己的服務。
Demo
PHP的SOAP有兩種模式,一種是wsdl模式,一種是non-wsdl模式,wsdl模式需要實作生成wsdl檔案,但是不靈活。 wsdl檔案生成的類檔案在最後提供 這裡有兩個類, http://localhost/soap/util.php
class Utils{
private $name;
private $sex;
public function Utils($name,$sex){
$this->name = $name;
$this->sex=$sex;
}
public function showInfo(){
return $this->sex.'--utils---hello--word--'.$this->name;
}
public function getInfo(){
}
}
class Tuti{
private $bir;
public function Tuti($bir='1999-12-12'){
$this->bir=$bir;
}
public function getInfo(){
return 'Tuil==='.$this->bir;
}
}
還有一些函數
http://localhost/soap/func.php
function getIp(){
return 'getIp====';
}
function getDir(){
return 'getDir';
}
function showDir(){
return 'showDis';
}
http://localhost/soap/service.php
$soap_server = new SoapServer(null, array(
'uri'=>'service.php'
));
//$soap_server->setClass('Utils','zhao','nv');
//$soap_server->setClass('Tuti','1991-12-09');
$soap_server->addFunction('getIp');
$soap_server->addFunction('getDir');
//$soap_server->addFunction('');
$soap_server->handle();
http://localhost/client/client.php
$client = new SoapClient(null,array('location'=>'http://localhost/soap/service.php','uri'=>'service.php'));
//$client = new SoapClient('http://localhost/www/soap-server/demo/test.wsdl');
//echo $client->showInfo();
//echo $client->getInfo();
echo '<br>=====<br>';
echo $client->getIp();
echo '==='.$client->getDir();
這樣我們通過通路client.php就可以通路到 http://localhost/soap/下的方法和類
注意
setclass隻能是一個,不能和addFunction同用 Client端location和uri必須都的有,Server端必須有uri
附件生成wsdl檔案