Simple SOAP client with wsdl2php using WSDL

Nusoap used to be my favourite php toolkit for SOAP communication. But I was struggling a lot with it to make a simple client from the wsdl file. I really liked the way .NET SOAP modules parse the wsdl in a second and give you classes that you can use in your code. So why not having the same thing in php?

Luckily for me somebody thought of it first. Enter wsdl2php. It is simple, lightweight and easy to use. It parses the wsdl file and creates ready to use php classes. It is really handy for tackling those complex data types that you might have on your web services. It can be used both on Linux and Windows from the php command line.

So go ahead and download the latest version from sourceforge. If you are on a Linux distribution you can use the pear installer to install it from the .tgz file:

sudo pear install wsdl2php-0.2.1-pear.tgz

and then use it as a command line app with:

wsdl2php http://yoursoapserver/Service.wsdl

If you are on Windows, no worries, you can also extract the tgz and call the wsdl2php.php file from the command line with the URL to the WSDL as an argument. Something like:

C:\PHP5\php.exe -f "C:\wsdl2php\wsdl2php.php" --  "http://yoursoapserver/Service.wsdl"

There are also more info how to use php cli under windows on the php manual pages.

The script should generate a php file in the working directory with all the classes and data types that are found on the WSDL. The generated client class extends php's SoapClient class, which shold come with the php core. You just have to include it in your php app, and you are ready to go:

require_once 'YourWebService.php';
$client = new yourWebService();
$input = "Your Input"; # check the generated classes to find out
                       # what the required input should be. 

try{
   echo $client->someFunction($input);
}
catch (Exception $e){
   echo $e->getMessage();
}


If you are dealing with complex data types, be sure to check the generated classes to determine what your input variables should look like.

If you learn to use this tool, it should make your work with SOAP and WSDL a lot easier.

However if you are struggling to hard and still can not make a soap client work with a wsdl file and generate the desired XML output read my blog post about posting raw XML with nusoap to a SOAP Web Service

7 comments:

  1. Hi,

    It would be great if you could give me your feedbacks on http://www.wsdltophp.com website which uses the WsdlToPhp project.

    best regards,
    Mikaƫl DELSOL

    ReplyDelete
  2. I got the following error:

    Fatal error: Call to undefined function __soapCall() in \Webservice.php on line 10182

    Any ideas?

    ReplyDelete
    Replies
    1. Addendum
      Here is the full code where the error occurs:

      public function GetVersion() {
      return $this>__soapCall('GetVersion', array(), array(
      'uri' => 'urn:Webservice',
      'soapaction' => ''
      )
      );
      }

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hm, I called wsdl2php on the command line and it produced a PHP file for my webservice, but one of the auto-generated classes in the file is calles 'Exception', which leads to the error:

    "Fatal error: Cannot redeclare class Exception in /path/to/generated/phpWebServiceClass.php on line 11"

    Do you any idea on what to do about this?

    ReplyDelete
  5. Catchable fatal error: Argument 1 passed to Data::searchEmployee() must be an instance of searchEmployee, string given, called in C:\xampp\htdocs\wsdlclient\wsdl2.php on line 9 and defined in C:\xampp\htdocs\wsdlclient\Data.php on line 101
    +++++++++++++++++
    any idea ?

    ReplyDelete