What is SOAP??
SOAP stands for Simple Object Access Protocol. SOAP uses HTTP request for intraction between programs. Because of HTTP request is supported by all servers and browsers, that's why it is a best way to communicate between applications.
SOAP best fit for you, if you want to communication between applications running on different Operating system, with different programming languages and technologies.
Structure of SOAP Message
This is based on XML, but it followed a standered schema. This is following format of SOAP message by striping out all data
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
soap:Envelope is optional and usually contains information about authentication and session handling.The SOAP protocol doesnt provide any built-in authentication, but allows to include it in this header tag.
soap:Body element is required, which contains the actual message, including method names and, in the case of a response, the return values of the method. The soap:Fault element is optional; if present, it holds any error messages or status information for the SOAP message and must be a child element of soap:Body.
Building a SOAP server
Follow the link http://sourceforge.net/projects/nusoap to download Nusoap package and unzip in project's root folder.
Now we are creating a server file. This server file handle all request and gives response as needed. The good thing about using NuSOAP is that it can create a WSDL file for you.
Create a file in project root named server.php with following code:
<?php
require "nusoap/lib/nusoap.php";
function getUser($user_id) {
if ($user_id == 1) {
return join(",", array(
"The is testing of SOAP service",
"This is user JHON"));
}
else {
return "No user belongs to this id";
}
}
$server = new soap_server();
$server->configureWSDL("userlist", "urn:userlist");
$server->register("getUser",
array("user_id" => "xsd:string"),
array("return" => "xsd:string"),
"urn:userlist",
"urn:userlist#getUser",
"rpc",
"encoded",
"Get user info by id");
$server->service($HTTP_RAW_POST_DATA);
- getUser is the function name
- array("user_id" => "xsd:string") defines the input argument to getUser and its data type
- array("return" => "xsd:string") defines the functions return value and its data type
- urn:userlist defines the namespace
- urn:userlist#getUser defines the SOAP action
- rpc defines the type of call (this could be either rpc or document)
- encoded defines the value for the use attribute (encoded or literal could be used)
- The last parameter is a documentation string that describes what the getUser function does
Now point your browser to http://yourwebroot/server.php?wsdl and youll see the brand new WSDL file created for you. Go ahead and copy that source and save it as its own file called server.wsdl and place it in you web directory.
Now you have server.wsdl in your project root, having code like
<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:userlist" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:userlist">
<types>
<xsd:schema targetNamespace="urn:userlist"
>
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="getUserRequest">
<part name="user_id" type="xsd:string" /></message>
<message name="getUserResponse">
<part name="return" type="xsd:string" /></message>
<portType name="userlistPortType">
<operation name="getUser">
<documentation>Get user info by id</documentation>
<input message="tns:getUserRequest"/>
<output message="tns:getUserResponse"/>
</operation>
</portType>
<binding name="userlistBinding" type="tns:userlistPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getUser">
<soap:operation soapAction="urn:userlist#getUser" style="rpc"/>
<input><soap:body use="encoded" namespace="urn:userlist" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>
<output><soap:body use="encoded" namespace="urn:userlist" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></output>
</operation>
</binding>
<service name="userlist">
<port name="userlistPort" binding="tns:userlistBinding">
<soap:address location="http://localhost/test/server_new.php"/>
</port>
</service>
</definitions>
If you point your browser to http://yourwebroot/server.php, you can see the complete listing of functions registerd in SOAP.
Building a SOAP Client
Create a file named client.php with following code:
<?php
require "nusoap/lib/nusoap.php";
$client = new nusoap_client("server.wsdl", true);
$error = $client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
$result = $client->call("getUser", array("user_id" => 1));
if ($client->fault) {
echo "<h2>Fault</h2><pre>";
print_r($result);
echo "</pre>";
}
else {
$error = $client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
else {
echo "<h2>Books</h2><pre>";
echo $result;
echo "</pre>";
}
}
echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
You can use both url for creating nusoap client and sending request. See follwoing:
$client = new nusoap_client("server.wsdl", true);
or
$client = new nusoap_client("http://yourwebroot/server.php?wsdl", true);
Now if you point your browser to url http://yourwebroot/client.php you can simply see response of your SOAP service.
Find attachment for complete code zipped.
Thanks
0 Comment(s)