Zend SOAP Server Webservice quickstart
Posted: June 21st, 2010 | Author: Ward Bekker | Filed under: PHP | No Comments »Below a quick writeup of my first impression with building an basic Zend Soap Webservice. I invite you to add spelling and grammer corrections in the comments for my education.
Starting point
- My team needs to implement a SOAP service for mass posting of vacancies to a job board system.
- The SOAP service is based on a WSDL of an existing service. So we’ll use these specifications as a starting point for the proof of concept.
- On a site-note: I prefer REST above SOAP, because of it’s elegant simplicity. But it wouldn’t make a lot of business sense in this case because a lot of, paying, consumers of the new service have working code for the old service. Adapting to a slightly changed SOAP service will be much easier than a switch to a brand new REST API.
Available SOAP Server extensions for PHP
There are several frameworks / extensions / toolkits for creating a SOAP server for PHP:
- Pear SOAP package. Probably an orphan package as it’s not updated since 2008 and has a beta status. You probably want to look at the alternatives.
- NuSoap SOAP toolkit. Started in 2002 and still under active development as the last release was just a few months ago at the time of writing.
- PHP 5 SOAP extensions. The official SOAP extension for PHP since version 5.
- Zend SOAP Server. Part of the Zend Framework, so probably not very useful if that’s not your current PHP framework.
As we use the Zend framework for this project, it was a natural choice to use it’s SOAP server implementation. We might opt for one of the alternatives if we slam into a brick wall later down the line.
Testing the waters
The steps I’ve taken to get a basic Zend SOAP Server based on the WSDL up and running
- I copied the wdsl to the /public directory of the Zend framework application making it publicly accessible under http://example.org/jobtool.wsdl
- I created a new controller under application/controllers/soapController.php with an public indexAction function. Example code
- The new SOAP service is now available under http://example.org/soap
- Next step: actually handle SOAP requests. Example code. Handling of the soap request is as expected: SOAP method arguments are passed as function arguments. Complex types are represented as a stdClass objects, which basically are associative arrays. Nested complex types are translated to nested stdClass instances. You don’t get any warnings or exceptions if your argument count is different than specified in the SOAP request. IMHO that’s undesirable. I rather have big fat ugly exceptions in that case than subtle bugs. The associative array you return are translated to the complex type as specified in the WSDL and returned to the client.
- To test the SOAP service without the need for a full-blown client i’ve used the free soapUI tool. You point this tool to the WDSL and it automatically creates fake soap request that you can use to test your brand new SOAP services. Make sure you specified the correct urls in the soapAction attributes in the WSDL.
Closing Thoughts
I hope this post saved you some when time building your first SOAP Webservice using Zend Framework. I don’t know yet from experience if the Zend SOAP Server will handle more advanced scenario’s. Only time will tell. Let me know how it works for you.
Leave a Reply