SOAP Introduction
- SOAP stands for Simple Object Access Protocol.
- SOAP is a protocol for accessing web services.
- SOAP is based on XML.
==========================================================================
WSDL
WSDL stands for Web Services Description Language
WSDL is an XML-based language for describing Web services.
WSDL is a W3C recommendation
==========================================================================
What is SOAP?
- SOAP stands for Simple Object Access Protocol
- SOAP is a communication protocol
- SOAP is for communication between applications
- SOAP is a format for sending messages
- SOAP communicates via Internet
- SOAP is platform independent
- SOAP is language independent
- SOAP is based on XML
- SOAP is simple and extensible
- SOAP allows you to get around firewalls
- SOAP is a W3C recommendation
==========================================================================
Why SOAP?
It is important for application development to allow Internet communication between programs.
SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
Today's applications communicate using Remote Procedure Calls (RPC) between objects like DCOM and CORBA, but HTTP was not designed for this. RPC represents a compatibility and security problem; firewalls and proxy servers will normally block this kind of traffic.
A better way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.
==========================================================================
An Envelope element that identifies the XML document as a SOAP message
A Header element that contains header information
A Body element that contains call and response information
A Fault element containing errors and status information
==========================================================================
A SOAP message MUST be encoded using XML
A SOAP message MUST use the SOAP Envelope namespace
A SOAP message MUST use the SOAP Encoding namespace
A SOAP message must NOT contain a DTD reference
A SOAP message must NOT contain XML Processing Instructions
==========================================================================
<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 provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
Today's applications communicate using Remote Procedure Calls (RPC) between objects like DCOM and CORBA, but HTTP was not designed for this. RPC represents a compatibility and security problem; firewalls and proxy servers will normally block this kind of traffic.
A better way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.
==========================================================================
SOAP Building Blocks
A SOAP message is an ordinary XML document containing the following elements:An Envelope element that identifies the XML document as a SOAP message
A Header element that contains header information
A Body element that contains call and response information
A Fault element containing errors and status information
==========================================================================
Syntax Rules
Here are some important syntax rules:A SOAP message MUST be encoded using XML
A SOAP message MUST use the SOAP Envelope namespace
A SOAP message MUST use the SOAP Encoding namespace
A SOAP message must NOT contain a DTD reference
A SOAP message must NOT contain XML Processing Instructions
==========================================================================
Skeleton SOAP Message
<?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>
==========================================================================
Example : (Need to fetch the top goal scores of the tournament from footballpool)
This post will show a simple example of how to interpretate a WSDL file and a very simple, yet quick example of how to extract information from this file through PHP.
First of all please enable SOAP in your PHP configuration.
Find the methods and its required parameters webservice url using WSDL.
//$url = 'http://footballpool.dataaccess.eu/data/info.wso?wsdl';
/*
// for now lets concentrate on part of xml element from which we can fetch the information
<xs:element name="TopGoalScorers">
<xs:complexType>
<xs:sequence>
<xs:element name="iTopN" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
*/
//we now finally know that the method TopGoalScorers which we saw in the first WSDL fragment expects one parameter as input. This parameter is called iTopN and is of the type int.
//let fetch the information using PHP using SoapClient
$url = 'http://footballpool.dataaccess.eu/data/info.wso?wsdl';
$client = new SoapClient($url);
$result = $client->TopGoalScorers(array('iTopN'=>5));
//print the result in table
$array = $result->TopGoalScorersResult->tTopGoalScorer;
print "
<table border='2'>
<tr>
<th>Rank</th>
<th>Name</th>
<th>Goals</th>
</tr>
";
foreach($array as $k=>$v){
print "
<tr>
<td align='right'>" . ($k+1) . "</td>
<td>" . $v->sName . "</td>
<td align='right'>" . $v->iGoals . "</td>
</tr>";
}
print "</table>";
==========================================================================
Example: (Example for cyberlogic.gr SOAP webservice)
$soapUrl = "http://wl.filos.com.gr/services/WebService.asmx";
$namespace = "http://www.cyberlogic.gr/webservices/";
$action = "PlaceSearch";
// create the request XML
$request = new SimpleXMLElement('<PlaceSearchRequest/>');
$request->Username = 'SERUNCD';
$request->Password = 'TA78UNC';
$request->PlaceType = 'Cities';
$request->Language = 'en';
// create the soap variable
$var = new DOMDocument();
$var->appendChild($var->createElementNS($namespace, 'xml'));
$var->documentElement->nodeValue = $request->asXML();
$soapVar = new SoapVar($var->saveXML($var->documentElement), XSD_ANYXML);
// create soap client in non-WSDL mode
$client = new SoapClient(null, array(
'location' => $soapUrl,
'uri' => $namespace
));
try {
// place soap call
$result = $client->__soapCall(
$action, array($soapVar), array('soapaction' => "$namespace$action")
);
// process result
foreach ($result->PlaceSearch->Response->Cities->City as $i => $City) {
printf("#%03d: %s\n", $i, $City->CityName);
}
} catch (Exception $e) {
echo $e->getMessage(), "\n";
echo $client->__getLastRequest(); // set 'trace' => true SoapClient option
}
==========================================================================
Example: Using CURL
$soap_request = 'xml='.urlencode(trim('
<?xml version="1.0" encoding="utf-8"?>
<PlaceSearchRequest>
<Username>SERUNCD</Username>
<Password>TA78UNC</Password>
<PlaceType>Cities</PlaceType>
<Language>en</Language>
</PlaceSearchRequest>
'));
$header = array(
'POST /services/WebService.asmx HTTP/1.1',
'Host: wl.filos.com.gr',
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, 'http://wl.filos.com.gr/services/WebService.asmx/PlaceSearch');
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$output = curl_exec($soap_do);
$info = curl_getinfo($soap_do);
if (curl_exec($soap_do) === FALSE)
{
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
}
else
{
curl_close($soap_do);
}
echo 'The server responded: '.$info['http_code']."\n";
echo 'Output: '.$output;
echo "\n";
<?xml version="1.0" encoding="utf-8"?>
<PlaceSearchRequest>
<Username>SERUNCD</Username>
<Password>TA78UNC</Password>
<PlaceType>Cities</PlaceType>
<Language>en</Language>
</PlaceSearchRequest>
'));
$header = array(
'POST /services/WebService.asmx HTTP/1.1',
'Host: wl.filos.com.gr',
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, 'http://wl.filos.com.gr/services/WebService.asmx/PlaceSearch');
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$output = curl_exec($soap_do);
$info = curl_getinfo($soap_do);
if (curl_exec($soap_do) === FALSE)
{
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
}
else
{
curl_close($soap_do);
}
echo 'The server responded: '.$info['http_code']."\n";
echo 'Output: '.$output;
echo "\n";
==========================================================================
Soap useful more exampleshttps://help.exacttarget.com/en/technical_library/web_service_guide/getting_started_developers_and_the_exacttarget_api/connecting_to_the_api_using_php/
https://developer.decibel.net/sample-code-soap-php
Comments
Post a Comment