<?php


$arrFrom = explode(',',$_GET['from']);
$arrTo = explode(',', $_GET['to']);

$from = $arrFrom[1] . "," . $arrFrom[0];
$to = $arrTo[1] . "," . $arrTo[0];

$compare = strcmp($from, $to);
if ( $compare != 0 ){
	
	if ( $_GET['routeType'] == "fastest" ){
		$postfields = '{"coordinates":[['.$from.'],['.$to.']],"preference":"fastest","units":"km"}';
	}else{
		$postfields = '{"coordinates":[['.$from.'],['.$to.']],"preference":"shortest","units":"km"}';
	}
	

	$ch = curl_init();


	curl_setopt($ch, CURLOPT_URL, "http://api.openrouteservice.org/v2/directions/driving-car");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_HEADER, FALSE);

	curl_setopt($ch, CURLOPT_POST, TRUE);

	curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	  "Accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8",
	  "Authorization: 5b3ce3597851110001cf6248c2dbccb21e3e496aab43381b9907b6ac",
	  "Content-Type: application/json; charset=utf-8"
	));
	 // karl 5b3ce3597851110001cf62480b522d267ac14da08dc0c8691dcbe2fb
	 // david 5b3ce3597851110001cf6248c2dbccb21e3e496aab43381b9907b6ac
	 // vzwmobiel 5b3ce3597851110001cf6248ae6f648ff7a54c819790a710e686190e
	 
	$response = curl_exec($ch);
	curl_close($ch);

	$obj = json_decode($response, true);
	
	//print_r ($obj);
	
	$distance = $obj["routes"][0]["summary"]["distance"];
	$time = $obj["routes"][0]["summary"]["duration"];

	$xml = "<respone>";
	$xml = $xml . "<route>";
	$xml = $xml . "<distance>" . $distance . "</distance>";
	$xml = $xml . "<time>" . $time . "</time>";
	$xml = $xml . "</route>";
	$xml = $xml ."</respone>";



}else{
	$xml = "<respone>";
	$xml = $xml . "<route>";
	$xml = $xml . "<distance>0</distance>";
	$xml = $xml . "<time>0</time>";
	$xml = $xml . "</route>";
	$xml = $xml ."</respone>";
	}

echo $xml;

?>