<?php

// SOAP test client


exit();



function getLastRequestFormated($cli) {
	$dom = new DomDocument;
	$dom->loadXML($cli->__getLastRequest());
	$dom->formatOutput = true;
	$dom = $dom->saveXML();
	$dom = preg_replace_callback('`(<\??[^\s>]*)([^>]*)(\??>)`', 'tag', $dom);
	return $dom;
}	

function tag($m) {
	$m = $m[0];
	if(preg_match('`<\[CDATA\[([^]]|\][^]]|\]\][^>])*\]\]>`i', $m)) return '<span style="color:purple">'.htmlentities($m).'</span>';
	$start = preg_replace('`^(<\??[^\s>=]*).*`', '\1', $m);
	$end = preg_replace('`[^?>]`', '', substr($m, -2));
	$mid = trim(substr($m, strlen($start), -1 * strlen($end)));
	if(preg_match_all('`([^=]+)=([^\s]*|"[^"]*")\s*`', $mid, $m)) {
		$mid = '';
		for($i=0; $i<count($m[0]); $i++) $mid .= ' <span style="color:green">'.htmlentities($m[1][$i]).'</span>=<span style="color:orange">'.htmlentities($m[2][$i]).'</span>';
	}
	return '<span style="color:blue">'.htmlentities($start).$mid.htmlentities($end).'</span>';
}

ini_set('soap.wsdl_cache_enabled', '0');

//header('Content-Type: text/plain; charset=UTF-8');
try {
	$cli = new SoapClient('url_to_farm.wsdl_file', array('trace' => 1, 'soap_version'  => SOAP_1_1));
	
	if(isset($_GET['a'])) {
		print_r($cli->authenticateRemoteAppAndRun('remoteapp', 'pwd', 'animalExists', array('name' => $_GET['a'])));
	}elseif(isset($_GET['d'])) {
		print_r($cli->authenticateRemoteAppAndRun('remoteapp', 'pwd', 'animalDelete', array('name' => $_GET['d'])));
	}elseif(isset($_GET['new'])) {
		print_r($cli->authenticateRemoteAppAndRun('remoteapp', 'pwd', 'animalCreate', array('name' => $_GET['new'])));
	}
	
	if(isset($_GET['a']) || isset($_GET['d']) || isset($_GET['new'])) echo '<pre>'.getLastRequestFormated($cli).'</pre>';
	
	foreach($cli->authenticateRemoteAppAndRun('remoteapp', 'pwd', 'animalsList') as $w) echo '<a href="'.$w['url'].'">'.$w['name'].'</a> <a href="?a='.$w['name'].'">check</a> <a href="?d='.$w['name'].'">del</a><br />';
	//print_r($cli->authenticateRemoteAppAndRun('remoteapp', 'pwd', 'animalsList'));
	echo '<a href="?new=foo'.time().'">NEW</a><br />';
	
	echo '<pre>'.getLastRequestFormated($cli).'</pre>';
}catch(SoapFault $f) {
	die('SOAP::Client fault : '.$f->getMessage());
}
?>
