用 wsdl 文件

服务端

 1class service
 2{
 3  public function HelloWorld()
 4   {
 5      return  "Hello";
 6   }
 7  public  function Add($a,$b)
 8   {
 9      return $a+$b;
10   }
11}
12$server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));
13$server->setClass("service");
14$server->handle();

资源描述文件,可以用工具(zend studio)生成。其实就是一个 xml 文件。

 1<?xml version="1.0" encoding="UTF-8"?>
 2<wsdl: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/">
 3  <wsdl:types>
 4    <xsd:schema targetNamespace="http://localhost/interface/">
 5      <xsd:element name="HelloWorld">
 6        <xsd:complexType>
 7          <xsd:sequence>
 8            <xsd:element name="in" type="xsd:string"/>
 9          </xsd:sequence>
10        </xsd:complexType>
11      </xsd:element>
12      <xsd:element name="HelloWorldResponse">
13        <xsd:complexType>
14          <xsd:sequence>
15            <xsd:element name="out" type="xsd:string"/>
16          </xsd:sequence>
17        </xsd:complexType>
18      </xsd:element>
19      <xsd:element name="Add">
20      	<xsd:complexType>
21      		<xsd:sequence>
22      			<xsd:element name="in" type="xsd:int"></xsd:element>
23      		</xsd:sequence>
24      	</xsd:complexType>
25      </xsd:element>
26      <xsd:element name="AddResponse">
27      	<xsd:complexType>
28      		<xsd:sequence>
29
30      			<xsd:element name="out" type="xsd:int"></xsd:element>
31      		</xsd:sequence>
32      	</xsd:complexType>
33      </xsd:element>
34    </xsd:schema>
35
36</wsdl:types>
37<wsdl:message name="AddRequest"> <wsdl:part name="a" type="xsd:int"></wsdl:part>
38<wsdl:part name="b" type="xsd:int"></wsdl:part>
39</wsdl:message>
40<wsdl:message name="AddResponse">
41<wsdl:part name="c" type="xsd:int"></wsdl:part>
42</wsdl:message>
43<wsdl:portType name="TestSoap"> <wsdl:operation name="Add">
44<wsdl:input message="tns:AddRequest"></wsdl:input>
45<wsdl:output message="tns:AddResponse"></wsdl:output>
46</wsdl:operation>
47</wsdl:portType>
48<wsdl:binding name="soapSOAP" type="tns:TestSoap">
49<soap:binding style="document"
50transport="http://schemas.xmlsoap.org/soap/http" />
51<wsdl:operation name="Add">
52<soap:operation soapAction="http://localhost/interface/Add" />
53<wsdl:input>
54<soap:body use="literal"
55namespace="http://localhost/interface/" />
56</wsdl:input>
57<wsdl:output>
58<soap:body use="literal"
59namespace="http://localhost/interface/" />
60</wsdl:output>
61</wsdl:operation>
62</wsdl:binding>
63<wsdl:service name="TestSoap">
64<wsdl:port binding="tns:soapSOAP" name="soapSOAP">
65<soap:address location="http://localhost/interface/myservice.php"/>
66</wsdl:port>
67</wsdl:service>
68</wsdl:definitions>

客户端

1$soap = new SoapClient('http://localhost/interface/soap.wsdl');
2echo $soap->Add(1,2);

不用 wsdl 文件

服务器端

 1class service
 2{
 3  public function HelloWorld()
 4   {
 5      return  "Hello";
 6   }
 7  public  function Add($a,$b)
 8   {
 9      return $a+$b;
10   }
11}
12$server=new SoapServer(null,array('uri' => "abcd"));
13$server->setClass("service");
14$server->handle();

客户端

 1try{
 2	$soap = new SoapClient(null,array(
 3			"location" => "http://localhost/interface/soap.php",
 4			"uri"      => "abcd",  //资源描述符服务器和客户端必须对应
 5			"style"    => SOAP_RPC,
 6			"use"      => SOAP_ENCODED
 7			   ));
 8
 9    echo $soap->Add(1,2);
10
11}catch(Exction $e){
12	echo print_r($e->getMessage(),true);
13}