#native_company# #native_desc#
#native_cta#

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:

<?php

    
/**************************************************************

    *  Description:

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

    **************************************************************/

        

    // includes nusoap classes

    
require('inc/nusoap.php');

   

    
// create server

    
$l_oServer = new soap_server();

  

    
// wsdl generation

    
$l_oServer->debug_flag=false;

    
$l_oServer->configureWSDL('Weather''http://weather.org/Weather');

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

    // add complex type

    
$l_oServer->wsdl->addComplexType(

        
'WeatherData',

        
'complexType',

        
'struct',

        
'all',

        
'',

        array(

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

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

    );

  

    
// register method

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

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

            array(
'return'=>'tns:WeatherData'), 

                
'http://weather.org/Weather');

  

    
// method code (get DB result)

    
function getWeather ($a_stInput) {

        if (
is_string($a_stInput)) {   

            
$l_oDBlink   = @mysql_connect(

                
'localhost''someone''something');

            
$l_oDBresult = @mysql_db_query(

                
'weather'

                
'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 
soap_fault('Server'''

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

            }

            
mysql_close($l_oDBlink);

          

            
// 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

    
$l_oServer->service($HTTP_RAW_POST_DATA);

?>