Banner

Quickstart web services with SOAP and Zend Framework

(15 votes, average: 4.20 out of 5)
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.

Source - http://bogdan-albei.blogspot.com/2009/05/quickstart-web-services-with-soap-and.html

Trackback(0)
Comments (14)Add Comment
0
...
written by Hermes Lorenzo, April 28, 2010
Good work, very simple and useful.......thanks
0
...
written by Hermes Lorenzo, April 28, 2010
Good work, very easy and useful....thanks
0
...
written by Dawe, October 20, 2010
Don't use GET !! Use getParam is clener smilies/wink.gif

Use:
if( $this->getRequest()->getParam('wsdl') ) {
...
}
0
...
written by Dawe, October 20, 2010
Don't use GET !! Use getParam - is clener smilies/wink.gif

Use:
if( $this->getRequest()->getParam('wsdl') ) {
...
}
0
...
written by Dawe, October 20, 2010
Don't use GET !! Use getParam - is clenner smilies/wink.gif

Use:
if( $this->getRequest()->getParam('wsdl') ) {
...
}
0
...
written by Dawe, October 20, 2010
Don't use GET !! Use getParam - is clenner smilies/wink.gif

Use:
if( !is_null($this->getRequest()->getParam('wsdl')) ) {
...
}
0
...
written by ....., March 24, 2011
USE: if( $this->getRequest()->getParam('wdsl', false) )

Thats clean, when you use the default state from getParam
0
...
written by adesh kumar, July 26, 2011
Don't use GET !! Use getParam...........
you can use like this above condition
if(array_key_exists('wsdl', $this->getRequest()->getParams()))
{
}
0
...
written by AlexanderPlutov, September 23, 2011
You can find some additional information about zend_soap and web-services here http://plutov.by/post/web_service_soap
0
...
written by zend webservice newbie, September 29, 2011
Hi,
I am new to webservice. I copied the code and changed the wsdl url
I am not able to run the client action.
I am getting an error stating :
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://10.76.233.199:8083/soap?wsdl'
I executed the stated wsdl url in browser and it is working properly i.e. giving xml correctly.
I am not able to figure out the issue.

Any help would be gr8

Thanks


0
...
written by Shanmuganathan, October 11, 2011
Hi, Good work. Thanks to share this.I use this webservice example its works fine. Whenever i add a new method in soaptest.php then i face a error while i run that webservice. it shows the "XX method" is not a valid method for this service. Please help me to add a new method in that webservice
0
...
written by Shanmuganathan, October 11, 2011
Hi, I am new to zend webservice. i use your application in to my system its working fine. but i face exception while adding a new method in that. i met "XXX "is not a valid method for this service. How to i add a new method in this webservice.
0
...
written by Shanmuganathan, October 11, 2011
If you using zend framework : please change your Url like this http://Your host/projectname/public/controllername?wsdl'
0
...
written by Mich, December 20, 2011
Im new to Web Services.My question is since it has more than one method how would each method be called from the client side?

Write comment

busy

Related news items:
Newer news items:
Older news items:

Feedback Form