Developing Web Services Using PHP
Pages: 1, 2, 3, 4
function getCatalogEntry($catalogId) {
if($catalogId=='catalog1')
return "<HTML> … </HTML>";
elseif ($catalogId='catalog2')
return "<HTML>…</HTML>";
}
The WSDL cache is enabled by default. Disable the WSDL cache by setting the soap.wsdl_cache_enabled configuration option to 0.
ini_set("soap.wsdl_cache_enabled", "0");
Create a SoapServer object using the catalog.wsdl WSDL.
$server = new SoapServer("catalog.wsdl");
Add the getCatalogEntry function to the SoapServer object using the addFunction() method. The SOAP web service provides the getCatalogEntry operation.
$server->addFunction("getCatalogEntry");
Handle a SOAP request.
$server->handle();
The soap-server.php script is listed below.
<?php
function getCatalogEntry($catalogId) {
if($catalogId=='catalog1')
return "<HTML>
<HEAD>
<TITLE>Catalog</TITLE>
</HEAD
<BODY>
<p> </p>
<table border>
<tr><th>CatalogId</th>
<th>Journal</th><th>Section
</th><th>Edition</th><th>
Title</th><th>Author</th>
</tr><tr><td>catalog1</td>
<td>IBM developerWorks</td><td>
XML</td><td>October 2005</td>
<td>JAXP validation</td>
<td>Brett McLaughlin</td></tr>
</table>
</BODY>
</HTML>";
elseif ($catalogId='catalog2')
return "<HTML>
<HEAD>
<TITLE>Catalog</TITLE>
</HEAD
<BODY>
<p> </p>
<table border>
<tr><th>CatalogId</th><th>
Journal</th><th>Section</th>
<th>Edition</th><th>Title
</th><th>Author
</th></tr><tr><td>catalog1
</td><td>IBM developerWorks</td>
<td>XML</td><td>July 2006</td>
<td>The Java XPath API
</td><td>Elliotte Harold</td>
</tr>
</table>
</BODY>
</HTML>";
}
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("catalog.wsdl");
$server->addFunction("getCatalogEntry");
$server->handle();
?>
In the next section, I will create a SOAP client to send a request to the SOAP server.
Creating a SOAP Client
Create a PHP script, soap-client.php, in the C:\Apache2\htdocs directory. In the PHP script, create a SOAP client using the SoapClient class. The WSDL document, catalog.wsdl, is specified as an argument to the SoapClient constructor. The WSDL document specifies the operations that are available to the SOAP client.
$client = new SoapClient("catalog.wsdl");
Specify the catalogId for which a catalog entry is to be retrieved. Invoke the getCatalogEntry method of the SOAP web service.
$catalogId='catalog1'; $response = $client->getCatalogEntry($catalogId);
Output the response to the browser.
echo $response;
The soap-client.php script is listed below.
<?php
$client = new SoapClient("catalog.wsdl");
$catalogId='catalog1';
$response = $client->getCatalogEntry($catalogId);
echo $response;
?>
Invoke the soap-client.php PHP script with the URL http://localhost/soap-client.php.The catalog entry for the catalog1 catalogId gets output as shown in Figure 1.

Figure 1. Invoking the SOAP client