SOAP Trivia: SOAP used to stand for Simple Object Access Protocol, but in SOAP 1.2 t was dropped and now SOAP means SOAP.
About SOAP
SOAP is anouther tool for communication between two systems, allowing you to define and exchange data types in request or responce. It is interlinked to XML as all messages are sent in a SOAP envelope that is an XML wrapper, but creating XML for this wrapper is tedious therefore PHP5 simplifies this process with its SOAP extension making use of Servers and Clients easy.
A SOAP Web service is defined by using a Web Service Description Language (WSDL, pronounced “whisdl”) document. This, in turn, is yet another XML document that describes the function calls made available by a Web service, as well as any specialized data types needed by it.
SOAP also supports Remote Procedure Call (RPC). this allows you to execute a procedure on another address space (a different local computer). RPC may be referred to as remote invocation or remote method invocation.
Using SOAP Web Services
As we mentioned SOAP is used for communication between two computers, in the example bellow we will talk to google. SoapClient gives us such ability, the first step is to tell SOAP the path to a WSDL fle.
<?php
try{
$client = new SoapClient('http://api.google.com/GoogleSearch.wsdl');
$results = $client->doGoogleSearch($key, $query, 0, 10, FALSE, '',FALSE, '', '', '');
foreach ($results->resultElements as $result){
echo '<a href="'.htmlentities($result->URL).'">';
echo htmlentities($result->title, ENT_COMPAT, 'UTF-8');
// echo htmlentities($result->title);
echo '</a><br/>';
}
}
catch (SoapFault $e){
echo $e->getMessage();
}
?>
This creates a new SOAP client using Google’s WSDL file. SoapClient uses theWSDL file to construct an object mapped to the methods defined by the web service; thus, $client will now provide the methods doGetCachedPage(), doSpellingSuggestion(), and doGoogleSearch(). In our example, the script invokes the doGoogleSearch() method to return a list of search results. If SoapClient encounters any problems, it will throw an exception, which we can trap as explained in the Object-oriented Programming in PHP chapter). The constructor of the SOAPClient class also accepts, as an optional second parameter, an array of options that can alter its behaviour; for example, you can change the way data is encoded, or whether the entire SOAP exchange is to be compressed, and so on.