Zend Framework 101: Zend_Soap
Consuming the Web Service
Now that we've created our web service (at the location
http://example.com/webservice.php), we can create a client script to consume the web service.
For this, we are going to use
Zend_Soap_Client class. You could use PHP's built-in
SoapClient class (or even use a programming language other than PHP), but since this is a series on the Zend Framework we'll use the
Consuming the service is simply a matter of instantiating
Zend_Soap_Client with the URL of the WSDL file (
http://example.com/webservice.php?wsdl) as the only argument, then calling the functions from the PHP class we created earlier as if they were local function calls.
Listing 5 shows how we make use of the web service calls. The PHP script can live on a completely different web server and URL to your server. After all, that is the point of web services!
require_once('Zend/Soap/Client.php'); $url = 'http://exsmple.com/webservice.php?wsdl'; $client = new Zend_Soap_Client($url); echo sprintf('Server Timestamp: %s', $client->getDate()); echo "<br />\n"; echo $client->getAgeString('Quentin', 28);
In this code you will see we call
$client->getAgeString(). When these functions are called, internally
Zend_Soap_Client is communicating with the SOAP server specified in
While this is a somewhat simplified implementation, it works, and it should demonstrate how to implement both the server and client aspects of a SOAP web service!