12

Is there a way to generate a PHP Soap Client from a WSDL file?

I mean something like wsdl.exe or svcutil.exe in .net, that generates code for a class that can be the client of a service, not something like:

$WSDL     = new SOAP_WSDL($wsdl_url); 
$client   = $WSDL->getProxy(); 

My problem is that I want the PHP client to be able the work with a service, even when that service doesn't expose its WSDL.

hakre
  • 193,403
  • 52
  • 435
  • 836
Meidan Alon
  • 3,074
  • 7
  • 45
  • 63

6 Answers6

17

I found generator really useful

https://github.com/wsdl2phpgenerator/wsdl2phpgenerator

Instructions (from github) :

  • Download wsdl2phpgenerator-2.3.0.phar from the latest release
  • Run "php wsdl2phpgenerator-2.3.0.phar -i input.wsdl -o tmp/my/directory/wsdl"

Work for web hosted wsdl too

eg

php wsdl2phpgenerator-2.3.0.phar -i http://someurl/input.wsdl -o tmp/my/directory/wsdl
aqm
  • 2,942
  • 23
  • 30
  • Best solution ever : you don't have to use composer, it will generate a folder with all needed class and how to use them to call the remote method – Sami Apr 29 '20 at 12:09
10

You can use the method [generateProxyCode] provided in the package SOAP_WSDL (http://pear.php.net/reference/SOAP-0.9.4/SOAP/SOAP_WSDL.html#methodgenerateProxyCode) method instead and save it to a file:

$WSDL     = new SOAP_WSDL($wsdl_url); 
$php      = $WSDL->generateProxyCode();
file_put_contents('wsdl_proxy.php', '<?php ' . $php . ' ?>');

require 'wsdl_proxy.php';
useless
  • 1,876
  • 17
  • 18
Greg
  • 316,276
  • 54
  • 369
  • 333
  • 5
    two notes about the sample code: you need to include the PEAR class with require_once("SOAP/WSDL.php"); and the method is not $WSDL->getProxyCode(); but $WSDL->generateProxyCode(); – Stefano Feb 01 '14 at 12:38
8

There is an app for this, it's called wsdl2phpgenerator:

http://code.google.com/p/wsdl2phpgenerator/

Run it against a WSDL file and it will generate classes based on the WSDL services.

Clay Hinson
  • 932
  • 1
  • 6
  • 9
  • wsdl2phpgenerator will give you a great head start – jjwdesign Jan 13 '12 at 15:25
  • 1
    To get the latest version go to the new home of wsdl2phpgenerator, on github, https://github.com/walle/wsdl2phpgenerator There is also a notable fork at https://github.com/reload/wsdl2phpgenerator that try to integrate pull requests I haven't really had the time to verify and merge. – Fredrik Wallgren Apr 06 '13 at 15:14
4

Just to help anyone else who comes across this post and thinks "how the heck do I work w/ this SOAP_WSDL thing?" (like myself)

Open the command line and get to your php directory (I installed XAMPP Lite in this example)

Once in the php directory I ran the pear.bat script. After this I was able to type the following via cmd line

pear -V (provides the version of your install)

pear list

If you type the above and don't see SOAP you need to do the following from the cmd line:

  • pear install Net_DIME-1.0.1

  • pear install Mail_Mime-1.5.2

  • pear install Mail-1.2.0b1

  • pear install SOAP-0.12.0

Now after you install these packages and do another "pear list" you should see SOAP listed.

If so you can include a reference to the php files pulled down inside the pear directory under SOAP.

One example of this path might be C:\xampplite\php\PEAR\SOAP

Toran Billups
  • 27,111
  • 40
  • 155
  • 268
0

I used wsdl2php, a simple PEAR tool; it seems that project is dead, but you can still download the latest version here: http://sourceforge.net/projects/wsdl2php/

It require a development machine with PHP 5 and PEAR, and you have to install it with this PEAR command:

sudo pear install wsdl2php-0.2.1-pear.tgz

After this, you can generate the PHP classes file with this command:

wsdl2php <WSDL_URL>

It generates a main class that extends SoapClient, and many other classed that represent requests, responses, and complex objects, so it is very useful when developing in a IDE with "intellisense" like NetBeans.

Stefano
  • 318
  • 1
  • 4
  • 13
0

I've tried to use everething that were listed here.

Found the another choise: https://github.com/mikaelcom/WsdlToPhp

Pluses in comparition with previous:

  • No dependencies. Both for generator and created client.

  • Classes for in and out parameters.

  • Examples of using for created client. It's course not so important. But some times very useful

  • Less of code (In comparision with SOAP_WSDL)

Minuses:

  • Answer and any complex subtype are wrapped to another object that contains technical info.
Kamil Gareev
  • 146
  • 3
  • 12
  • Thanks for your detailed feedback. Could you send me more details about your remark "Answer and any complex subtype are wrapped to another object that contains technical info." at contact@wsdltophp.com, it would be really appreciated, Thks – Mikaël DELSOL Apr 23 '15 at 08:18
  • It's ,actually, not correct. In .NET you can get objects exact the same you have it at service. But In my case when I get responce in PHP from .NET Service, I've got "additional" wrappers. They actually present in WSDL, but .NET ,somehow, "knows" that some classes are just for technical needs and remove it in result. – Kamil Gareev Apr 23 '15 at 19:08
  • Thanks, if you had some examples it would be great, otherwise I'll search if it can be avoided. – Mikaël DELSOL Apr 23 '15 at 20:53