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: 86
Reviews: 24
Average rating:
Highest rating: 5
Lowest rating: 1
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 «
COMMENT: SUDHAKAR
MAR 23, 2009 - 09:55:07 AM
COMMENT: JASPAL SINGH

APR 8, 2009 - 11:26:41 AM
COMMENT: MUHAMMAD SOHAIL


APR 18, 2009 - 12:47:07 PM
Finally I have successfully created nusoap web service.
Thanks to all of you. «
COMMENT: SATISH
APR 25, 2009 - 03:17:41 PM
can you please tell how to append data in nusoap web service in single url address field. can you please say for above example.
best,
satish «
COMMENT: RAHEEL
MAY 6, 2009 - 04:04:04 PM
This was pretty simple and handy example for a beginner to start with web services in php.
I have one issue. Web services in php doesn't allow session management. I have spend a whole day in finding out some stuff but nothing paid my bills. I was trying a simple task i.e. refresh already instantiated session just to keep session alive. The main aim is that my vendor wants to access web service to keep the session alive by accessing it after every 20 minutes. ASP.net seems to have this facility but not php.
Wish somebody can come up with some solution.
Thanks in advance «
COMMENT: SAGAR J
MAY 15, 2009 - 03:21:18 PM
Finally through the WebService.
Just one thing
for PHP 5.2 use
d/l new nusoap library and use
$client=new nusoap_client($wsdl, array('wsdl'));
LOL «
COMMENT: SAURABH

MAY 25, 2009 - 06:33:17 PM
No Deserializer found to deserialize a 'http://com.myserver.webservice/myWS.xsd:ArrayOfcom_myservertype_WeoTrvFamilyParamInUser; using encoding style http://schemas.xmlsoap.org/soap/encoding/. [java.lang.IllegalArgumentException]
Any one Any Idea?
Thanks heaps everyone
Saurabh «
COMMENT: RAJ GOHIL
JUN 5, 2009 - 08:34:46 PM
and replace with following 2 lines.
if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);
I hope this would help. «
COMMENT: MOHAN KRISHANA
JUN 24, 2009 - 01:34:39 PM
Its really a nice one , Guys http://vinsomania.blogspot.com/2009/06/php-web-services-with-nusoap.html
its really nice
cooll
Krishna «
COMMENT: BHARAT
JUL 27, 2009 - 12:30:19 PM
COMMENT: BANAVOZ
AUG 12, 2009 - 04:17:06 PM
Here is another example of using built-in PHP SOAP extension:
http://banavoz.com/2009/08/implementing-soap-web-service-in-php/
Have a nice day! «
COMMENT: SUEN
AUG 25, 2009 - 09:55:35
I have tried to use SoapUI to test but how could I test the php soap server by the client.php?
thx a lot~ «
COMMENT: ALEX
OCT 2, 2009 - 04:56:26 PM
I am actually using soapUI and I also think it is great. It saves a lot of time.
I would like to be able to change the endpoint on php if different than the one given on the wsdl file. I magaed to do this on soapUI but so far I have been unable to do it on a php file.
Hope you can help. «
COMMENT: SHAUN

OCT 13, 2009 - 11:09:08
Reasons:
Good with OOPHP5+
Very well structured
Rather easy to understand/implament
«
COMMENT: TAN-TAN

OCT 14, 2009 - 10:43:57 AM
Any help would be appreciated.
http://code.google.com/p/ezerphp/ «
COMMENT: BIBHU

NOV 5, 2009 - 05:58:47 AM
Thanks «
COMMENT: ERIK
NOV 30, 2009 - 11:34:48
But I needed to implement Martin's 2 fixes to the client and server to get the example to work.
But otherwise, a very well written article. «
COMMENT: SUNIL JOSEPH

DEC 2, 2009 - 12:11:51 PM
Fatal error: Uncaught SoapFault exception: [Client] Function ("call") is not a valid method for this service in D:\xampp\htdocs\webservice\client.php:6 Stack trace: #0 [internal function]: SoapClient->__call('call', Array) #1 D:\xampp\htdocs\webservice\client.php(6): SoapClient->call('ws_add', Array) #2 {main} thrown in D:\xampp\htdocs\webservice\client.php on line 6
I am using XAMPP 1.7.0 in Windows XP
Any help is appreciated
thanks in advance «
COMMENT: SOFA
JAN 7, 2010 - 15:08:38
Thank you for very usefull article «
COMMENT: ASHIM SIKDER

JAN 11, 2010 - 08:52:12 PM
Things need to keep in mind.
1. download Updated version nusoap.php from http://sourceforge.net/projects/nusoap/
2. in client.php file enter online hosting server name [not localhost] $wsdl="http://onlinehostingserver/server.php?wsdl";
2. in client.php file change to $client=new nusoap_client($wsdl, array('wsdl'));
Thanks
Ashim «
COMMENT: SANJEET KUMAR
MAR 18, 2010 - 12:04:06 PM
COMMENT: SANJEET KUMAR
MAR 18, 2010 - 12:49:06 PM
COMMENT: FERDY
MAR 18, 2010 - 21:10:47
COMMENT: GARRY
MAR 30, 2010 - 07:18:40 PM
COMMENT: NIKUNJ

APR 21, 2010 - 06:22:19 PM
http://my-source-codes.blogspot.com/2010/04/access-php-web-service-in-vbnet.html «
COMMENT: JAWAD
APR 22, 2010 - 09:53:26 AM
COMMENT: J
MAY 14, 2010 - 03:50:15 PM
ps. I look at the response and in my case it's in XML, now I'm thinking to bind the result with my XSLT and now I'm pretty much have no idea how that would be done...or maybe if the result is actually is in array, I might simply take each content of the array out and place them in the HTML tags without the needs of XSLT (also save my day if I want to paginate the result)
thanks.
J «
COMMENT: AMAN

JUN 11, 2010 - 12:41:18 PM
i have updated the links in client for the WSDL and also uses NuSOAP for PHP5
no error is coming but it also not showing the output!!!!
PLz Help,
Thanx in Advance «
COMMENT: GREG
AUG 19, 2010 - 02:32:59 PM
please help me «
COMMENT: BRIAN
AUG 30, 2010 - 03:29:14 PM
COMMENT: PAUL
DEC 6, 2010 - 06:52:31 PM
echo $client->__call('ws_add', $param);
It was missing the double-underscore in the example and didn't work until I added it. «
COMMENT: PAUL
DEC 6, 2010 - 06:55:57 PM
COMMENT: SANDEEP VERMA


DEC 7, 2010 - 08:42:19 AM
How to test all web services in PHP? «
COMMENT: ZAILANI

JAN 3, 2011 - 04:09:06 AM
COMMENT: QWERTY

JAN 3, 2011 - 02:15:37 PM
COMMENT: BAIBHAV
FEB 2, 2011 - 12:22:48 PM
This site helped me a lot as I'm new to ws. «
COMMENT: BENMAK
FEB 11, 2011 - 02:28:50 PM
COMMENT: AMIT
FEB 19, 2011 - 06:50:46 AM
COMMENT: CHAMIKA
FEB 24, 2011 - 12:23:26 PM
1. Fatal error: Cannot redeclare class soapclient in D:\xampp\htdocs\webservice\libs\nusoap.php on line 7240
Then i took the latest nusoap.php and replaced with the old one.
server.php is working as you said.
but client.php gives
Warning: SoapClient::SoapClient() expects parameter 2 to be array, string given in D:\xampp\htdocs\webservice\client.php on line 4
Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient() [soapclient.soapclient]: Invalid parameters in D:\xampp\htdocs\webservice\client.php:4 Stack trace: #0 D:\xampp\htdocs\webservice\client.php(4): SoapClient->SoapClient('http://localhos...', 'wsdl') #1 {main} thrown in D:\xampp\htdocs\webservice\client.php on line 4
Can i have any solution for this problem. «
COMMENT: CHAMIKA
FEB 25, 2011 - 07:26:34 AM
require_once('libs/nusoap.php');
$wsdl="http://xxx.xxx.x.xxx/webservice/server.php?wsdl";
$client=new nusoap_client($wsdl);
$param=array('int1'=>'15.00', 'int2'=>'10');
echo $client->call('ws_add', $param);
Then the above problem won't come!!! this is i think
NuSOAP's class name is 'soapclient'. PHP5 includes a SOAP extension whose class name is also 'soapclient'. As a result, you effectively can't use NuSOAP with PHP5. NuSOAP for PHP5 simply changes the class name to 'nusoap_client'.
COMMENT: ROTCEH
MAR 21, 2011 - 16:26:35
"Message: Assigning the return value of new by reference is deprecated
Filename: lib/nusoap.php"
i don't know what to do, because i could't find any update to nusoap library thats works well with php 5.3.2
Someone can share the solution? «
COMMENT: JEDRZEJ
JUN 21, 2011 - 11:13:41
COMMENT: BIJAY KARM

JUN 21, 2011 - 12:40:32 PM
but when i start that client.php
but it doesn't shows the result.
Note: i have put all the files in appropiate folder «
COMMENT: MBENTO

JUN 27, 2011 - 16:49:09
COMMENT: DAAN
JUL 18, 2011 - 12:16:21 PM
COMMENT: VASAGAN
JUL 28, 2011 - 12:35:35 PM
COMMENT: WARLORD

AUG 17, 2011 - 05:36:09 AM
thank you, «
COMMENT: FARHAD
AUG 22, 2011 - 01:33:37 PM
Any one can how to create a web servicces using php,actually i'm new in web services,
i'm waiting for your response. «
COMMENT: ALMAS

SEP 9, 2011 - 02:40:06 PM
Please tell me the details.
Thanks «
COMMENT: RALPH VAN DER HORST

SEP 22, 2011 - 08:27:31 AM
many thanks
Ralph van der horst
Freelance Testconsultant in The Netherlands «
COMMENT: RALPH VAN DER HORST

SEP 22, 2011 - 08:41:38 AM
Quick solution:
I have edited and decomment this in nusoap.php and the soapui test worked like a charm. «
COMMENT: MAURIZIO


SEP 27, 2011 - 12:41:37 AM
Thank you so much!
P.S. I am on linux and it works there as well «
COMMENT: SANDEEP PATHAK

OCT 12, 2011 - 11:55:39 AM
I am a web developer working in india ... with a company . i just want to know the initial steps for creating webservices ... i mean do any one have any refrence website which show me the tutorial of creating web services in php...please help me asap its quite impotent for me «
COMMENT: JAMES VINCE

OCT 14, 2011 - 13:24:13
Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient(): Invalid parameters in /var/www/webservice/client.php:10 Stack trace: #0 /var/www/webservice/client.php(10): SoapClient->SoapClient('http://127.0.0....', 'wsdl') #1 {main} thrown in /var/www/webservice/client.php on line 10
Any help?
P.s I have only altered to set the server as http://127.0.0.1/webservice/server.php?wsdl «
COMMENT: ARUN KUMAR GUPTA
NOV 1, 2011 - 07:32:40 AM
After running the script client.php, there are nothing to print, no output. «
COMMENT: ERIK SOLIS
JAN 11, 2012 - 11:30:24 P.M.
require_once('libs/nusoap.php');
$wsdl="http://localhost/server.php?wsdl";
$client=new nusoap_client($wsdl, 'wsdl');
$param=array('int1'=>'1515.00', 'int2'=>'1515');
echo $client->call('ws_add', $param);
perfect !! «
COMMENT: TAHUITE
FEB 2, 2012 - 11:34:25 P.M.
Thanks a lot. «
COMMENT: MADHU SUDAN

JUL 19, 2012 - 12:32:24 PM
i am new to web services. i am facing some problem. plz help me to sort out.
1.I didnt get any thing after execution....means I am getting blank screen on 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 emailid. its a urgent for me.
For your reference ...
i am using xamp. «
COMMENT: PHP DEVELOPER
DEC 6, 2012 - 04:59:43 AM