新聞中心
如何在php中使用webservice?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

具體實現(xiàn)方法如下:
首先大家要簡單了解何謂webservice,接下來就做兩個非常簡單的例子,webservice還是逃不開server端與client端。
這里的測試環(huán)境為:apache2.2.11 php5.2.10
做這個測試之前,要確認(rèn)你的php配置文件中已經(jīng)將soap擴(kuò)展打開,即
復(fù)制代碼 代碼如下:
extension=php_soap.dll;
OK 現(xiàn)在我們來體驗webservice
server端 serverSoap.php
復(fù)制代碼 代碼如下:
$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ù)的例子,我們再搞個class的。
server端 serverSoap.php
復(fù)制代碼 代碼如下:
$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;
}
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
標(biāo)題名稱:如何在php中使用webservice-創(chuàng)新互聯(lián)
文章位置:http://www.dlmjj.cn/article/igsps.html


咨詢
建站咨詢
