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 Zend_Soap_Client
class.
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.
Note: There are several options that can be customised when instantiating Zend_Soap_Client, but to keep things simple I won't cover them here. You can read more athttp://framework.zend.com/manual/en/zend.soap.client.html.
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!
Listing 5 Instantiating the Zend_Soap_Client class and making calls to the web service (client.php)
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->getDate()
and $client->getAgeString()
. When these functions are called, internally Zend_Soap_Client
is communicating with the SOAP server specified in $url
.
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!
No comments:
Post a Comment