About the Author

Chris Shiflett

Chris Shiflett is an author and speaker who leads the web application security practice at OmniTI.


PHP Advent Calendar Day 18

Today's entry, provided by Christian Wenz, is entitled WSDL Despite PHP 5.

Christian Wenz

Name
Christian Wenz
Blog
hauser-wenz.de/blog/
Biography
Christian Wenz got hooked on PHP when he introduced it to one of the largest web sites back in early '99, and he hasn't looked back since.
Location
Munich, Germany

We are working a lot with web services these days. One of the things I like best, from a technical perspective, is the WSDL standard. WSDL is used to provide information about a SOAP web service: operation, data types, messages, location, and the like. Using WSDL, consuming a web service is very easy. Most technologies support an implementation of the proxy pattern and can automatically create a local proxy object from a WSDL description. Here's a quick example that shows how PHP 5's very own SOAP extension works with WSDL:

<?php
 
$c = new SoapClient('/path/to/wsdl');
$result = $c->myWebServiceMethod($arg1, $arg2);
 
?>

Calling myWebServiceMethod() on the local client object instructs the SOAP extension to call the remote method of the same name and parse the SOAP data coming back from the service. Unfortunately, the SOAP extension does not support automatic WSDL creation for a SOAP service, whereas most competing technologies do. One of the beauties of PHP is that the language is not strongly typed, which makes WSDL generation more difficult. However, there are several ways to generate WSDL with PHP 5, with a little help.

Userland Code

One of the oldest web services libraries for PHP is NuSOAP. The original site has not been updated in over three years, but NuSOAP remains in active development on SourceForge. When creating a NuSOAP service, call the configureWSDL() method, and provide the data types for input arguments and the return value when calling register(). Here's a short example (sans the actual business logic of the service, which resides in myWebServiceMethod()):

<?php
 
function myWebServiceFunction($inputValue) {
    /* ... */
}
 
require 'nusoap.php';
 
$soap = new soap_server();
$soap->configureWSDL('NuSOAPService', 'http://hauser-wenz.de/');
$server->wsdl->schemaTargetNamespace = 'http://soapinterop.org/xsd/';
 
$soap->register('myWebServiceMethod',
                array('inputValue' => 'xsd:string'),
                array('outputValue' => 'xsd:string'),
                'http://soapinterop.org/');
 
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soap->service($HTTP_RAW_POST_DATA);
 
?>

NuSOAP runs on PHP 4 and PHP 5. A NuSOAP-based web service is obviously compatible with SOAP extension and other SOAP implementations as well.

Extensions

A binary PHP extension performs much better than userland code. An extension that supports WSDL generation is WSF/PHP from WSO2. Once you install, XML comments provide hints to the extension about the data types and operations used in the service.

<?php
 
function myWebServiceFunction($inputValue) {
    /* ... */
}
 
$ops = array('myWebServiceFunction' => 'myWebServiceFunction');
$svr = new WSService(array('operations' => $ops, 'bindingStyle' => 'rpc-enc'));
$svr->reply();
 
?>

When you request this page, you get basic information about the service. When you append ?wsdl to the address, you get a fitting WSDL description.

Additional Options

Some other tools and packages promise WSDL generation; here is a (possibly incomplete) list:

Even if you don't want to rely on external code or extensions, using one of the aforementioned options might still be of interest. You can use them to create the WSDL, and then use that WSDL (after modifying the service's address) to fuel the SOAP extension-based service.

Happy holidays!

About This Post

PHP Advent Calendar Day 18 was posted on Tue, 18 Dec 2007 at 23:29:39 GMT.

1 Comments

1. Mike Coughlin's GravatarMike Coughlin said:

Thanks, Christian.

I was just reading Robert Richards' SOAP chapter and thinking that creating a WSDL document is butt-ugly!

Your options look more promising; so, thanks - for pre-doing the "homework".

Wed, 19 Dec 2007 at 04:29:49 GMT Link


Post A Comment

Personal Details and Comment

Style Guide

Line breaks are converted to paragraphs. Also use:

  • <a href="" title="">text</a>1
  • <em>text</em>
  • <blockquote><p>text</p></blockquote>
  • <code>2  <?php  if ($foo) {      $foo = TRUE;  }  ?></code>
  1. Note: <code> can be used inline (e.g. in paragraphs) or in a block as shown. Include whitespace and newlines in blocks.

Please enter Chris (my first name) below. This is a primitive spam prevention technique, and I apologize for the inconvenience.

Preview and Submit

Upcoming Talks

O'Reilly Open Source Convention

21 - 25 Jul 2008

At Oregon Convention Center, Portland, Oregon.

ZendCon

15 - 18 Sep 2008

In Santa Clara, California.

PHP Appalachia

11 - 14 Oct 2008

At Big Bear Lodge, Gatlinburg, Tennessee.

New Comments

Amir wrote:

Hi chris! Please check this and guide me: http://forums.devnetwork.net/viewtopic.php?f=34&t=8...

Posted in
Nathan Bentley wrote:

Hi Chris, A great tutorial, which should help a lot of people! We implemented something simil...

Posted in
Daniel S wrote:

Just recently I sold my 1.gen Macbook(core duo version). And to be honest, I don't miss it for on...

Posted in Top X List of Mac OS X Annoyances
Buke Beyond wrote:

I agree it is ridiculous that php is doing this. I am using php for generating commands for othe...

Posted in PHP Stripping Newlines
Davis Ford wrote:

I agree, although I have a list of many more annoyances. However, rather than complain about the...

Posted in Top X List of Mac OS X Annoyances

Browse Comments