Webservice学习笔记之PHP使用soapserver+wsdl构建服务
刚开始接触webservice时曾经被wsdl语言的各种标签搞的头大,不过为了搞清楚每个标签的含义,我还是硬着头皮啃了两个小时的规范文档,如果你想要深入理解webservice的话,还是非常建议你仔细读读wsdl规范,只有这样才能知其所以然。
其实使用PHP语言构建webservice本身就不是一件非常推荐的事情,这个语言的解释型特性决定了他无法达到像java等编译型语言的效率。不过谁让PHP开发快速呢,简单,容易上手!
PHP的5.0版本以后就已经内置了soapServer的class,这里就假设你正在使用5.0以上的版本,5.0以下版本的请搜索nusoap。
闲话少说,下面我们构建一个查询用户基本信息的实例:
首先,创建wsdl文件,如:user.wsdl,内容如下:
Xml代码
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.somelocation.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.somelocation.com">
- <types>
- <xsd:schema targetNamespace="http://www.somelocation.com">
- <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
- <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
- </xsd:schema>
- </types>
- <message name="userDataRequest">
- <part name="operation" type="xsd:string" />
- <part name="statement" type="xsd:string" />
- </message>
- <message name="userDataResponse">
- <part name="return" type="xsd:string" />
- </message>
- <portType name="userWsdlPortType">
- <operation name="userData">
- <documentation>Query User Data</documentation>
- <input message="tns:userDataRequest" />
- <output message="tns:userDataResponse" />
- </operation>
- </portType>
- <binding name="userWsdlBinding" type="tns:userWsdlPortType">
- <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- <operation name="userData">
- <soap:operation soapAction="http://www.somelocation.com#feelbad" style="rpc" />
- <input><soap:body use="encoded" namespace="http://www.somelocation.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></input>
- <output><soap:body use="encoded" namespace="http://www.somelocation.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
- </operation>
- </binding>
- <service name="userWsdl">
- <port name="userWsdlPort" binding="tns:userWsdlBinding">
- <soap:address location="http://localhost/soap/user.php" />
- </port>
- </service>
- </definitions>
复制代码 然后,在服务端创建user.php文件,内容如下:
Php代码
- <?php
- //设置不缓存wsdl
- ini_set("soap.wsdl_cache_enabled","0");
- //初始化wsdl服务
- $server = new SoapServer("user.wsdl");
- //主功能性的class,这里可以分离出来写各种复杂的逻辑
- class USER {
- function getInfo($userId) {
- return json_encode(array('userId'=>$userId,'userName'=>'Zhang San'));
- }
- function getGroup($userId) {
- return json_encode(array('userId'=>$userId,'userGroup'=>'111'));
- }
- }
- //接口的主入口函数
- function userData($operation,$statement){
- return USER::$operation($statement);
- }
- //注册主函数
- $server->AddFunction("userData");
- //启动soap server
- $server->handle();
- ?>
复制代码 接下来,我们就可以使用soapClient访问刚才的服务了,client.php,代码如下:
Php代码
- <?php
- $client = new SoapClient('http://localhost/soap/user.php?wsdl');
- $res = $client->__soapCall('userData',array('operation'=>'getInfo','statement'=>'111'));
- print_r($res);
- ?>
复制代码 这里其实最复杂的操作就是创建wsdl文件,java语言中可以很方便的自动创建,而php中只能使用zend studio,这是个商业软件,就算了,所以推荐给大家一个非常好用的wsdl生成工具:nusoap,因为官方号称使用C语言编写的的soapServer扩展要比其他使用PHP语言编写的soap服务端效率高很多,所以我们虽然不用nusoap,还是可以利用它来帮助生成wsdl的,而且很方便,以下就是我生成user.wsdl文件的代码:
Php代码
- <?php
- require_once('./lib/nusoap.php');
- $server = new soap_server();
- //配置WSDL namespace;
- $server->configureWSDL('userWsdl','http://www.somelocation.com',false,'rpc','http://schemas.xmlsoap.org/soap/http','http://www.somelocation.com');
- //注册服务
- $server->register('userData',
- array('operation' => 'xsd:string','statement' => 'xsd:string'),
- array('return' => 'xsd:string'),
- 'http://www.somelocation.com',
- 'http://www.somelocation.com#feelbad',
- 'rpc',
- 'encoded',
- 'print feel bad'
- );
- function userData($operation,$statement){
- return "Query User Data";
- }
- $HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
- $server->service($HTTP_RAW_POST_DATA);
- ?>
复制代码 经过几小时的奋战,终于战胜了webservice,战胜了wsdl,欢迎大家跟我交流。 |