Zend Framework 101: Zend_Soap
Handle SOAP requests
Next we must handle any requests to the web service. This involves filling in the "
else
" block from the previous listing.
To achieve this, we use the
Zend_Soap_Server
class. Just like Zend_Soap_AutoDiscover
, it needs to the know the name of the class that holds the web service methods.
Additionally, when we instantiate
Zend_Soap_Server
we need to pass to it the URL of the web service. This is so the server knows which methods to handle, as well its data types (Zend_Soap_Server
doesn't do any auto-discovery of the class you pass to it with setClass()
).
You can either hard-code the URL of the WSDL file, or you can auto-generate its location as in Listing 4.
Note: This URL auto-generator is somewhat simple. You may need to account for different port names or for using HTTPS.
Listing 4 shows how to instantiate the
Zend_Soap_Server
class, set the WSDL, set the PHP class and then handle the request.
Listing 4 Handle SOAP requests (webservice.php)
require_once('MyWebService.php'); if ($_SERVER['QUERY_STRING'] == 'wsdl') { require_once('Zend/Soap/AutoDiscover.php'); $auto = new Zend_Soap_AutoDiscover(); $auto->setClass('MyWebService'); $auto->handle(); } else { require_once('Zend/Soap/Server.php'); $wsdl = sprintf( 'http://%s%s?wsdl', $_SERVER['HTTP_HOST'], $_SERVER['SCRIPT_NAME'] ); $soapServer = new Zend_Soap_Server($wsdl); $soapServer->setClass('MyWebService'); $soapServer->handle(); }
That's all there is to it. The next step is to actually consume the web service, as covered in the next section.
No comments:
Post a Comment