Trying to make a nusoap webservices example from ZEND work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Trying to make a nusoap webservices example from ZEND work

Post by raghavan20 »

I am trying to make an example from ZEND work.
There are basically two files
1. server file - taxCalc.php
2. client file - taxCalcClient.php

Function provided: Tax calculation
Function prototype: float taxCalc ($rate, $sub)

All nusoap files are loaded into a sub-directory nusoap from where the client file is running.

I provide codes below and the links to the files in action.
Can anyone find out where is the problem? I am sure that the function in the server side is not called.
The error it returns

Code: Select all

Code: Client
String: method '' not defined in service
Client file

Code: Select all

<html> 
<head> 
<title>Tax calculator Client</title> 
</head> 
<body> 
<h1>Tax calculated:</h1> 
<?php 
/** 
* taxCalcClient.php 
* An example PHP script which uses NuSOAP to access 
* a tax calculator function. 
* 
* Created on August 7, 2002, 6:11 PM 
* @author  Sean Campbell 
* @version 1.0 
*/ 

/* Include the NuSOAP library. */ 
require_once('nusoap/nusoap.php'); 

/* Initialize the client's parameter list. */ 
$param = array('rate' => $_GET['rate'],  
               'sub' => $_GET['sub'] 
               ); 
print_r($param);
/* Create a new client by providing the endpoint to the 
* constructor. */ 
$client = new soapclient( 
                    'http://testdomain.farvista.net/taxCalc.php' 
                    ); 

/* Call the taxCalc() function, passing the parameter list. */ 
$response = $client->call('taxCalc', $param); 

/*  handle any SOAP faults. */ 
if ($client->fault) { 
    echo "FAULT: <p>Code: {$client->faultcode}<br />"; 
    echo "String: {$client->faultstring}"; 
} else { 
    echo "$" . $response; 
} 
?> 
</body> 
</html>
Server file

Code: Select all

<?php 
/** 
* taxCalc.php 
* An example PHP script which uses NuSOAP to provide a 
* tax calculator function. 
* 
* Created on August 7, 2002, 6:11 PM 
* @author  Sean Campbell 
* @version 1.0 
*/ 

/* Include the NuSOAP library. */ 
require_once('nusoap/nusoap.php');  

/* Create a new SOAP server instance. */ 
$server = new soap_server; 

/* Register the taxCalc function for publication. */ 
$server->register('taxCalc');  

/** 
* Calculates the total (including tax) for a purchase based on the 
* rate and subtotal provided. 
* 
* @param string $rate          The tax rate in percent. 
* @param string $sub           The subtotal for the purchase. 
* 
* @return float $total            The total for the purchase. 
* 
* @author  Sean Campbell 
* @version 1.0 
*/ 
function taxCalc ($rate, $sub) 
{ 
    if ($rate == '' || $rate <= 0) { 
        /* Return a SOAP fault indicating a negative or 
         * zero tax rate was transmitted. */ 
        return new soap_fault( 
            'Client', '', 
            'Must supply a positive, non-zero tax rate.','' 
        ); 
    } 

    if ($sub == '' || $sub <= 0) { 
        /* Return a SOAP fault indicating a negative or 
     * zero subtotal was transmitted. */ 
        return new soap_fault( 
            'Client', '', 
            'Must supply a positive, non-zero subtotal.', '' 
        ); 
    } 
     
    /* Return the generated total for the purchase. */ 
    return (($rate / 100) * $sub) + $sub;  
}  


/* Begin the HTTP listener service and exit. */ 
$server->service($_SERVER['HTTP_RAW_POST_DATA']); 
exit(); 
?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

For more information, I come to find that the service is not registered and it's the equivalent of the error returned.

Basically the registering statement is

Code: Select all

$server->register('taxCalc');
I replaced with this statement but even then it does not work

Code: Select all

$server->register('taxCalc', array("rate"=>"xsd:integer", "sub"=>"xsd:integer"), array("return"=>"xsd:integer"));
The registering method in nusoap.php

Code: Select all

/**
	* register a service function with the server
	*
	* @param    string $name the name of the PHP function, class.method or class..method
	* @param    array $in assoc array of input values: key = param name, value = param type
	* @param    array $out assoc array of output values: key = param name, value = param type
	* @param	mixed $namespace the element namespace for the method or false
	* @param	mixed $soapaction the soapaction for the method or false
	* @param	mixed $style optional (rpc|document) or false Note: when 'document' is specified, parameter and return wrappers are created for you automatically
	* @param	mixed $use optional (encoded|literal) or false
	* @param	string $documentation optional Description to include in WSDL
	* @param	string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
	* @access   public
	*/
	function register($name,$in=array(),$out=array(),$namespace=false,$soapaction=false,$style=false,$use=false,$documentation='',$encodingStyle=''){
		global $HTTP_SERVER_VARS;

		if($this->externalWSDLURL){
			die('You cannot bind to an external WSDL file, and register methods outside of it! Please choose either WSDL or no WSDL.');
		}
		if (! $name) {
			die('You must specify a name when you register an operation');
		}
		if (!is_array($in)) {
			die('You must provide an array for operation inputs');
		}
		if (!is_array($out)) {
			die('You must provide an array for operation outputs');
		}
		if(false == $namespace) {
		}
		if(false == $soapaction) {
			if (isset($_SERVER)) {
				$SERVER_NAME = $_SERVER['SERVER_NAME'];
				$SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];
			} elseif (isset($HTTP_SERVER_VARS)) {
				$SERVER_NAME = $HTTP_SERVER_VARS['SERVER_NAME'];
				$SCRIPT_NAME = $HTTP_SERVER_VARS['SCRIPT_NAME'];
			} else {
				$this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
			}
			$soapaction = "http://$SERVER_NAME$SCRIPT_NAME/$name";
		}
		if(false == $style) {
			$style = "rpc";
		}
		if(false == $use) {
			$use = "encoded";
		}
		if ($use == 'encoded' && $encodingStyle = '') {
			$encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
		}

		$this->operations[$name] = array(
	    'name' => $name,
	    'in' => $in,
	    'out' => $out,
	    'namespace' => $namespace,
	    'soapaction' => $soapaction,
	    'style' => $style);
        if($this->wsdl){
        	$this->wsdl->addOperation($name,$in,$out,$namespace,$soapaction,$style,$use,$documentation,$encodingStyle);
	    }
		return true;
	}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

EDITED

Post by raghavan20 »

As a turning point to the investigation, the problem seems to come from a different area from the nusoap classes.
All classes report a common error...missing nusoap base class.
I find that all soap client and server classes inherit base class which is not included surprisingly....

Example start of class.soapclient.php....you can find that there is no include or require....I have now included nusoap base class file

Code: Select all

<?php




/**
*
* soapclient higher level class for easy usage.
*
* usage:
*
* // instantiate client with server info
* $soapclient = new soapclient( string path [ ,boolean wsdl] );
*
* // call method, get results
* echo $soapclient->call( string methodname [ ,array parameters] );
*
* // bye bye client
* unset($soapclient);
*
* @author   Dietrich Ayala <dietrich@ganx4.com>
* @version  $Id: class.soapclient.php,v 1.52 2005/07/27 19:24:42 snichol Exp $
* @access   public
*/
class soapclient extends nusoap_base  {
EDIT:::::
But more to the confusion all the classes declared in separate files are included in the nusoap.php file itself.
So there appears no need for inclusion of any other file other than nusoap.php since it contains all the classes in itself to work properly...now we are back to the start of the problem. :?
Post Reply