SOAP : NuSOAP

Server Code

<?php
require_once ('../lib/nusoap.php');
$server = new soap_server();
$namespace = 'urn:dingdongtictic';
$server -> configureWSDL('ddtt-gen-prov', $namespace);

$server->wsdl->addComplexType(
	'parameter',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'key' => array(
			'name' => 'key',
			'type' => 'xsd:string',
		),
		'value' => array(
			'name' => 'value',
			'type' => 'xsd:string',
		),
	)
);

$server->wsdl->addComplexType(
	'parameters',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'param' => array(
			'name' => 'param',
			'type' => 'tns:parameter',
			'maxOccurs' => 'unbounded',
			'minOccurs' => '0',
		)
	)
);

$server->wsdl->addComplexType(
	'responseType',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'resultCode' => array(
			'name' => 'resultCode',
			'type' => 'xsd:string',
		),
		'resultDesc' => array(
			'name' => 'resultDesc',
			'type' => 'xsd:string',
		),
	)
);

$server->register('login',                							// method name
    array('userName' => 'xsd:string', 'password' => 'xsd:string'),  // input parameters
    array('response' => 'tns:responseType'),    					// output parameters
    $namespace,                      	  							// namespace
    $namespace . '#login',                							// soapaction
    'rpc',                                							// style
    'encoded',                            							// use
    'This command will be used to get the authentication key'      	// documentation
);

$server->register('activateService',
    array('identifier' => 'xsd:string', 'paramMap' => 'tns:parameters'),
	array('response' => 'tns:responseType'),
	$namespace,
	$namespace . '#activateService',
	'rpc',
	'encoded',
	'This command will be used for initial activation & reactivation'
);

function login($username, $password) {
        return array('resultCode' => $username, 'resultDesc' => $password);
}

function activateService($identifier, $paramMap) {
		return array('resultCode' => $identifier, 'resultDesc' => print_r($paramMap, TRUE));
}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

Client Code

<?php

require_once 'lib/nusoap.php';

class GenProxy {

	private static $singleton;
	private $client;
	private $wsdl_path;
	private $username;
	private $password;
	private $authKey;

	private function __construct($url) {
		$this -> wsdl_path = $url;
		$this -> client = new nusoap_client($this -> wsdl_path, true);
	}

	public static function getProxy($url) {
		if (!isset(self::$singleton)) {
			self::$singleton = new GenProxy($url);
		}
		return self::$singleton;
	}

	public function getWsdl() {
		echo $this -> wsdl_path;
	}

	// change the access modifier to private
	public function getAuthKey() {
		echo $this -> authKey;
	}

	private function debugRequest() {
		echo '<h2>Request</h2>';
		echo '<pre>' . htmlspecialchars($this -> client -> request, ENT_QUOTES) . '</pre>';
	}

	private function debugResponse() {
		echo '<h2>Response</h2>';
		echo '<pre>' . htmlspecialchars($this -> client -> response, ENT_QUOTES) . '</pre>';
	}

	private function debugResult($client, $result) {
		if ($client -> fault) {
			echo '<h2>Fault</h2><pre>';
			print_r($result);
			echo '</pre>';
		} else {
			// Check for errors
			$err = $client -> getError();
			if ($err) {
				// Display the error
				echo '<h2>Error</h2><pre>' . $err . '</pre>';
			} else {
				// Display the result
				echo '<h2>Result</h2><pre>';
				print_r($result);
				echo '</pre>';
			}
		}
	}

	public function login($username, $password) {
		$this -> username = $username;
		$this -> password = $password;
		$result = $this -> client -> call('login', array('userName' => $username, 'password' => $password));
		$this -> debugRequest();
		//$this -> debugResponse();
		// check result for errors
		// store auth key
		$this -> authKey = $result['resultCode'];
	}

	public function activate() {
		$arr = array('identifier' => '506', 'paramMap' => array('param' => array( array('key' => 'key1', 'value' => 'value1'), array('key' => 'key2', 'value' => 'value2'))));
		echo print_r($arr);
		$result = $this -> client -> call('activateService', $arr);
		$this -> debugRequest();
		$this -> debugResponse();
		//echo $result['resultDesc'];
	}

	public function deactivate() {
		echo $this -> client -> call('deactivateService', array('identifier' => '506', 'paramMap' => array('key' => 'key 1', 'value' => 'value 1')));
	}

	public function modify() {
		echo $this -> client -> call('modifyService', array('identifier' => '507', 'oldParamMap' => 'should be an array type thing', 'newParamMap' => 'should be an array type thing'));
	}

}
?>

Usage

<?php
require_once 'GenProxy.php';
$proxy = GenProxy::getProxy(<path to server wsdl>);
$proxy -> login('banda', 'bandapwd');
$proxy -> activate();
$proxy -> getAuthKey();
?>
Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: