NuSOAP and registering classes/objects

Discussion in 'PHP' started by grutland, Jun 2, 2011.

  1. #1
    Hello,

    Been looking into turning our current API into one that will allow WSDL access.
    I have looked at WSDL's myself and they mean absolutely nothing and look like gibberish!
    NuSOAP seems to be a good way of being able to generate the WSDL.

    The problem I'm having is that I'm trying to expose a class through our API.
    The problem with NuSOAP is that it only seems to allow you to add functions.

    I wonder if any one can help...
    This is my current code (varialbe name etc have been generalised)
    require_once 'nusoap.php';
    
    $nusoap = new soap_server;
    
    $namespace = 'urn:testapi';
    $nusoap->configureWSDL('testapi', $namespace);
    
    foreach(get_class_methods('testapi') as $method){
    	$reflect = new ReflectionMethod('testapi', $method);
    	if($reflect->isPublic() && !$reflect->isStatic()){
    		$input = array();
    		foreach($reflect->getParameters() as $parameter)
    			$input[$parameter->name] = 'xsd:string';
    		$nusoap->register($method, $input, array('return' => 'xsd:string'), $namespace, false, 'rpc', 'encoded');		
    	}
    }
    
    $nusoap->service(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');
    PHP:
    The problem is that the function doesn't exist, so I tried adding in an eval() after the register() call.
    Like this:
    $args = null;
    foreach($input as $key => $val)
    	$args .= (!empty($args) ? ', ' : null) .'$'. $key;
    eval("function $method(){ return print_r(func_get_args(), true); }");
    PHP:
    Which works well until I want to pass in an array which then gets passed back as "Array".

    Is there any way of registering a class or object through NuSOAP instead of attempting the eval... or getting eval() to work to allow for arrays being passed in.

    Thanks in advance.
    Gary
     
    grutland, Jun 2, 2011 IP
  2. grutland

    grutland Active Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #2
    Just in case any one is interested, I have worked out a solution.

    if(isset($_REQUEST['wsdl'])){
    	require_once ABS_ROOT .'/classes/api/nusoap/nusoap.php';
    	$server = new nusoap_server;
    	
    	$server->configureWSDL('etouchesapi', 'urn:testapi');
    	
    	foreach(get_class_methods('testapi') as $method){		
    		$reflect = new ReflectionMethod('testapi', $method);
    		if($reflect->isPublic() && !$reflect->isStatic()){
    			$input = array();
    			foreach($reflect->getParameters() as $parameter)
    				$input[$parameter->name] = 'xsd:string';
    			$server->register($method, $input, array('return' => 'xsd:string'), 'uri:testapi', false, 'rpc', 'encoded');
    		}
    	}
    	
    	$server->service($isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');
    }else{
    	$server = new SoapServer(null, array('uri' => 'urn://testapi'));
    	$server->setObject(new testapi);
    	$server->setPersistence(SOAP_PERSISTENCE_SESSION);
    	$server->handle();
    }
    PHP:
    Hope this helps any one who has ever run into the same problem as me.
     
    grutland, Jun 2, 2011 IP