PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend Framework 101: Zend_Soap

Handle WSDL request

If the URL of your web service is http://example.com/webservice.php, then clients will expect to be able to retrieve the WSDL of your web service at http://example.com/webservice.php?wsdl. As such, we must handle requests to this URL.

When somebody hits the URL we will invoke the WSDL generator that comes with Zend_Soap in order to generate the WSDL file, then send it back to the client. The class we use to generate the WSDL is Zend_Soap_AutoDiscover. All that is required is that we pass the name of the class we created in the previous section (MyWebService) to the setClass() method of our Zend_Soap_AutoDiscover instance.

Listing 2 shows the code we use to achieve this. We will improve on this code shortly.

Listing 2 Auto-generating the WSDL file (webservice.php)
<?php
    require_once('MyWebService.php');
    require_once('Zend/Soap/AutoDiscover.php');
 
    $auto = new Zend_Soap_AutoDiscover();
    $auto->setClass('MyWebService');
    $auto->handle();
?>
Note: We need to still include our class in the script so that Zend_Soap_AutoDiscover knows about it.

Figure 1 shows how the WSDL file might look in your browser

Figure 1 Representation of WSDL in Firefox
Figure 1: Representation of WSDL in Firefox

If you were to now visit the webservice.php file in your browser you will see the generated WSDL file (which is simply an XML file that web service clients know how to interpret).

As noted previously, we only want to send the WSDL when the client includes a query string of “wsdl”. To check for this we use the $_SERVER['QUERY_STRING'] variable.

Listing 3 Ensuring the WSDL is only served when requested (webservice.php)
<?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 {
        // handle SOAP requests here
    }
?>

Now when you request the webservice.php file nothing will be shown in your browser, but if you request it as webservice.php?wsdl then the WSDL file will once again be displayed.

Another point of interest is that if you add a new method to the MyWebService class, it will automatically be added to the WSDL file, thereby making it available to all clients.

In This Article