PHP 使用soap有两种方式。 一、用wsdl文件 服务器端。 <?php class service { public function HelloWorld() { return "Hello"; } public function Add($a,$b) { return $a+$b; } } $server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2)); $server->setClass("service"); $server->handle(); ?> 资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/interface/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetnamespace="http://localhost/interface/"> <types> <schema targetnamespace="http://localhost/interface/"> <element name="HelloWorld"> <complextype> <sequence> <element name="in" type="xsd:string"></element> </sequence> </complextype> </element> <element name="HelloWorldResponse"> <complextype> <sequence> <element name="out" type="xsd:string"></element> </sequence> </complextype> </element> <element name="Add"> <complextype> <sequence> <element name="in" type="xsd:int"></element> </sequence> </complextype> </element> <element name="AddResponse"> <complextype> <sequence> <element name="out" type="xsd:int"></element> </sequence> </complextype> </element> </schema> </types> <message name="AddRequest"> <part name="a" type="xsd:int"></part> <part name="b" type="xsd:int"></part> </message> <message name="AddResponse"> <part name="c" type="xsd:int"></part> </message> <porttype name="TestSoap"> <operation name="Add"> <input message="tns:AddRequest"> <output message="tns:AddResponse"></output> </operation> </porttype> <binding name="soapSOAP" type="tns:TestSoap"> <binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></binding> <operation name="Add"> <operation soapaction="http://localhost/interface/Add"></operation> <input> <output> <body use="literal" namespace="http://localhost/interface/"></body> </output> </operation> </binding> <service name="TestSoap"> <port binding="tns:soapSOAP" name="soapSOAP"> <address location="http://localhost/interface/myservice.php"></address> </port> </service> </definitions> 客户端调用 <?php $soap = new SoapClient('http://localhost/interface/soap.wsdl'); echo $soap->Add(1,2); ?> 二、不用wsdl文件 服务器端 <?php class service { public function HelloWorld() { return "Hello"; } public function Add($a,$b) { return $a+$b; } } $server=new SoapServer(null,array('uri' => "abcd")); $server->setClass("service"); $server->handle(); ?> 客户端 <?php try{ $soap = new SoapClient(null,array( "location" => "http://localhost/interface/soap.php", "uri" => "abcd", //资源描述符服务器和客户端必须对应 "style" => SOAP_RPC, "use" => SOAP_ENCODED )); echo $soap->Add(1,2); }catch(Exction $e){ echo print_r($e->getMessage(),true); } ?>
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了webserver 两种模式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。