Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,是一种跨编程语言和跨操作系统平台的远程调用技术。
WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程的方法通过Web来调用这个应用程序。我们把调用这个WebService的应用程序叫做客户端,而把提供这个WebService的应用程序叫做服务端。
php webservice实例
本实例环境为:apache2.2.11 php5.2.10
确认php配置文件中已经将soap扩展打开, 代码:
extension=php_soap.dll;
client端 clientSoap.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; }
服务器端 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; }
声明:如需转载,请注明来源于www.webym.net并保留原文链接:http://www.webym.net/jiaocheng/978.html