Using SOAP in PHP

This document assumes knowledge of PHP and that you know what SOAP is. If you do not know what SOAP is, see my paper ‘what is soap?’ here.

PHP is so popular across the internet; most hosting companies and campuses have it installed; that quite often we are asked how to use PHP to deploy or access webservices using XML-RPC/SOAP. Although PHP is not my choice for doing webservices I can and will answer this question, with just one word of caution: PHP handles data types really poorly, don’t use it for any complex services.

Which Library To Use

First thing no one has a desire to manually parse XML, build envelopes and do networking (if you do quit reading now and see: for the specs). So we should choose a library (or class) to do the tedious work for us. There are many to pick from, just take a look on google, but I think the best is dietrich’s ‘NUSOAP’ which can be downloaded free from Download it and install it somewhere the webserver has access to.

Building a (very) Basic Service

You’ll now want to do a basic service (server). The following code is a variation on the required hello world example. It will return a greeting when called upon by a client. The variation is that it requires a name to send a greeting to.

$s = new soap_server;

function helloyou($name){
// We require the name of the person we are saying hello to
// so if they do not supply it we return an fault message.
if($name == ”){
return new soap_fault(‘Client’,”,’I do not know who you are!’);
return “hello $name!”;

Building a (very) Basic Client

Now that you have a basic server script written, place it on your webserver. Now we can write a client to access it. The following is super basic! A user fills in a form with their name and then the webservice is accessed to get a greeting for the user.

<?php include(‘/path/to/nusoap.php’); ?>
<!–Yes I am aware I am missing the DOCTYPE tag.– >
<from id=”Some form” action=”THISDOCNAME.php” method=”get”>
Name:<input name=”name” type=”text” value=”world”>
<input type=”submit” value=”submit”>
if ($name) {
//we create an array with the element name that has the form value of name
$parameters = array(‘name’=>$name);

//now we must create a soapclient object
$soapclient = new soapclient(‘’);
//now we call the server.
echo “Greeting returned”, $soapclient->call(‘helloyou’,$parameters);

Handling Errors and Debugging

Services like the one above, as simple as they are, may still require error handling. For example if the client submitted the name as NULL the server would return an error message. To see what I mean take the ‘if ($name)’ out of the client example and then add the following error handling code to see for yourself:

$soapclient = new soapclient(‘’);
//now we call the server.
$result = $soapclient->call(‘helloyou’,$parameters);
if ($err = $soapclient->getError()) { // if error returned then this has a value
echo ‘The Server returned error: ‘, $err; // echo the error faultstring
} else {
echo ‘Greeting returned: ‘, $result;

Something a Little More Useful

So it’s all well and good to do simple scripts. But, now you want to venture in to doing some more complex?

The following will demonstrate a webservice to list courses available to students from a db over a network. The client will connect to the webservice and request a course listing. The courses will be pulled from a MySQL database and the results returned as a nested array to the client.

The DB

# This is the SQL to create the table
# in MySQL.
# Table structure for table courses

CREATE TABLE courses (
id int(11) NOT NULL auto_increment,
name text NOT NULL,
number int(11) NOT NULL default ‘0’,
seats int(11) NOT NULL default ‘0’,

# data for table courses

INSERT INTO courses VALUES (1, ‘ENGR’, 1215, 15);
INSERT INTO courses VALUES (2, ‘ENGR’, 1233, 20);
INSERT INTO courses VALUES (3, ‘MATH’, 1219, 32);
INSERT INTO courses VALUES (4, ‘ENGL’, 1209, 17);
INSERT INTO courses VALUES (5, ‘PHYS’, 1205, 30);
INSERT INTO courses VALUES (6, ‘ARCH’, 3305, 12);
INSERT INTO courses VALUES (7, ‘ARCH’, 1201, 16);

The Server


$s = new soap_server;

function getcourses(){
$course = new EXAMPLE;
return $course->ListCourses();

class EXAMPLE {

function DB_Open(){
mysql_connect (“localhost”, “login”, “password”);
mysql_select_db (“soapexamples”);

function ListCourses(){
$query = “SELECT * FROM courses ORDER BY name, number ASC;”;
$result = mysql_query($query);
$cnt = 0;
while ($course = mysql_fetch_assoc($result)){
$courses[$cnt] = array(‘name’ => $course[“name”], ‘number’ => $course[“number”], ‘seats’ => $course[“seats”]);
$cnt = $cnt + 1;
return $courses;

The Client

This is not the most complex thing ever, but it does give you a ‘real life’ type example. The client doesn’t send anything and I have no security implementation here… Check the papers at for a paper on SOAP security (I am writing that next).

$soapclient = new soapclient(‘’);
// $soapclient->debug_flag=true; //turn on debugging
$courses = $soapclient->call(‘getcourses’);
// echo $soapclient->request; // shows XML of SOAP request
// echo $soapclient->response; //shows XML of SOAP response

foreach ($courses as $c) {
// The array is nested so this has the same recursions as there are courses
foreach ($c as $info) {
// this only loops once, but you need it anyway. (the array is 3d even
// though there are only 2d of intended structure)
echo “Name: “;
echo $info[“name”];
echo ” Number: “;
echo $info[“number”];
echo ” Seats: “;
echo $info[“seats”];
echo “<hr />”;

Other Things

  • NuSOAP also allows the use of WSDL pages to access webservices, you’ll want to look at the ‘READ ME’ file that comes with the download. I will not cover it here, because, it works basically the same way as above.
  • I’ve put the example files online at:
  • The server components are online as addressed in the example files.
  • The Client php’s are online here and here .

Have fun!

3 سال پیش

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.