wsdl wsdl(网络服务描述语言,web services description language)是一门基于 xml 的语言,用于描述 web services 以及如何对它们进行访问。这种文档可描述某个 web service。它可规定服务的位置,以及此服务提供的操作(或方法)。
一个 wsdl 文档的主要结构是类似这样的:
definition of types........ definition of a message.... definition of a port....... definition of a binding....
wsdl 文档可包含其它的元素,比如 extension 元素,以及一个 service 元素,此元素可把若干个 web services 的定义组合在一个单一的 wsdl 文档中。
php生成wsdl 类代码(soapdiscovery.class.php):
class_name = $class_name; $this->service_name = $service_name; } /** * soapdiscovery::getwsdl() returns the wsdl of a class if the class is instantiable. * * @return string **/ public function getwsdl() { if (empty($this->service_name)) { throw new exception('no service name.'); } $headerwsdl = n; $headerwsdl.= service_name targetnamespace=urn:$this->service_name xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/ xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/ xmlns:tns=urn:$this->service_name xmlns:xsd=http://www.w3.org/2001/xmlschema xmlns:soap-enc=http://schemas.xmlsoap.org/soap/encoding/ xmlns=http://schemas.xmlsoap.org/wsdl/>n; $headerwsdl.= n; if (empty($this->class_name)) { throw new exception('no class name.'); } $class = new reflectionclass($this->class_name); if (!$class->isinstantiable()) { throw new exception('class is not instantiable.'); } $methods = $class->getmethods(); $porttypewsdl = 'service_name.'port>'; $bindingwsdl = 'service_name.'binding type=tns:'.$this->service_name.port>nn; $servicewsdl = 'service_name.>nnservice_name.'port binding=tns:'.$this->service_name.binding>nnn; $messagewsdl = ''; foreach ($methods as $method) { if ($method->ispublic() && !$method->isconstructor()) { $porttypewsdl.= 'getname().>n.'getname().request />ngetname().response />nn; $bindingwsdl.= 'getname().>n.'service_name.'#'.$this->class_name.'#'.$method->getname(). />nservice_name encodingstyle=http://schemas.xmlsoap.org/soap/encoding/ />nnnservice_name encodingstyle=http://schemas.xmlsoap.org/soap/encoding/ />nnn; $messagewsdl.= 'getname().request>n; $parameters = $method->getparameters(); foreach ($parameters as $parameter) { $messagewsdl.= 'getname(). type=xsd:string />n; } $messagewsdl.= n; $messagewsdl.= 'getname().response>n; $messagewsdl.= 'getname(). type=xsd:string />n; $messagewsdl.= n; } } $porttypewsdl.= n; $bindingwsdl.= n; return sprintf('%s%s%s%s%s%s', $headerwsdl, $porttypewsdl, $bindingwsdl, $servicewsdl, $messagewsdl, ''); } /** * soapdiscovery::getdiscovery() returns discovery of wsdl. * * @return string **/ public function getdiscovery() { return nnn; }} ?>
使用方法(服务端server.php):
getwsdl(); file_put_contents(wsdl_url,$str);} //soap开启并接收client传入的参数响应 $server = new soapserver(wsdl_url);$server->setclass('helloworld');$server->handle(); //测试定义公开的类class helloworld { private $nombre = ''; public function __construct($name = 'world') {$this->name = $name;} public function greet($name = '') {$name = $name?$name:$this->name;return 'hello '.$name.'.';} public function servertimestamp() {return time();} }?>
客户端client.php:
greet('ieliwb'); var_dump($result); echo the answer isresult;}catch (soapfault $f){ echo error message: {$f->getmessage()};}?>
创建 webservice 1. 创建wsdl
非标准的webservice,可能只能php才能访问 标准的webservice,就必须要使用wsdl(webservice description language,就是用xml语法标准来描述你的服务内容,我是这么理解的) 在这里我只介绍标准的webservice。那么如何创建wsdl呢?对于php来说这确实是件很不容易的事情,有人说用zend studio创建很方便,这是一种方法。但对于那些不喜欢用zend studio的人来说,会觉得创建一个webservice还要安装zend studio,太强人所难了,我就是,嘿嘿。
在这里我介绍一个简单的方法,到网上下载soapdiscovery.class.php类,里面有个公用方法:getwsdl,这个方法末尾是用的return,那么,你修改一下这个方法,我是这么做的:
//return sprintf('%s%s%s%s%s%s', $headerwsdl, $porttypewsdl, $bindingwsdl, $servicewsdl, $messagewsdl, '');//生成wsdl文件,将上面的return注释$fso = fopen($this->class_name . .wsdl , w);fwrite($fso, sprintf('%s%s%s%s%s%s', $headerwsdl, $porttypewsdl, $bindingwsdl, $servicewsdl, $messagewsdl, ''));
现在生成wsdl的类有了,soapdiscovery.class.php。
我只要再准备一个提供服务的类或者函数就可以创建wsdl了。比如我有个类:person,文件名为:person.class.php★,里面有两个方法,一个是say,一个是run。很简单。
到这里有两个类了:soapdiscovery.class.php和person.class.php。
开始正式生成wsdl:创建文件server.php。将以下内容拷贝进去,运行即可生成一个person.wsdl文件
getwsdl();?>
2. 创建webservice服务端程序
将server.php文件的内容清空,复制以下代码进去:
setclass(person);//注册person类的所有方法 $objsoapserver->handle();//处理请求?>
3. 创建webservice客户端程序,测试webservice是否有效,文件名是:client.php
say()); echo
; echo($client->run()); echo
;?>
ok,结束。.net如果要使用的话,你只要提供一个url给他就行了。
获得url的方法:你可以先到person.wsdl文件里面查找,这里的url(具体url是根据你的目录确定的)就是你要提供给.net开发人员使用的。不过别高兴太早,后面要加:“?wsdl”,http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl这样才是对的,不信你可以将url拷贝到浏览器的地址栏里看下就知道了。
.net开发人员获得你给他的url之后,就可以在自己的项目里面添加一个服务引用或者web引用了,然后就可以根据提示完成相关操作,对于使用.net的开发人员来说很简单的。
http://www.bkjia.com/phpjc/752376.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/752376.htmltecharticlewsdl wsdl(网络服务描述语言,web services description language)是一门基于 xml 的语言,用于描述 web services 以及如何对它们进行访问。这种文档可...