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

Create Web Service Methods

Our first step in creating a web service is to create a single PHP class that contains the methods you want people to be able to execute using SOAP. Each public method that belongs to this class is a method that can be executed your web service.

Tip: If you want public methods in your class that aren't auto-discovered (and thereby included in your WSDL), prefix the function name with a double-underscore. For instance, public function __myHiddenPublicFunction() { … }.

Each method in this class must be documented using the PhpDoc syntax, for the purposes of dynamically generating the WSDL file. At minimum this requires specifying input parameters (using @param) and the return data type (using @return). The reason for doing this is for generation of the WSDL file.

Let's being by creating a simple class with two methods. The first method (getDate()) will return the current date and time of the server. This method has no arguments and returns a string.

The second method (getAgeString()) accepts a name and an age and returns a nicely formatted string using these values. This method has a string argument and an integer argument and returns a string.

Listing 1 The PHP class with our web service methods (MyWebService.php)
<?php
    /**
     * Web service methods
     */
    class MyWebService
    {
        /**
         * Get the server date and time
         *
         * @return  string
         */
        public function getDate()
        {
            return date('c');
        }
 
 
        /**
         * Get a nicely formatted string of a person's age
         *
         * @param   string  $name   The name of a person
         * @param   int     $age    The age of a person
         * @return  string
         */
        public function getAgeString($name, $age)
        {
            return sprintf('%s is %d years old', $name, $age);
        }
    }
?>

In This Article