Example of using soap in PHP
PHP uses soap in two ways.
1. Use the wsdl File
Server.
<? Php
{
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 ();
?>
Resource Description file, which can be generated using zend studio. It is actually an xml file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Wsdl: definitions xmlns: soap = "The 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/">
<Wsdl: types>
<Xsd: schema targetNamespace = "http: // localhost/interface/">
<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: element name = "Add">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "in" type = "xsd: int"> </xsd: element>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "AddResponse">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "out" type = "xsd: int"> </xsd: element>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
</Xsd: schema>
</Wsdl: types>
<Wsdl: message name = "AddRequest"> <wsdl: part name = "a" type = "xsd: int"> </wsdl: part>
<Wsdl: part name = "B" type = "xsd: int"> </wsdl: part>
</Wsdl: message>
<Wsdl: message name = "AddResponse">
<Wsdl: part name = "c" type = "xsd: int"> </wsdl: part>
</Wsdl: message>
<Wsdl: portType name = "TestSoap"> <wsdl: operation name = "Add">
<Wsdl: input message = "tns: AddRequest"> </wsdl: input>
<Wsdl: output message = "tns: AddResponse"> </wsdl: output>
</Wsdl: operation>
</Wsdl: portType>
<Wsdl: binding name = "soapSOAP" type = "tns: TestSoap">
<Soap: binding style = "document"
Transport = "http://schemas.xmlsoap.org/soap/http"/>
<Wsdl: operation name = "Add">
<Soap: operation soapAction = "http: // localhost/interface/Add"/>
<Wsdl: input>
<Soap: body use = "literal"
Namespace = "http: // localhost/interface/"/>
</Wsdl: input>
<Wsdl: output>
<Soap: body use = "literal"
Namespace = "http: // localhost/interface/"/>
</Wsdl: output>
</Wsdl: operation>
</Wsdl: binding>
<Wsdl: service name = "TestSoap">
<Wsdl: port binding = "tns: soapSOAP" name = "soapSOAP">
<Soap: address location = "http: // localhost/interface/myservice. php"/>
</Wsdl: port>
</Wsdl: service>
</Wsdl: definitions>
Client call
<? Php
$ Soap = new SoapClient ('HTTP: // localhost/interface/soap. wsdl ');
Echo $ soap-> Add (1, 2 );
?>
2. Do not use the wsdl File
Server
<? Php
{
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 ();
?>
Client <? Php
Try {
$ Soap = new SoapClient (null, array (
"Location" => "http: // localhost/interface/soap. php ",
"Uri" => "abcd", // The resource descriptor server and client must correspond
"Style" => SOAP_RPC,
"Use" => SOAP_ENCODED
));
Echo $ soap-> Add (1, 2 );
} Catch (Exction $ e ){
Echo print_r ($ e-> getMessage (), true );
}
?>