In PHP there is no easy way to create a wsdl files. Unfortunately developers have to create or generate wsdl files manually. If we write WSDL file manually there is a risk that it will not work in other languages like C#, Action Script or Java. Fortunately there is a program called Enterprise Architect which can create WSDL files compatible with all languages. It generates WSDL files with any complexity you want.

You will need Enterprise Architect 7.5 professional. This is quite important, other versions like desktop edition do not support WSDL generation.

Let’s assume that we want to write a web service which takes a name as a parameter and returns a warm “Hello”. The method will be something like:

1
sayHelloTo("Vlad");  //output will be "Hello Vlad".


Before we start it is very important to know what WSDL file consist of. WSDL file is a XML structure with the following main parts.

1
2
3
4
5
<types> - shows what data type will be transmitted. Int, string, byte,  complex (array, objects) etc.
<messages> - What messages will be transmitted. For example input/output or request/response
<portType> - what operations (functions/methods) will be supported. For example getUser() or sayHelloTo()
<binding> - how messages be transmitted. For example SOAP or HTTP
<service> -  where is the service located. What is the URL of the application which knows how to execute the service. For example: http://example.com/server.php

Now when we know all this we can start.

1. Open EA, and create a new project file. For example wsdl.eap

2. From project browser create a new Class Model with Class diagram
wsdl EA PHP

3. Under Toolbox, click on More button and select WSDL
wsdl EA PHP

4. double click on the Class diagram you created in step 2 to open it on the screen.

5. Select Namespace Element from the toolbox and drop it in on the opened diagram. You should get a dialog which asks for a package name and namespace. I called my package AmdarisTest
WSDL PHP EA

6. After you click OK, EA will generate a sample web service. You can see it in the Project Browser. Notice that system generated 6 folders. types, messages, portTyle, binding and services. Just as WSDL file structure.

7. TYPES. Open Types folder and click on types diagram. Remove InputParameters and OutputParameters generated by Enterprise Architect. From the toolbox drag and drop 2 simple type elements. Call them NameInput and HelloOutput. Both must be strings. One will be used to pass the name to the web server and another one will be used to get the return.
sayHelloTo(“Vlad”); //output will be “Hello Vlad” – input and output are both strings

8. MESSAGES. Open messages folder and click on messages diagram. Remove SampleInput and SampleOutput generated by EA. From the toolbox drag and drop 2 messages. Call them NameInputMessage and NameOutputMessage. From tool box drag and drop over NameInputMessage the message part element (blue cube). A new dialog will pop up. Set up the following values:
Name: InputParam
Type: select NameInput (created in the previous step)

Do the same with NameOutputMessage, Name: OutputParam, type HelloOutput. We created these types in step 7.

9. PORTTYPE. Open portType folder and click on portType diagram. Remove SamplePortTypeHTTP and SamplePortTypeSOAP generated by EA. From the toolbox drag and drop porttype element. Give it a name for example AmdarisPort. From tool box drag and drop over your porttype the porttype operation element (pink cube). A new dialog will pop up. Set up the following values:
Name: sayHelloTo (this is our function)
Operation Type: Request-Response
Input : Name: Request1, Message: NameInputMessage (created in previous step)
Output: Name: Response1, Message: NameOutputMessage (created in previous step)

10. BINDING. Open binding folder and click on binding diagram. Remove SampleBindingHTTP and SampleBindingSOAP generated by EA. From the toolbox drag and drop binding element. A new dialog will pop up. Set up the following values.
Name: Binding1
PortType: select yours. Mine is AmdarisPort.
Protocol: Soap
leave all other fields as is.

11. SERVICES. Open Services folder and click on services diagram. Remove SampleService generated by EA. From Toolbox drag and drop service element. A new dialog will pop up. Give your service a name, click new and select your binding created in the previous page. Set location to where your server script is. For example http://www.example.com/server.php. Make sure it has http:// in front of your URL.

12. In the project browser find and remove SampleWSDLFile generated by EA. Go back to the main diagram called overview and open it. From the toolbox drag and drop WSDL element. A new dialog will pop up. Select the path where you want to save your wsdl, select your service and click OK.

13. In the main top menu click on Project->Web Services->Generate WSDL. Check the settings and click Generate.

That is it, your WSDL file should be saved on your hard drive.
To consume and test your WSDL file use the following PHP codes:
Server side:

1
2
3
4
5
6
7
8
9
10
<?php
class sayHello {
 function sayHelloTo($name){
return "Hello ".$name;
  }
}
$server = new SoapServer("test.wsdl");
$server->setClass("sayHello");
$server->handle();
?>

Client side:

1
2
3
4
5
6
7
8
<?php
  $client = new SoapClient("test.wsdl",array("trace"      => 1,"exceptions" => 0));
print_r($client->sayHelloTo("Vlad"));
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
?>