<?php

/***************************************
*	iTrack web service sample
****************************************/

function my_print($label,$obj) {
	print "<h2>".$label."</h2>";
	echo "<pre>";
	echo htmlspecialchars(var_export($obj,1));
	echo "</pre>";
}

if ($_POST['psw'] != '') {
	$username = $_POST['username'];
	$psw = $_POST['psw'];
	$host = "https://api.itrack.hu/wsif/services/DefaultWebService?wsdl";
	try {
		//try to connect
		$client = new SoapClient($host, array("exceptions" => 1));

		//getting the list of available functions
		$farray = $client->__getFunctions();
		my_print("Available functions in DefaultWebService",$farray);
		
		//try to login;
		$result = $client->login(array('login' => $username,'password' => $psw));	
		$sessionid = $result->return; 		

		//getting the list of vehicles
		$result = $client->getTargets(array('session_id' => $sessionid));	
		my_print("Vehicles",$result->return);

		//getting the last positions of vehicles
		$xml = simplexml_load_string($result->return);
		$targets = array();
		foreach($xml->target as $target) {
			foreach($target->attributes() as $name => $value) {
				if($name=='id') {
					array_push($targets , $value);
				}
			}
		}
		$result = $client->getLastCoordinates(array('session_id' => $sessionid, 'targets' => $targets));	
		my_print("Last positions",$result->return);

	} catch  (SoapFault $E) {
		echo $E->faultstring;   
	}
} else {


?>
	<h1>iTrack webservice test</h1>
	<form id="login" action="itracksoapsample.php" method="post">
		
		username<input id="username" name="username" size="10" type="text" value="" />
		psw<input id="psw" name="psw" size="10" type="text" value="" /><br />
		
		<input id="submit" name="submit" type="submit" value="login" />
	</form>

<?php

}

?>
