標籤:
首先大家要簡單瞭解了何謂webservice,接下來就做兩個非常簡單的例子,webservice還是逃不開server端與client端。我測試的環境為:apache2.2.11 php5.2.10做這個測試之前,要確認你的php設定檔中已經將soap擴充開啟,即extension=php_soap.dll;OK 現在我們來體驗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.phptry { $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的。//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.phptry { $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 webservice執行個體(轉載)