??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美午夜电影在线观看 ,亚洲精品视频自拍,亚洲成av人片在线观看wwwhttp://www.aygfsteel.com/javagrass/category/36873.htmlzh-cnSun, 21 Dec 2008 02:32:43 GMTSun, 21 Dec 2008 02:32:43 GMT60PHP SOAP扩展 http://www.aygfsteel.com/javagrass/archive/2008/12/21/247583.html墙头?/dc:creator>墙头?/author>Sun, 21 Dec 2008 01:19:00 GMThttp://www.aygfsteel.com/javagrass/archive/2008/12/21/247583.htmlhttp://www.aygfsteel.com/javagrass/comments/247583.htmlhttp://www.aygfsteel.com/javagrass/archive/2008/12/21/247583.html#Feedback0http://www.aygfsteel.com/javagrass/comments/commentRss/247583.htmlhttp://www.aygfsteel.com/javagrass/services/trackbacks/247583.htmlWeb services。换句话_PHP开发者可以利用这个PHP扩展来写他们自己的Web servicesQ也可以写一些客L来用给定的Web services?br />
PHP5中的q个SOAP扩展目的是ؓ了实现PHP?em>Web services的支持。与其它实现PHP对Web services的支持的Ҏ不同QSOAP扩展是用C写的Q因此它比其它方法具有速度优势?br />
SOAP扩展支持以下规范?br />
* SOAP 1.1
* SOAP 1.2
* WSDL 1.1

SOAP扩展主要用来处理RPC形式?em>Web services。不q,你也可以使用文本形式的WSDL文g配合WSDL模式的服务端和客L?br />
q个扩展使用 GNOME XML库来处理XML?br />
扩展中的c?br /> q个扩展实现?个类。其中有三个高的类Q它们的Ҏ很有用,它们?SoapClientQSoapServer和SoapFault。另外三个类除了构造器外没有其它别的方法,q三个是低的类Q它们是SoapHeaderQSoapParam和SoapVar?br />

SoapClientc?/strong>

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>

<wsdl:definitions
    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模式服务

下面是WSDL模式的服务所使用的SOAP扩展API代码Q?/p>

<?php
function greet($param) {
    $retval = 'Hello '.$param->name;
    $result = array('greetReturn' => $retval);
    return $result;
}

$server = new SoapServer('hello.wsdl');
$server->addFunction('greet');
$server->handle();
?>

在这个服务的实现q程中,函数实现了WSDL所定义的服务操作greetQgreet操作有一个WSDL指定的参敎ͼ按照greet操作的语义,q个参数是一个用L名字。最后handle调用了触发处理请求的服务对象?/p>


WSDL模式客户?/strong>

客户端代码如?/p>

<?php
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首先创Z个用WSDL文g作参数的SoapClient实例。接着__soapCall()调用作ؓ参数传入它的操作Q也是greet和传入操作的参数?br /> h和响?br />
当你上q的PHP脚本攑֜你web服务器目录下的文档中Qƈ利用WEB览器或在PHP解析器的命o行调用脚本,客户端发送一个SOAPh到服务端脚本Q服务端向客户端发送一个SOAP响应来响应客L的请求?br />
下面是客L所发送的SOAPhQ?/p>

<?xml version="1.0" encoding="UTF-8"?>  
   <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>

下面是服务端响应上诉h而发送的SOAP响应Q?/p>

<?xml version="1.0" encoding="UTF-8"?>
   <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>

上面的SOAP消息都是利用WSDL模式的服务端和客L来获取的。也可以利用non-WSDL模式的服务端和客L来生与上面相同的SOAP消息。但是,PHP代码必须有一Ҏ变。下一节会说明如何使用non-WSDL模式?br />
non-WSDL模式服务?/strong>

<?php
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模式中,想WSDL模式一样首先实现greet函数的功能,但是函数实现的方式跟WSDL模式E稍有所不同。在non-WSDL模式中,我们? 返回一个SoapParam对象作ؓ响应Q而不是一个数l。创建服务时Q第一个参数设为nullQ说明没有提供WSDLQ接着传递一个选项作ؓ参数Q这 个选项参数是服务的URI。最后像WSDL模式一栯用剩下的Ҏ?br />
non-WSDL模式客户?/strong>

<?php
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());
}
?>

在non-WSDL模式中,因ؓ没有使用WSDLQ传递了一个包含服务所在位|和服务URI的参数数l作为参数。然后象WSDL模式中一栯用__soapCall()ҎQ但是用了SoapParamcȝ指定格式打包参数。返回的l果获取greet中的响应?/p>

相关文章:

PHP代码的优与劣

如何用PHP和mysql创徏一个ShoutBox

PHP的文件包?/a>

PHP的正则表辑ּ处理函数ȝ分析

PHP文g上传实例详解

l论
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>

]]>
վ֩ģ壺 ˳ƽ| | | ɽ| | | | ׿| | ǭ| ڶ| ӥ̶| ̨| ǹ| ɽ| ƺ| ˻| | | Դ| | | | ۲| ̩| | | | | | | | | ̨| | ¬| | | | | |