soap的两种实现方式:WSDL和Non-WSDL
1.non-wsdl模式利用php的soap扩展实现:
server
<?php class HandleClass { public function test() { return '23434'; } } //uri必填 $soap_server = new SoapServer(null, array('uri'=>'http://soap/','location'=>'http://localhost/soap/server.php')); $soap_server->setClass('HandleClass'); $soap_server->handle();
client
<?php //location和uri必填 $soap_client = new SoapClient(null, array('location'=>'http://localhost/soap/server.php','uri'=>'http://soap/')); echo $soap_client->test();
output:
23434
2.wsdl利用php的soap扩展实现
server:
<?php class service { public function HelloWorld() { return "Hello"; } } $server = new SoapServer ('helloworld.wsdl'); $server->setClass ( "service" ); $server->handle ();
client:
<?php $client = new SoapClient ( "helloworld.wsdl" ); echo $client->HelloWorld ();
wsdl:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/helloworld/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloworld" targetNamespace="http://www.example.org/helloworld/"> <wsdl:types> <xsd:schema targetNamespace="http://www.example.org/helloworld/"> <xsd:element name="HelloWorld"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="HelloWorldResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="HelloWorldRequest"> </wsdl:message> <wsdl:message name="HelloWorldResponse"> <wsdl:part name="HelloWorldReturn" type="xsd:string"/> </wsdl:message> <wsdl:portType name="helloworld"> <wsdl:operation name="HelloWorld"> <wsdl:input message="tns:HelloWorldRequest"/> <wsdl:output message="tns:HelloWorldResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="helloworldSOAP" type="tns:helloworld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://www.example.org/helloworld/HelloWorld"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="helloworld"> <wsdl:port binding="tns:helloworldSOAP" name="helloworldSOAP"> <soap:address location="http://localhost/soap/wsdlServer.php"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
output:
Hello
BTW,zendstudio 10中可以通过插件的 方式支持wsdl的图形化编辑,见下图
3.non-wsdl模式利用zend的soap组件实现
handle:
<?php class Example_Manager { /** * Returns list of all products in database * * @return array */ public function getProducts() { $db = Zend_Registry::get ( 'Zend_Db' ); $sql = "SELECT * FROM products"; return $db->fetchAll ( $sql ); } /** * Returns specified product in database * * @param integer $id * @return array Exception */ public function getProduct($id) { if (! Zend_Validate::is ( $id, 'Int' )) { throw new Exception ( 'Invalid input' ); } $db = Zend_Registry::get ( 'Zend_Db' ); $sql = "SELECT * FROM products WHERE id = '$id'"; $result = $db->fetchAll ( $sql ); if (count ( $result ) != 1) { throw new Exception ( 'Invalid product ID: ' . $id ); } return $result; } }
server:
public function soapserverAction() { $this->getHelper('viewRenderer')->setNoRender(true); $server = new Zend_Soap_Server(null, array('uri' => 'http://zenddemo.com/index/soapserver', 'location'=> 'http://zenddemo.com/index/soapserver')); $server->setClass('Example_Manager'); $server->handle(); }
client:
public function soapclientAction() { $this->getHelper('viewRenderer')->setNoRender(true); $options = array( 'location' => 'http://zenddemo.com/index/soapserver', 'uri' => 'http://zenddemo.com/index/soapserver' ); try { $client = new Zend_Soap_Client(null, $options); $result = $client->getProducts(); print_r($result); } catch (SoapFault $s) { die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } }
4.wsdl模式用zend的soap组件实现
handle:同上
server:
public function soapwsdlserverAction() { $this->getHelper('viewRenderer')->setNoRender(true); $server = new Zend_Soap_AutoDiscover(); $server->setClass('Example_Manager'); $server->setUri('http://zenddemo.com/index/soapserver'); $server->handle(); }
client:
public function soapclientAction() { $this->getHelper('viewRenderer')->setNoRender(true); try { $client = new Zend_Soap_Client('http://zenddemo.com/index/soapwsdlserver'); $result = $client->getProducts(); print_r($result); } catch (SoapFault $s) { die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring); } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } }