Wednesday, 20 May 2009

Quickstart web services with SOAP and 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.

So without further ado, here's the code(discussing a Zend Framework component, the code presented here uses the Zend MVC, but you can use it without the Zend MVC):

This is the source code for the controller:
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"));


}

}

And the code for the Soaptest.php 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;
}

}

You can also download the full project here.

As you can see you don't have to write a lot of code to back up the web service.

Let's discuss the controller first, because there's where the “magic” happens. The index action handles two types of requests: the request for the WSDL, handled by the hadleWSDL() method and the actual SOAP request, handled by the handleSOAP() method.

You can go ahead and try to see how your WSDL looks by accessing http://URL_TO_WEB_SERVICE/soap?wsdl , where URL_TO_WEB_SERVICE is the URL where you have deployed the example. Now imagine that you would have to construct and maintain this yourself, by hand, as old school bearded guys would. Well you don't, because this is handled by Zend_Soap_AutoDiscover which will create the WSDL file for you. The only thing that Zend_Soap_AutoDiscover needs to know is the class you want to use for the web service. Also, because PHP is not strongly typed, you will have to put PHPDoc blocks, because SOAP needs to know what types you are using as parameters and what types you are returning. Have a look here if PHPDoc does not ring a bell .

The SOAP server is handled by the Zend_Soap_Server class, and all it needs is the class you intend to use for the web service, and the URI to your WSDL file. Remember when you checked out how the WSDL file looks? That's exactly the URI you will have to use. In the example you will have to put that into the $_WSDL_URI variable, defined in the SoapController.

That was the SOAP server. Simple, right? Now let's have some tests on the server by implementing a simple SOAP client. The client is handled by the Zend_Soap_Client class that is constructed in the same manner as the server class, it needs just the URI to the WSDL file. After you have constructed the client, you can access the methods defined by the SOAP server in the same way you would access the methods of an object. In the example above you have a simple class, called Soaptest, that defines three very simple methods. Feel free to change the class and test your own methods. While you are playing with the server, you might notice that the WSDL file is cached, so if you change something into the Soaptest.php file, you might not get the expected result. Just delete the cached WSDL file from /tmp/wsdl-* while you do your tests.

You definitely want to have a look at the Zend Framework documentation located here.

That was it, as promissed: your SOAP web service up in minutes.

17 comments:

phoenixHunter said...

thank you for your tutorial, but i have a problem with this. i get this error:
"Message: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost/ZfSoapTest/public/soap?wsdl' : Premature end of data in tag html line 2"

can u help me ?

bogdanalbei said...

Can you post the result of your wsdl URI, htp://..../soap?wsdl ? You can access that directly from the browser. I think something is wrong with your WSDL file.

phoenixHunter said...

I found the problem.
this is related to charset :)
thank you anyway.
this article was very helpful 4 me.

Nicolas said...
This post has been removed by the author.
Cesar Mancilla. said...

Great post¡¡

Andrius said...

If you try to stick on ZF modular structure, you can try my soap webservice example. It also makes automatic documetation and test forms by reflection methods. Try it here http://www.fordnox.com/blog/2009/05/zf-18-soap-server-example-module

Gabi Solomon said...

great article, thanks for taking the time to write it.
I am thinking of adding a Soap Service on a project i am curently working on and this come just in time

webnet said...

Muito bom o post, valeu!
Parabens

website design,web design said...
This post has been removed by a blog administrator.
Mazhar Hussain Shah said...
This post has been removed by a blog administrator.
MaurĂ­cio Vinicius de O. Santos said...

Great post :)

Pali said...

I am trying to consume a web service and it is giving me an error

Message: SOAP extension is not loaded.

I am new to Zend Framework and dont know how to proceed

bogdanalbei said...

For Pali: You have to enable the PHP SOAP extension first. See here http://uk3.php.net/manual/en/soap.installation.php

steven said...
This post has been removed by a blog administrator.
Ndiritu said...

Very good article to quickly get started with SOAP web services.

Per said...

HI, thanks for this quick start guide to SOAP.
I have set up your soapcontroller in an existing site and the wsdl is working, but I have a series of Zend_Loader errors when I point my browser to the soap service. (this results in my app throwing the error page and the xml presented is malformed).
I have tried placing the file Soaptest.php in different locations, everytime the same error, and in the last try I put it in the models directory and I should not need to require Soaptest.php in the SoapController, but even if I do I get the same error.
My the url to wsdl is
http://adamz.casa/soap?wsdl
(adamz.casa is on my local machine)
The xml response from the service contains no data but all the tags are there and the NameSpace is http://adamz.casa/index.php
Warning Zend_Loader::include_once(Array.php) and
Zend_loader::include_once(Int.php) cannot be found in path.
I notice that you use Zend_Application which my installation of Zend Framework does not have. Should the Soap service not work anyway?
I would be very grateful if you could point me in the right direction to fix this error.
I will need a SOAP service on my app by the end of next week.

bogdanalbei said...

Try getting the latest Zend Framework, if you don't have Zend_Application it means you miss the nice features in the latest releases. I would even suggest installing Zend Server Community Edition - http://www.zend.com/en/products/server/ . It comes preconfigured with the latest Zend Framework and everything you need to use it. The example posted should work out of the box, with minor changes of the actual WSDL url.

Post a Comment