Monday, August 3, 2009

Web Services a la Zend

Zend Studio 7 was released this past week, and I wanted to commemorate this announcement by showing something new that hasnt been done on devzone or the rest of the web yet. In the past I have already shown how to debug a script, and how to reproduce and troubleshoot a problem, and even how to profile a bottleneck and then fix it with caching, so this time we will create a SOAP web service and use Studio to generate the WSDL.

The hardest part I find in creating SOAP services in PHP isnt the actual PHP coding but the assoc WSDL file, so in my article I will be using both Zend Studio 7 and Zend Server to create a very simple SOAP server and client and show how to easily create the WSDL file.

Let's start with the example, it will be a simple SOAP service that will just join two strings together, I'll call it JOINER. To call the SOAP service, I need a client so I put this code in a file called SOAPclient.php. (click on code to see it bigger):
Notice in the client code above, there really is only two lines 5 and 6 that are actually needed to call a SOAP service in PHP, line 5 references the WSDL file in this case it will be on local server. and on line 6 we are calling the JOINER function and passing two parameters to the service. On line 3 we are disabling WSDL caching so that if there are any changes to the file it wont be cached, you can delete that line when you are done with developement. This is all you need if you are calling a SOAP service from PHP. In our case we will also create the PHP service part and assocated WSDL file. I'll keep it all on the localhost but you all know we can move things around so that both parts are on different servers, just change the reference of "localhost" to its IP address or DNS name.

Next we need to create the service and we start with a simple PHP function that we will expose to the internet as a SOAP service. I call this file SOAPserver.php

The first thing to note in this code is the function JOINER it looks like a normal PHP function, but to expose it to the internet as SOAP service we just need to add lines 13-15 and also create a WSDL file for it.

For the WSDL we need to add a spcial docblocks to the PHP code that specifies what TYPE the incoming and outgoing parameters will be for the service, even though PHP is loosely typed, other languages that need to talk to my SOAP service will need to know exactly what variable types to pass along, that's why we have lines 2-7 with @param and @return. This will help when we are creating the WSDL file.

Now in Zend Studio 7 to create a WSDL file, click on the SOAPservice.php file and press (CTRL+N) in menu select WEB SERVICE > WSDL

and I gave my WSDL a file name called "myservice.wsdl" see the following

Then press NEXT>

In the namespace its important to type "URN: somenamespace", and select RPC literal for a REMOTE PROCEDURE CALL. Then press FINISH button. There are 4 things to update in this next screen:

Make sure your properties tab at bottom is selected, then press on the left box called myserviceSOAP that entry inside it should say "" you will replace this with the location of your SOAP service, in my case the following "http://localhost/MyDemos/SOAPserver.php"

Next in the center box, you should see "NewOperation" so change that to one of the function calls in our service, in our case "JOINER"

Then next to INPUT you should see "NewOperationsRequest" and I changed mine to "A" so it assoc to the first parameter in my function.

Next press the RIGHT mouse button on it and select ADD PART and change "NewPart" to "B" that is the second name of my parameter.

That's it!!! Now save your WSDL. You can look at the XML SOURCE or use the GUI WSDL designer like we did to create the file for our function.

Execute your soap service by using the client script. http://localhost/MyDemos/SOAPclient.php

If it worked you should see something like this:


Now for my simple example both the client and the SOAP server are on the same machine so the next step would be to move the SOAPserver.php and WSDL file to another machine and adjust the "localhost" references in both the PHP code and the WSDL file to the IP or DNS of the remote server.

It makes sense to also do this example in Zend Framework, but in the next day or two, I'll try to post a video of these same steps above atleast.


Juan said...

It is possible to create asycronus soap clients using zend framework?
I want to use then on the server to call external providers.

Many thanks for your reply.

Edward Kietlinski said...

Zend Framework SOAP tutorial

Juan said...

Thanks Edward but as long as I red zend soap client does not allow asyncronous call, in other words I can not register a method where I will get the reply from the server or at least I will not be able to find it, are there any way?

steven said...

Hi Friend,
Congratulations for this nice looking blog. In this post everything about Web Development. I am also interested in latest news, sometimes i posted on Customized application development