在php编程中webservice的实现方法,有关php中webservice原理与实现技巧,webservice实现方法,分别包括server端与client端,感兴趣的朋友参考下。
php实现webservice的方法
什么是webservice?
通过例子来了解下,webservice实现方法,分别包括server端与client端。
测试环境为:apache2.2.11 php5.2.10
确保php配置文件中已将soap扩展打开,即为:
extension=php_soap.dll;
开始体验web service用php是怎么实现的?php调用webservice实例代码。
1、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;
}
2、客户端调用服务器端函数
1)、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;
}