Article: PHP & Web Services »
FERDY CHRISTANT - NOV 11, 2005 (07:07:52 PM)
Today I investigated how to create and consume web services in PHP. I was planning on writing an article about it, but found two extensive articles on the subject existing:
Both use the free NuSOAP library to both create and consume services in PHP. Instead of writing a similar article, I will cover two things:
- Show the result of my own example, and explain how it is different
- Demonstrate an effective way to test web services using a free tool called SoapUI
The code
Since my point is to get you up and running with web services in PHP, I'm keeping the actual service simple. My service exists out of a single "add" method that takes two integer values and returns the total, also as an integer. Following the approach in the article above, the end result of my PHP project is like this ("webservice" is my project's root folder):
| File | Description |
|---|---|
| webservice/libs/functions.php | Contains the implementation of the add function |
| webservice/libs/nusoap.php | SOAP library |
| webservice/client.php | Tests the web service |
| webservice/server.php | SOAP server |
Go ahead a have a look at the source code of these files. Compared to the examples in the forementioned articles, there is only one difference, but it is an important one. Instead of including the function implementation in the server.php file, which generates the WSDL file, I have put the implementation in a separate file. Both approaches work, the difference is merely conceptual. I see a web service merely as a wrapper around existing functionality. A web service should define functionality, yet not implement it.
One cool feature of the NuSOAP library is that of web service inspection. If you open the server.php file in a browser, you will get something like this:
This basically is a friendly presentation of the methods in the WSDL file that is generated by server.php. Note that if you open server.php?wsdl you will get the raw XML output of the WSDL file. In this simple example, we have a single method defined in our service: ws_add.
Testing the web service
The project's file list contains a file named "client.php". This file performs a simple test on our web service, also using the NuSOAP library:
<?php
require_once('libs/nusoap.php');
$wsdl="http://yourhost/webservice/server.php?wsdl";
$client=new soapclient($wsdl, 'wsdl');
$param=array('int1'=>'15.00', 'int2'=>'10');
echo $client->call('add', $param);
?>
Although fairly easy, you may not want to write a test for each method you define in a web service like this, especially if your web service consumers are not other PHP scripts. Luckily, there's a more easy and effective way of testing a web service: SoapUI.
SoapUI is a free tool that allows you to test any web service. Let's see how we can use SoapUI to test our web service. I'll focus on a simple test, and leave you to play with the other features.
- Download, install and start SoapUI
- Go to File->New WSDL Project
- Give your project any name
- Choose a location to store your SoapUI project file
- Right-click on your new project, and go to Add WSDL from URL
- Enter http://yourhost/webservice/server.php?wsdl
- Choose Yes to generate default requests for all methods
- Expand the project tree and double-click on "Request1":
- This is where the magic starts. You will now see a split-screen window. On the left, you will see your SOAP request. On the right, the SOAP response, which is empty because we haven't done anything yet. The thing to do now is to edit the parameters in the SOAP request. In the request you see two question marks ("?") for the values of the integer parameters of the ws_add function. Edit the values for your test purpose.
- Click on the green "Play" button in the top of the window to send the SOAP request. The right panel has now returned the SOAP response. Inspect the "total" return value of the response to see if the ws_add web service is functioning properly.
click to enlarge
Conclusion
My conclusion is simple. Using the NuSOAP library, both creating and consuming web services is simple and effective. In combination with the SoapUI tool to test your work, you will become even more productive. As for next steps, keep in mind that the NuSOAP library can also use the XML-RPC and REST methods for web services.



Comments: 27
Reviews: 7
Average rating:
Highest rating: 5
Lowest rating: 4
COMMENT: DENNIS ATTINGER
NOV 15, 19:22:21
Dennis «
COMMENT: DA
OCT 15, 07:21:30 AM
echo $client->call('add', $param);
to following line
echo $client->call('ws_add', $param); «
COMMENT: VIOLA
OCT 23, 02:03:10 AM
What editor do you use to perform "Right-click on your new project, and go to Add WSDL from URL" «
COMMENT: TOM VAN LEIJSEN
NOV 21, 13:23:17
I've tried many, many examples. But by every example i'm trying i get only a boolean(false) back from my soap server.
Also in your example, could you please help me. «
COMMENT: GIZMOPOP

DEC 4, 09:18:41 PM
COMMENT: DEEPTHI TALLURI
DEC 17, 12:55:59 PM
I got the follwing error
Fatal error: Cannot redeclare class soapclient in F:\wamp\www\webservice\libs\nusoap.php on line 7240
could you please give me the solution for this «
COMMENT: NGUYỄN NHÂN TÂM
JAN 15, 2008 - 02:30:39 AM
COMMENT: EGON
MAR 28, 2008 - 02:30:03 PM
You have 2 soapclient's i think you work with php5.2 and included there to
the php_soap module delete that one
or nusoap or php_soap but not both «
COMMENT: HELLO

APR 11, 2008 - 12:30:34 PM
COMMENT: SHAJI
APR 24, 2008 - 09:36:23 AM
COMMENT: EDDIE

APR 29, 2008 - 10:34:41 PM
Catchable fatal error: Object of class soapval could not be converted to string in C:\wamp\www\libs\nusoap.php on line 3613 «
COMMENT: PRITAM
MAY 9, 2008 - 11:47:09 AM
1. I got this error "
Fatal error: Cannot redeclare class soapclient in C:\wamp\www\soap\lib\nusoap.php on line 7240". After so much hunting for sol'n on google.....I got some sol'ns... such as replace the class and function name from "soapclient" to "nusoap_client", "nusoapclientPE" etc....
I tried out the solution and the error was GONE!
BUT.....I didnt get any thing after this....means I am getting blank screen o n my browser.
2. I debugged it with call() function in nusoap.php file. In that the return variable getting blank...... Now I dont know why this is happening?
Please let me know the sol'n on this AEAP. its a urgent for me.
For your reference ...
1)I am using WAMP5 Ver 1.6.6 as a server.
2)Also my host port is 81(i.e. localhost:81).
3)OS-> Win XP Prof.
Hope you will reply me with the sol'n AEAP.
Thanks,
Pritam «
COMMENT: MANOHAR
MAY 23, 2008 - 12:39:31 AM
COMMENT: CARLOS OROZCO


JUN 8, 2008 - 17:49:15
I fix this problem making an upgrade of the unsoap library on http://sourceforge.net/projects/nusoap/ and all works like a charm. Thanks to the author for the valuable info.
Best Regards, «
COMMENT: GML
JUN 12, 2008 - 10:26:28 AM
I've a question . How can I test several webs services at once, with soapUI tool?
Thanks it advance «
COMMENT: CARLOS
JUL 3, 2008 - 17:48:33
thanks thanks thanks !!!!!!! After many days of fighting with WS this doc I finish close some ideas and be able to walk my own webserver .... thanks again! and sory for my bad inglish. I am argentine!!! «
COMMENT: HEENA
AUG 15, 2008 - 02:14:46 AM
and i m getting this error
PHP Catchable fatal error: Object of class soapval could not be converted to string in /development/meshconference/meshampintranet/web/nusoap.php on line 3613
please help me to find out whats wrong... «
COMMENT: REDDRAGON
AUG 21, 2008 - 11:11:03 AM
COMMENT: SD
AUG 22, 2008 - 11:29:13 AM
COMMENT: ANGGA LINGGA


AUG 22, 2008 - 05:49:45 PM
COMMENT: RENJITH
SEP 20, 2008 - 05:11:05 AM
COMMENT: RENJITH
SEP 20, 2008 - 05:12:58 AM
COMMENT: MARTIN
NOV 22, 2008 - 14:51:24
first change the call to the webservice in 'client.php'
echo $client->call('ws_add', $param); «
COMMENT: MARTIN
NOV 22, 2008 - 14:52:01
function ws_add($int1, $int2){
#return new soapval('return','xsd:integer',add($int1, $int2));
return add($int1, $int2);
} «
COMMENT: MARTIN
NOV 22, 2008 - 14:54:29
HTTP Web Server: Lotus Notes Exception - The formula has exceeded the maximum allowable memory usage. «
COMMENT: MARTIN
NOV 22, 2008 - 14:54:56
debug_str;?> «
COMMENT: WEBDESIGN

DEC 16, 2008 - 11:05:11
THX «