administration mode

 

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):

FileDescription
webservice/libs/functions.phpContains the implementation of the add function
webservice/libs/nusoap.phpSOAP library
webservice/client.phpTests the web service
webservice/server.phpSOAP 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.

BOOKMARK THIS CONTENT
del.icio.us technorati digg Furl YahooMyWeb Reddit NewsVine

Comments: 18
Reviews: 6
Average rating: rating
Highest rating: 5
Lowest rating: 4

COMMENT: DENNIS ATTINGER email

NOV 15, 19:22:21

comment » Leuk artikel! Erg duidelijk. Volgens mij kunnen we best wel vermelden dat uit testen bleek dat er goede ontwikkel- en testtools beschikbaar zijn om Webservices te bouwen met PHP. En is zie dan geen bezwaar om naar dit artikel te verwijzen.

Dennis «

COMMENT: DA

OCT 15, 07:21:30 AM

comment » Change following line

echo $client->call('add', $param);

to following line

echo $client->call('ws_add', $param); «

COMMENT: VIOLA

OCT 23, 02:03:10 AM

comment » have a question

What editor do you use to perform "Right-click on your new project, and go to Add WSDL from URL" «

COMMENT: TOM VAN LEIJSEN email

NOV 21, 13:23:17

comment » Please help me!

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 emailrating

DEC 4, 09:18:41 PM

comment » thankmuch! «

COMMENT: DEEPTHI TALLURI email

DEC 17, 12:55:59 PM

comment » i am using wamp server,i have downloaded four files. This is the url i have used to test the webservice. http://localhost/webservice/client.php.

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 email

JAN 15, 2008 - 02:30:39 AM

comment » thank for your share . «

COMMENT: EGON

MAR 28, 2008 - 02:30:03 PM

comment » >Fatal error: Cannot redeclare class soapclient in >F:\wamp\www\webservice\libs\nusoap.php on line 7240

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 emailrating

APR 11, 2008 - 12:30:34 PM

comment » Good work...... «

COMMENT: SHAJI rating

APR 24, 2008 - 09:36:23 AM

comment » gud «

COMMENT: EDDIE emailrating

APR 29, 2008 - 10:34:41 PM

comment » I get this in the response window:

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 email

MAY 9, 2008 - 11:47:09 AM

comment » Hi, I tried out this NuSOAP but I am facing problems while implementing in this way.

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! 18

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 rating

MAY 23, 2008 - 12:39:31 AM

comment » Works good. I had to change the nusoap.php to change soapclient to soapclient2 as soapclient is already defined somewhere in php. However, the issue I'm looking at is that the client page is empty/blank. I think it's the same issue as TOM VAN LEIJSEN's. Else good work and thanks much. «

COMMENT: CARLOS OROZCO emailhomepagerating

JUN 8, 2008 - 17:49:15

comment » "Catchable fatal error: Object of class soapval could not be converted to string in C:\wamp\www\libs\nusoap.php on line 3613"

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 email

JUN 12, 2008 - 10:26:28 AM

comment » Hi!

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

comment »

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

comment » Hi i copied all files and tried to run it but is just went blank... no output...

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 email

AUG 21, 2008 - 11:11:03 AM

comment » hello..can anyone give me an example of nusoap with a web service that access a database..a simple insert will do 18 «

RATE THIS CONTENT (OPTIONAL)
Was this document useful to you?
 
rating Awesome
rating Good
rating Average
rating Poor
rating Useless
CREATE A NEW COMMENT
required field
required field HTML is not allowed. Hyperlinks will automatically be converted.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30