SoapClientc?/strong> <wsdl:definitions WSDL模式服务 <?php 在这个服务的实现q程中,函数实现了WSDL所定义的服务操作greetQgreet操作有一个WSDL指定的参敎ͼ按照greet操作的语义,q个参数是一个用L名字。最后handle调用了触发处理请求的服务对象?/p>
<?php 客户端代码中Q首先创Z个用WSDL文g作参数的SoapClient实例。接着__soapCall()调用作ؓ参数传入它的操作Q也是greet和传入操作的参数?br />
h和响?br />
<?xml version="1.0" encoding="UTF-8"?> 下面是服务端响应上诉h而发送的SOAP响应Q?/p>
<?xml version="1.0" encoding="UTF-8"?> 上面的SOAP消息都是利用WSDL模式的服务端和客L来获取的。也可以利用non-WSDL模式的服务端和客L来生与上面相同的SOAP消息。但是,PHP代码必须有一Ҏ变。下一节会说明如何使用non-WSDL模式?br />
<?php 在non
-WSDL模式中,想WSDL模式一样首先实现greet函数的功能,但是函数实现的方式跟WSDL模式E稍有所不同。在non-WSDL模式中,我们?
返回一个SoapParam对象作ؓ响应Q而不是一个数l。创建服务时Q第一个参数设为nullQ说明没有提供WSDLQ接着传递一个选项作ؓ参数Q这
个选项参数是服务的URI。最后像WSDL模式一栯用剩下的Ҏ?br />
<?php 在non-WSDL模式中,因ؓ没有使用WSDLQ传递了一个包含服务所在位|和服务URI的参数数l作为参数。然后象WSDL模式中一栯用__soapCall()ҎQ但是用了SoapParamcȝ指定格式打包参数。返回的l果获取greet中的响应?/p>
相关文章: l论
q个cȝ来?em>Web services。SoapClientcd以作为给定Web services的客L?br />
它有两种操作形式Q?br />
* WSDL 模式
* Non-WSDL 模式
在WSDL模式中,构造器可以使用WSDL文g名作为参敎ͼq从WSDL中提取服务所使用的信息?br />
non-WSDL模式中用参数来传递要使用的信息。这个类有许多可以用来用服务的有用的方法。其中SoapClient::__soapCall()是最重要的。这个方法可以用来调用服务中的某个操作?br />
SoapServerc?/strong>
q个cd以用来提?em>Web services?
与SoapClientcMQSoapServer也有两种操作模式QWSDL模式和non-WSDL模式。这两种模式的意义跟
SoapClient的两U模式一栗在WSDL模式中,服务实现了WSDL提供的接口;在non-WSDL模式中,参数被用来管理服务的行ؓ?br />
在SoapServercȝ众多Ҏ中,有三个方法比较重要。它们是SoapServer::setClass()QSoapServer::addFunction()和SoapServer::handle()?br />
SoapServer::setClass()Ҏ讑֮用来实现Web Service的类。SoapServer::setClass所讑֮的类中的所有公共方法将成ؓWeb Services的操?operation)?br />
SoapServer::addFunction()Ҏ用来d一个或多个作ؓWeb Services操作(operation)的函数?br />
SoapServer:: handle()Ҏ指示Web
Service脚本开始处理进入的h。Web
Service脚本是用PHP脚本写的一个或多个SoapServer对象的实例。尽你可以有不止一个的SoapServer对象Q但通常的习惯是一?
脚本只拥有一个SoapServer实例。在调用SoapServer::handle()Ҏ之前QWeb
Service脚本会用设|在SoapServer对象实例上的M信息来处理进入的h和输出的相应?/p>
SoapFaultc?/strong>
q个cMExceptioncȝ承而来Q可以用来处理错误。SoapFault实例可以抛出或获取Soap错误的相关信息ƈ按程序员的请求处理?br />
SoapHeaderc?/strong>
q个cd以用来描qSOAP headers。它只是一个只包含构造器Ҏ的数据容器?br />
SoapParamc?/strong>
SoapParam也是一个只包含构造器Ҏ的数据容器。这个方法可以用来描qC递给Web services操作的参数。在non-WSDL模式中这是一个很有用的类Q可以用来传递所期望格式的参C息?br />
SoapVarc?/strong>
SoapVar也是一个只包含构造器的低U类Q与SoapHeader和SoapParamcȝ伹{这个类可以用来l一?em>Web services操作传递编码参数。这个类对non-WSDL中传递类型信息是非常有用的?/p>
WSDL VS. non-WSDL模式
Web Services有两U实现模式:契约先行(Contract first)模式和代码先?Code first)模式?br />
契约先行模式使用了一个用XML定义的服务接口的WSDL文g。WSDL文g定义了服务必d现或客户端必M用的接口。SoapServer和SoapClient的WSDL模式基于这个概c?br />
?
代码先行模式中,首先要先写出实现服务的代码。然后在大多数情况下Q代码会产生一个契U,换种说法Q一个WSDL。接着客户端在使用服务的时候就可以使用
那个WSDL来获得服务的接口。尽如此,PHP5的扩展ƈ没有从代码输Z个WSDL的规定,考虑到这U情况,可以在non-WSDL模式下?
SoapServer和SoapClient?br />
SOAP扩展与Hello World
q一节介l如何用WSDL模式和non-WSDL模式来实现服务和客户端。相对而言Q用WSDL模式来实现服务和客户端会比较ҎQ假定已l有一个定义了接口的WSDL文g。因此这一节会先介l如何用WSDL模式实现一?em>Web Service?br />
在这个Hello World例子的服务中有一个被命名为greet的操作。这个操作有一个字W串形式的名字ƈq回一个字W串形式的greeting。所用到的WSDL如下Q?/p>
xmlns:impl='http://wso2.org/wsf/php/helloService'
xmlns:intf='http://wso2.org/wsf/php/helloService'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns:wsdlsoap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://wso2.org/wsf/php/helloService'>
<wsdl:types>
<schema elementFormDefault='qualified'
xmlns:impl='http://wso2.org/wsf/php/helloService'
xmlns:intf='http://wso2.org/wsf/php/helloService'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace='http://wso2.org/wsf/php/helloService' >
<element name='greet'>
<complexType>
<sequence>
<element name='name' type='xsd:string' />
</sequence>
</complexType>
</element>
<element name='greetResponse'>
<complexType>
<sequence>
<element name='greetReturn' type='xsd:string' />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name='greetRequest'>
<wsdl:part name='parameters' element='impl:greet' />
</wsdl:message>
<wsdl:message name='greetResponse'>
<wsdl:part name='parameters' element='impl:greetResponse' />
</wsdl:message>
<wsdl:portType name='helloService'>
<wsdl:operation name='greet'>
<wsdl:input name='greetRequest' message='impl:greetRequest' />
<wsdl:output name='greetResponse' message='impl:greetResponse' />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name='helloServiceSoapBinding' type='impl:helloService'>
<wsdlsoap:binding transport='http://schemas.xmlsoap.org/soap/http' style='document' />
<wsdl:operation name='greet'>
<wsdlsoap:operation soapAction='helloService#greet' />
<wsdl:input name='greetRequest'>
<wsdlsoap:body use='literal' />
</wsdl:input>
<wsdl:output name='greetResponse'>
<wsdlsoap:body use='literal' />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name='helloService'>
<wsdl:port binding='impl:helloServiceSoapBinding' name='helloService'>
<wsdlsoap:address location='http://localhost/hello/hello_service_wsdl.php' />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
下面是WSDL模式的服务所使用的SOAP扩展API代码Q?/p>
function greet($param) {
$retval = 'Hello '.$param->name;
$result = array('greetReturn' => $retval);
return $result;
}
$server = new SoapServer('hello.wsdl');
$server->addFunction('greet');
$server->handle();
?>
WSDL模式客户?/strong>
客户端代码如?/p>
try {
$client = new SoapClient('hello.wsdl');
$result = $client->__soapCall('greet', array(array('name' => 'Sam')));
printf("Result = %s", $result->greetReturn);
} catch (Exception $e) {
printf("Message = %s",$e->__toString());
}
?>
当你上q的PHP脚本攑֜你web服务器目录下的文档中Qƈ利用WEB览器或在PHP解析器的命o行调用脚本,客户端发送一个SOAPh到服务端脚本Q服务端向客户端发送一个SOAP响应来响应客L的请求?br />
下面是客L所发送的SOAPhQ?/p>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://wso2.org/wsf/php/helloService">
<SOAP-ENV:Body>
<ns1:greet>
<ns1:name>Sam</ns1:name>
</ns1:greet>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://wso2.org/wsf/php/helloService">
<SOAP-ENV:Body>
<ns1:greetResponse>
<ns1:greetReturn>Hello Sam</ns1:greetReturn>
</ns1:greetResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
non-WSDL模式服务?/strong>
function greet($param) {
$retval = 'Hello '.$param;
return new SoapParam($retval, 'greetReturn');
}
$server = new SoapServer(null, array('uri' => 'http://wso2.org/wsf/php/helloService'));
$server->addFunction('greet');
$server->handle();
?>
non-WSDL模式客户?/strong>
try {
$client = new SoapClient(null,
array('location' => 'http://localhost/hello/hello_service_nonwsdl.php',
'uri' => 'http://wso2.org/wsf/php/helloService'));
$result = $client->__soapCall('greet', array(new SoapParam('Sam', 'name'))); printf("Result = %s", $result);
} catch (Exception $e) {
printf("Message = %s",$e->__toString());
}
?>
q篇文章介绍了SOAP扩展Q可以在PHP中通过它来提供和?em>Web
Services。PHP扩展的强Ҏ它的单和快速。用C写的SOAP扩展来运行服务端和客L是非常简单的。虽然SOAP扩展在处理一些简单的
Web Services时很有用Q但是当用它来处理所有的Web Services时就表现出它的局限性?WSO
WSF/PHP是Z弥补PHP扩展的缺陯开发的Q它是开源的Q可以实现SOAPcM的功能ƈ且支持MTOMQWS-AddressingQWS-
Security和WS-RelaiableMessaging。WSO2 WSF/PHP
支持与SOAP扩展cM的API。我们正计划API打包h提供跟SOAP扩展一LAPIQ会用C来写?/p>