免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2623 | 回复: 0
打印 上一主题 下一主题

Webservice学习笔记之PHP使用soapserver+wsdl构建服务 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-21 21:05 |只看该作者 |倒序浏览
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代码

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <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">
  3. <types>
  4. <xsd:schema targetNamespace="http://www.somelocation.com">
  5. <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
  6. <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
  7. </xsd:schema>
  8. </types>
  9. <message name="userDataRequest">
  10.   <part name="operation" type="xsd:string" />
  11.   <part name="statement" type="xsd:string" />
  12. </message>
  13. <message name="userDataResponse">
  14.   <part name="return" type="xsd:string" />
  15. </message>
  16. <portType name="userWsdlPortType">
  17.   <operation name="userData">
  18.     <documentation>Query User Data</documentation>
  19.     <input message="tns:userDataRequest" />
  20.     <output message="tns:userDataResponse" />
  21.   </operation>
  22. </portType>
  23. <binding name="userWsdlBinding" type="tns:userWsdlPortType">
  24.   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
  25.   <operation name="userData">
  26.     <soap:operation soapAction="http://www.somelocation.com#feelbad" style="rpc" />
  27.     <input><soap:body use="encoded" namespace="http://www.somelocation.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></input>
  28.     <output><soap:body use="encoded" namespace="http://www.somelocation.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
  29.   </operation>
  30. </binding>
  31. <service name="userWsdl">
  32.   <port name="userWsdlPort" binding="tns:userWsdlBinding">
  33.     <soap:address location="http://localhost/soap/user.php" />
  34.   </port>
  35. </service>
  36. </definitions>
复制代码
然后,在服务端创建user.php文件,内容如下:

Php代码

  1. <?php

  2. //设置不缓存wsdl
  3. ini_set("soap.wsdl_cache_enabled","0");

  4. //初始化wsdl服务
  5. $server = new SoapServer("user.wsdl");

  6. //主功能性的class,这里可以分离出来写各种复杂的逻辑
  7. class USER {

  8.     function getInfo($userId) {
  9.         return json_encode(array('userId'=>$userId,'userName'=>'Zhang San'));
  10.     }

  11.     function getGroup($userId) {
  12.         return json_encode(array('userId'=>$userId,'userGroup'=>'111'));
  13.     }

  14. }

  15. //接口的主入口函数
  16. function userData($operation,$statement){
  17.   return USER::$operation($statement);
  18. }

  19. //注册主函数
  20. $server->AddFunction("userData");

  21. //启动soap server
  22. $server->handle();

  23. ?>
复制代码
接下来,我们就可以使用soapClient访问刚才的服务了,client.php,代码如下:

Php代码

  1. <?php

  2. $client = new SoapClient('http://localhost/soap/user.php?wsdl');
  3. $res = $client->__soapCall('userData',array('operation'=>'getInfo','statement'=>'111'));

  4. print_r($res);

  5. ?>
复制代码
这里其实最复杂的操作就是创建wsdl文件,java语言中可以很方便的自动创建,而php中只能使用zend studio,这是个商业软件,就算了,所以推荐给大家一个非常好用的wsdl生成工具:nusoap,因为官方号称使用C语言编写的的soapServer扩展要比其他使用PHP语言编写的soap服务端效率高很多,所以我们虽然不用nusoap,还是可以利用它来帮助生成wsdl的,而且很方便,以下就是我生成user.wsdl文件的代码:

Php代码

  1. <?php
  2. require_once('./lib/nusoap.php');

  3. $server = new soap_server();

  4. //配置WSDL namespace;
  5. $server->configureWSDL('userWsdl','http://www.somelocation.com',false,'rpc','http://schemas.xmlsoap.org/soap/http','http://www.somelocation.com');

  6. //注册服务
  7. $server->register('userData',
  8.     array('operation' => 'xsd:string','statement' => 'xsd:string'),
  9.     array('return' => 'xsd:string'),
  10.     'http://www.somelocation.com',
  11.     'http://www.somelocation.com#feelbad',
  12.     'rpc',
  13.     'encoded',
  14.     'print feel bad'
  15. );

  16. function userData($operation,$statement){
  17.   return "Query User Data";
  18. }

  19. $HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");

  20. $server->service($HTTP_RAW_POST_DATA);
  21. ?>
复制代码
经过几小时的奋战,终于战胜了webservice,战胜了wsdl,欢迎大家跟我交流。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP