天天看点

PHP中WebService应用_SOAP

简单介绍

我总结了一下我自己的理解,webservice就是一种web服务,那么他提供什么样的服务呢?举个例子,我想写个程序,每天告诉我天气情况,我难道要发射个气象卫星上天,每天监测,然后处理那海量的数据?noway,I just want know 明天出门要不要带伞,那么怎么办?这里就可以利用WebService来解决。所以,webservice是一组网络上的应用程序组件,想想我们平时怎么访问我们本地类库的,webservice提供了一种允许我们访问远程类库的方式。WebService是基于XML和HTTP的,可以说WebService并没有什么新的技术。XML可以描述消息,HTTP可以传输消息,两者一结合,就可以在应用程序之间进行消息传输了。

既然知道了WebService是很什么。那么我们怎么使用它呢?

其实在这个问题之前,还有个问题,How it works? 在webService中涉及到三个角色,一个是服务发布者(就像是气象局,如果不把天气情况发布出来,天知道明天是不是晴天),一个是服务中介者(这个很容易理解,气象局把信息发布到哪里?总不会直接发布到你这里吧?)还有一个就是服务请求者(也就是我们这些人)

在webservice中有三个基本要素,SOAP,WSDL,UDDI

SOAP是简单对象传输协议

WSDL是Web服务描述语言,这里包含三个信息,服务提供哪些操作,如何访问服务,服务位于哪里

UDDI是通用描述、发现、整合,是帮助web服务提供上发布自己的服务。

Demo   

PHP的SOAP有两种模式,一种是wsdl模式,一种是non-wsdl模式,wsdl模式需要实现生成wsdl文件,但是不灵活。 wsdl文件生成的类文件在最后提供 这里有两个类, http://localhost/soap/util.php

class Utils{
		private $name;
		private $sex;
		public function Utils($name,$sex){
			$this->name = $name;
			$this->sex=$sex;
		}
		public function showInfo(){
			return $this->sex.'--utils---hello--word--'.$this->name;
		}
		public function getInfo(){
			
		}
	}
	
	class Tuti{
		private $bir;
		public function Tuti($bir='1999-12-12'){
			$this->bir=$bir;
		}
		public function getInfo(){
			return  'Tuil==='.$this->bir;
		}
	}
           

还有一些函数

http://localhost/soap/func.php

function getIp(){
		return 'getIp====';
	}
	
	function getDir(){
		return 'getDir';
	}
	
	function showDir(){
		return 'showDis';
	}
           

http://localhost/soap/service.php

$soap_server = new SoapServer(null, array(
			'uri'=>'service.php'
	));
	//$soap_server->setClass('Utils','zhao','nv');
	//$soap_server->setClass('Tuti','1991-12-09');
	$soap_server->addFunction('getIp');
	$soap_server->addFunction('getDir');
	//$soap_server->addFunction('');
	$soap_server->handle();
           

http://localhost/client/client.php

$client = new SoapClient(null,array('location'=>'http://localhost/soap/service.php','uri'=>'service.php'));
	//$client = new SoapClient('http://localhost/www/soap-server/demo/test.wsdl');
	//echo $client->showInfo();
	//echo $client->getInfo();
	echo '<br>=====<br>';
	echo $client->getIp();
	echo '==='.$client->getDir();
           

这样我们通过访问client.php就可以访问到 http://localhost/soap/下的方法和类

注意

setclass只能是一个,不能和addFunction同用 Client端location和uri必须都的有,Server端必须有uri

附件生成wsdl文件