<?php
ini_set('memory_limit', '512M');
function send_WS_Request($webservices_url, $request, $log=false) {
	$logfile = "";
	$ws = "";
	// Create the client instance
	$client = new nusoap_client($webservices_url, true);
	// Check for an error
	$err = $client->getError();
	if ($err) {
	    // Display the error
	    if($log) {
	    	$logfile .= 'Constructor error. ' . "\n";
	    	$logfile .= implode(" | ", $err) . "\n";
	    }
	    // At this point, you know the call that follows will fail
	}
	// Call the SOAP method
	$result = $client->call(
			'getRequestedServer', 
			array(
				'SERVER_NAME' 		=> $request['server_name'],
	    		'FUNCTION_NAME' 	=> $request['function_name'],
		    	'FUNCTION_PARAMS' 	=> $request['function_params']
			)
		);
	// Check for a fault
	if ($client->fault) {
	     if($log) {
	    	$logfile .= "Faulty result" . "\n";
	    	$logfile .= implode(" | ", $result) . "\n";
	     }
	} 
	else {
	    // Check for errors
	    $err = $client->getError();
	    if ($err) {
	        // Display the error
	        if($log) {
	        	$logfile .= implode(" | ", $err) . "\n";
	        }
	    } else {
	        $ws = unserialize(base64_decode($result));
	    }
	}
	
	if($log) {
		// Display the request and response
		echo '<h2>Request</h2>';
		echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
		echo '<h2>Response</h2>';
		echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
		// Display the debug messages
		echo '<h2>Debug</h2>';
		echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
	}
	
	if($logfile == "") return $ws;
	else return false;
}
?>