《PHP學習:php實現webservice實例》要點:
本文介紹了PHP學習:php實現webservice實例,希望對您有用。如果有疑問,可以聯系我們。
PHP實例本文實例講述了php實現webservice的辦法.分享給大家供大家參考.具體實現辦法如下:
PHP實例首先大家要簡單了解何謂webservice,接下來就做兩個非常簡單的例子,webservice還是逃不開server端與client端.
PHP實例這里的測試環境為:apache2.2.11 php5.2.10
PHP實例做這個測試之前,要確認你的php配置文件中已經將soap擴展打開,即
代碼如下:
extension=php_soap.dll;
OK 現在我們來體驗webservice
PHP實例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;
}
這是客戶端調用服務器端函數的例子,我們再搞個class的.
PHP實例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;
}
PHP實例希望本文所述對大家的PHP程序設計有所贊助.
歡迎參與《PHP學習:php實現webservice實例》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/14213.html