A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework: Zend_Soap / Create a Web Service

Here is a quick tutorial on how to create your own web service using Zend Framework.

Web services are software systems designed to support interoperable machine-to-machine interaction over a network. Nowadays if you want to connect external systems, you probably want or have to use web services. What I will discuss here is how to get your own SOAP web service up in minutes.

SOAP(Simple Object Access Protocol ) is probably the most used web service protocol today. It relies on XML as its message format, and it uses HTTP for message transmission. The SOAP server uses WSDL(Web Services Description Language ) to describe its services to external clients. WSDL is simply an XML-based language that provides a model for describing Web services.

Back in the old days you had to know a lot about SOAP and WSDL create a web service. Have a look at http://www.php.net/soap to see what I mean. Definitely not very good looking. Luckily Zend Framework has a nice component, Zend_Soap, that handles all the SOAP hard work you would be supposed to do.

The thing I like about this tutorial is that it uses the Zend Framework MVC. See the code below…

require_once realpath(APPLICATION_PATH .
'/../library/').'/Soaptest.php';
 
class SoapController extends Zend_Controller_Action
{
    //change this to your WSDL URI!
   private $_WSDL_URI="http://192.168.188.128:8081/soap?wsdl";
 
    public function indexAction()
    {    
        $this->_helper->viewRenderer->setNoRender();
 
        if(isset($_GET['wsdl'])) {
            //return the WSDL
            $this->hadleWSDL();
        } else {
            //handle SOAP request
            $this->handleSOAP();
        }
    }
 
    private function hadleWSDL() {
        $autodiscover = new Zend_Soap_AutoDiscover();
        $autodiscover->setClass('Soaptest');
        $autodiscover->handle();
    }
 
    private function handleSOAP() {
        $soap = new Zend_Soap_Server($this->_WSDL_URI); 
        $soap->setClass('Soaptest');
        $soap->handle();
    }
 
    public function clientAction() {
        $client = new Zend_Soap_Client($this->_WSDL_URI);
 
        $this->view->add_result = $client->math_add(11, 55);
        $this->view->not_result = $client->logical_not(true);
        $this->view->sort_result = $client->simple_sort(
       array("d" => "lemon", "a" => "orange",
             "b" => "banana", "c" => "apple"));
 
    }
 
}
 
/* 
 *
 * For The TEST CLASS 
 *
 */
class Soaptest {
    /**
     * Add method
     *
     * @param Int $param1
     * @param Int $param2
     * @return Int
     */
    public function math_add($param1, $param2) {
        return $param1+$param2; 
    }
 
    /**
     * Logical not method
     *
     * @param boolean $param1
     * @return boolean
     */
    public function logical_not($param1) {
        return !$param1;
    }
 
    /**
     * Simple array sort
     *
     * @param Array $array
     * @return Array
     */
    public function simple_sort($array) {
        asort($array);
        return $array;
    }
 
}

A full explanation of code can be found in Bogdan Abei’s blog.

Thanks to the author for sharing this ;-)

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>