<?php 
require 'db.php';

echo json_encode(GetDrivingDistance(8.680458214672084, 77.74066197472887, 8.732642739140635, 77.71107998831792));

function GetDrivingDistance($lat1, $long1, $lat2, $long2)
{
    $url = "https://maps.googleapis.com/maps/api/directions/json?origin=".$lat1.",".$long1."&destination=".$lat2.",".$long2."&sensor=false&mode=driving&alternatives=true&key=AIzaSyC38nyMdGmo-rtoKrGIQ7AYNGfV7--uUHY";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);
    $dist = $response_a['routes'][0]['legs'][0]['distance']['text'];
    $dist_value = $response_a['routes'][0]['legs'][0]['distance']['value'];
    $time = $response_a['routes'][0]['legs'][0]['duration']['text'];
    $time_value = $response_a['routes'][0]['legs'][0]['duration']['value'];
    return array('distance' => $dist, 'distance_val' => $dist_value);
}