首先大家要簡(jiǎn)單了解了何謂webservice,接下來(lái)就做兩個(gè)非常簡(jiǎn)單的例子,webservice還是逃不開server端與client端。
我測(cè)試的環(huán)境為:apache2.2.11 php5.2.10
做這個(gè)測(cè)試之前,要確認(rèn)你的php配置文件中已經(jīng)將soap擴(kuò)展打開,即extension=php_soap.dll;
OK 現(xiàn)在我們來(lái)體驗(yàn)webservice
//server端 serverSoap.php
$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179/"));//This uri is your SERVER ip.
$soap->addFunction('minus_func'); //Register the function
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
function minus_func($i, $j){
$res = $i - $j;
return $res;
}
//client端 clientSoap.php
try {
$client = new SoapClient(null,
array('location' =>"http://192.168.1.179/test/serverSoap.php",'uri' => "http://127.0.0.1/")
);
echo $client->minus_func(100,99);
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
這是客戶端調(diào)用服務(wù)器端函數(shù)的例子,我們?cè)俑銈€(gè)class的。
//server端 serverSoap.php
$classExample = array();
$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179/",'classExample'=>$classExample));
$soap->setClass('chesterClass');
$soap->handle();
class chesterClass {
public $name = 'Chester';
function getName() {
return $this->name;
}
}
//client端 clientSoap.php
try {
$client = new SoapClient(null,
array('location' =>"http://192.168.1.179/test/serverSoap.php",'uri' => "http://127.0.0.1/")
);
echo $client->getName();
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。