刚开始接触webservice时曾经被wsdl语言的各种标签搞的头大,不过为了搞清楚每个标签的含义,我还是硬着头皮啃了两个小时的规范文档,如果你想要深入理解webservice的话,还是非常建议你仔细读读wsdl规范,只有这样才能知其所以然。
其实使用PHP语言构建webservice本身就不是一件非常推荐的事情,这个语言的解释型特性决定了他无法达到像java等编译型语言的效率。不过谁让PHP开发快速呢,简单,容易上手!
PHP的5.0版本以后就已经内置了soapServer的class,这里就假设你正在使用5.0以上的版本,5.0以下版本的请搜索nusoap。
闲话少说,下面我们构建一个查询用户基本信息的实例:
首先,创建wsdl文件,如:user.wsdl,内容如下:
<?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
//设置不缓存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
$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
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,欢迎大家跟我交流。
分享到:
相关推荐
webservice工具,针对wsdl、soap进行测试的小软件
WebService之自定义拦截器(server+client)
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
webservice soap wsdl详解
webService学习笔记
详细描述WebService中SOAP协议的规范,以及WSDL文件所需要包含的要素
WebService学习笔记 比较经典
webservice学习笔记doc文档,喜欢或想要了解webservice的朋友可以下载学习一下。
PHP webservice教程,soap wsdl密码验证,webservice高级应用; 好用一定要给好评哦
本实例包含php调用webservice及myeclipse创建webservice实例全部代码,简单易懂,运行绝对没有问题,文件齐全,导入即可运行,适合webservice初学者
Webservice学习笔记
尚硅谷Webservice学习笔记,详情请参看文档,仅供个人学习
Java使用SOAP获取webservice实例解析 具体实例分析说明。
java项目简单模拟soap型的webservice远程调用服务,内附使用说明文档方便学习和查阅,让你轻松学会soap-webservice的使用方法!
NULL 博文链接:https://zlhroar.iteye.com/blog/2229400
PHP使用SOAP调用WEBSERVICE接口
使用WSDL生成webService接口,编写Test调用方法进行测试