23 April 2009 ~ 0 Comments

Start SOAP with Zend Framework

Start SOAP with Zend Framework

SOAP? Hang on.. This is not the soap you probably first think of from your daily life. This is the SOAP in Information Technology term. Sorry but don’t mean to discriminate anyone out there ;)

For readers who have no idea what SOAP is. Here’s a short explanation about SOAP as defined by TechTarget.

SOAP (Simple Object Access Protocol) is a way for a program running in one kind of operating system (such as Windows 2000) to communicate with a program in the same or another kind of an operating system (such as Linux) by using the World Wide Web’s Hypertext Transfer Protocol (HTTP)and its Extensible Markup Language (XML) as the mechanisms for information exchange.

Here I will share my step-by-step guide on how to start your SOAP application using PHP and Zend as your Model View Controller (MVC) framework. First, I have the following application and framework installed. But I will not explain in detail about them in this post.

To start, first you need to enable a php extension called php_soap. In WampServer, this module has been pre-installed but you need to enable the module by the following steps.

  1. Click WampServer icon on the tray.
  2. Select PHP > PHP Extension.
  3. And finally click php_soap to enable it.

Then, we create a simple class called Models_Writer which has a function write that returns the specified string to the calling function.

class Models_Writer
{
  /**
   * Return the specified text
   *
   * @param string $string
   * @return string
   */
  public function write( $string )
  {
    return $string;
  }
}

Followed by a simple action function called wsdlAction which renders generated WSDL for the class Models_Writer using Zend_Soap_AutoDiscover. Hey hey stop! what the heck is WSDL?. In short,

WSDL or Web Services Description Language is an XML-based language that provides a model for describing Web services.

Follow this link on wikipedia to help you along with what exactly is WSDL.

Moving on…

/**
 * Render the WSDL
 */
public function wsdlAction()
{
  $wsdl = new Zend_Soap_AutoDiscover();
  $wsdl->setUri( APP_URL . '/index/' );
  $wsdl->setClass( 'Models_Writer' );
  $wsdl->handle();
  exit;
}

As shown on line 7, APP_URL is to be replaced with the url of the application (ie. http://localhost/). You may define APP_URL inside bootstrap.php for global use. Function setUri is used to define the url used as the server. In this example, the indexAction is used to serve as the Soap server. And to verify the generated WSDL is correct, use your browser and type http://localhost/index/wsdl/ with the assumption that all Action functions in this example will be coded inside IndexController class. This url should returns with the correct WSDL. Please take note of the url though, that it must end with a backslash ‘/’.

And if browser returns with an error message below.

XML Parsing Error: XML or text declaration not at start of entity

Please make sure there isn’t any trailing spaces before the <?php and after ?> in your php file.

Next, we shall look at the code to create your Soap server.

/**
 * Serve as the web service
 */
public function indexAction()
{
  $server = new Zend_Soap_Server( APP_URL . '/index/wsdl/' );
  $server->setClass( 'Models_Writer' );
  $server->handle();
  exit;
}

The above code instantiates Zend_Soap_Server with the url to the generated WSDL and set the class as Models_Writer and finally quits upon handling the request. Ok, now the indexAction is ready to serve as a Soap server.

Finally, to make sure that the server is handling the request correctly, let’s create the Soap client that makes a Soap request to the server.

/**
 * Client to request from web service
 */
public function clientAction()
{
  $client = new Zend_Soap_Client( APP_URL . '/index/wsdl/' );
  $response = $client->write( 'hello world' );
  var_dump( $response );
  exit;
}

The above code first instantiates Zend_Soap_Client with the specified url to the WSDL. Line below then calls the function write from the server. Here, you were probably wondering where in the code have we specified the server url. Well, if you recall the previous code when we first created the WSDL, we has actually specified the Soap server url. As a result, Soap client is able to know the server url from the WSDL.

Now try access http://localhost/index/client/, the page should return you with the following line:

string(11) "hello world"

Phew! it’s along explanation for a simple task. My arms are getting cramped now. I hope you are following well with my explanation. If not, well, I have a sample file you can download and test it on your local. I hope it works well. If not, let me know of your encounter.

Well, while experimenting this I have a stumbling block which I managed to find a solution to it. Here goes the encounter.

If you encounter the following error message:

Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in C:wampbinphpphp5.2.8
includesZendSoapWsdl.php on line 94
Fatal error: Call to undefined method domdocument::loadXML() in C:wampbinphpphp5.2.8
includesZendSoapWsdl.php on line 95

You need disable php_domxml on your WampServer.

I hope I give enough information. Feel free to drop your feedback. Thanks! Happy coding! :)

Source:

If you find this post helpful, appreciate if you could leave your rating below to indicate that this post is really useful and at the same time let others know about this post. Thank you friend :)

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading ... Loading ...

Don't leave just yet! You may also be interested to take a quick look at my other posts.

  1. Read Remote Content or File using Zend
  2. Start ORM with LINQ in .Net Framework
  3. Quick and Easy Way to Create Model Class in PHP
  4. Simple Steps to Start WCF with Visual Studio 2008
  5. Zend Caveats

Leave a Reply

Spam protection by WP Captcha-Free