PHP 中使用 SOAP(2)

PHP 中还实现了通过 WSDL 对 Web 服务的发布。

WSDL 是一种用于描述Web服务的语法规范,针对每个Web服务来说,它是一个说明文档,对web服务的位置,协议和接口进行详细的说明.由web服务的开发者提供。

WSDL文件包括5部分:types, Message,PortType,Binding和Service五部分.

1 Types定义: 类型定义,独立于语言.对应于SOAP消息中要传输的元素信息的定义
2 Message: 每个web方法对应两个message定义in和out.而message的定义包含了头和体
3 PortType: 每个web service对应一个PortType,该PortType中又包含了对其发布的方法, operation(操作)
4 Bindings: 指定每porttype中每个操作(类以及方法)的绑定信息,包含input和output的消息的格式.
5 Service: 每个web service绑定的port信息

Web 服务除过按照前述的示例形式发布外,还可以通过 WSDL 文档来发布。

示例:

要发布的类,文件 myservice.php:
<?php
class service
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}

$server=new SoapServer('TestSoap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>

WSDL 描述文档,文件 TestSoap.wsdl:

<?xml version='1.0' encoding='UTF-8'?>
<definitions name="TestSoap"
targetNamespace="urn:TestSoap"
xmlns:typens="urn:TestSoap"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">

<message name="Add">
<part name="a"/>
<part name="b"/>
</message>

<message name="AddResponse">
<part name="AddReturn"/>
</message>

<message name="HelloWorld"/>
<message name="HelloWorldResponse">

<part name="HelloWorldReturn"/>
</message>

<message name="Sub">
<part name="a"/>
</message>

<message name="SubResponse">
<part name="SubReturn"/>
</message>

<portType name="servicePortType">
<operation name="Add">
<input message="typens:Add"/>
<output message="typens:AddResponse"/>
</operation>
<operation name="HelloWorld">
<input message="typens:HelloWorld"/>
<output message="typens:HelloWorldResponse"/>
</operation>
<operation name="Sub">
<input message="typens:Sub"/>
<output message="typens:SubResponse"/>
</operation>
</portType>

<binding name="serviceBinding" type="typens:servicePortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="Add">
<soap:operation soapAction="urn:serviceAction"/>
<input>
<soap:body namespace="urn:TestSoap" use="literal"/>
</input>
<output>
<soap:body namespace="urn:TestSoap" use="literal"/>
</output>
</operation>
<operation name="HelloWorld">
<soap:operation soapAction="urn:serviceAction"/>
<input>
<soap:body namespace="urn:TestSoap" use="literal"/>
</input>
<output>
<soap:body namespace="urn:TestSoap" use="literal"/>
</output>
</operation>
<operation name="Sub">
<soap:operation soapAction="urn:serviceAction"/>
<input>
<soap:body namespace="urn:TestSoap" use="literal"/>
</input>
<output>
<soap:body namespace="urn:TestSoap" use="literal"/>
</output>
</operation>
</binding>

<service name="TestSoapService">
<port name="servicePort" binding="typens:serviceBinding">
<soap:address location="http://localhost:8080/_myPHP5/soap/Wsdl/myservice.php"/>
</port>
</service>

</definitions>

调用代码,文件 Client.php:

<?php
error_reporting(7);

$client = new SoapClient("http://localhost:8080/_myPHP5/soap/Wsdl/TestSoap.wsdl");
echo $client->HelloWorld();
echo("<br>");
echo $client->Add(10, 20);
?>

然而,WSDL 文档的编写是一件很麻烦的事情,无聊又容易出错。很多人认为那玩意儿不是人写的,但是,如果有好的软件工具,那玩意儿又是不需要人写的。Zend 公司的 ZED 5.0 系列和 Zend Studio for eclipse 6.0 原来是很好支持 WDSL 的可视化编辑和类的发布的(按照一个类文件智能生成),但 Zend studio 7.0 之后,这方面功能有所减弱。但基于 Eclipse 构建的 Zend studio 7.x,还是有一个 WSDL 的可视化编辑器,功能也还够用,生成的 WSDL 文件与以前有细微的变化。需要程序员必须对 WSDL 文档里的标签和元素很熟悉。

附录:关于 PHP 开发 Soap 的一些错误

1、开发的时候一定要关闭 php soap 的缓存,服务器和客户端都需要,不然会报:

Fatal error: Uncaught SoapFault exception: [Client] Function (”test”) is not a valid method for this service in ……\clien.php:5 Stack trace:
#0 [internal function]: SoapClient->__call(’test’, Array)
#1 D:\xampp\htdocs\clien.php(5): SoapClient->test()
#2 {main}

关闭方法:
ini_set("soap.wsdl_cache_enabled", "0");

可以通过类似 $client->__getFunctions() 等方法查看 Soap 的一些信息。


2、如果调试时报告不识别xml错误,请确保代码里没有空格等无关信息,比如 Utf-8 编码文件的 BOM 头。

作者:张庆(网眼) 西安 PHP 教育培训中心 2010-7-11
来自“网眼视界”:http://blog.why100000.com
作者微博:http://t.qq.com/zhangking
“十万个为什么”电脑学习网:http://www.why100000.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值