#native_company# #native_desc#

Building Web Services Using NuSOAP Toolkit Page 2

By Mitja Kramberger
on December 26, 2003

SOAP Server

Our server is going to create the web service. We will register one method (getWeather) which will accept a string (city) and return an array
with degrees measurement and weather forecast.
First we need NuSOAP toolkit (nusoap.php), which we’ll be in our “inc” directory so that we can easily include it. So let’s look at the actually needed code:



    *  Description:

    *  Creates a simple SOAP Server (server.php).



    // includes nusoap classes



// create server

$l_oServer = new soap_server();


// wsdl generation



$l_oServer->wsdl->schemaTargetNamespace 'http://weather.org/Weather';

    // add complex type








'degrees' => array('name'=>'degrees''type'=>'xsd:string'), 

'forecast' => array('name'=>'forecast''type'=>'xsd:string'))



// register method

$l_oServer->register('getWeather', array(

'city' => 'xsd:string'), 




// method code (get DB result)

function getWeather ($a_stInput) {

        if (
is_string($a_stInput)) {   

$l_oDBlink   = @mysql_connect(


$l_oDBresult = @mysql_db_query(


'SELECT degrees, forecast FROM current_data WHERE city = LCASE("' mysql_escape_string((string)$a_stInput) . '") LIMIT 1');


// simple error checking

if (!$l_oDBresult) {

                return new 
soap_fault('Server''''Internal server error.');



// no data avaible for x city

if (!mysql_num_rows($l_oDBresult)) {

                return new 

'Service contains data only for a few cities.');




// return data

return mysql_fetch_array($l_oDBresultMYSQL_ASSOC);    


// we accept only a string

else {

          return new 
soap_fault('Client''''Service requires a string parameter.');




// pass incoming (posted) data