title: Using PHP/Google Maps to Calculate Distances
body: <p>I'm trying to create a simple (or so I thought) PHP/Google Maps function which would allow me to calculate a distance between two addresses. I'm new to PHP, and here's what I've got so far, however, something's wrong. </p>
  
  <pre><code>&lt;section class="group"&gt;
  
          &lt;label for="start"&gt;Start:&lt;/label&gt;
          &lt;input type="text" name="start" id="start" value="" /&gt;
  
          &lt;label for="finish"&gt;Finish&lt;/label&gt;
          &lt;input type="text" name="finish" id="finish" value="" /&gt;
  
          &lt;input type="button" value="Calculate distance" id="calculate-distance" /&gt;
  
          &lt;div id="results"&gt;&lt;/div&gt;
  
      &lt;/section&gt;
  
      &lt;?php
      function getLatLong($address) {
  
      $address = str_replace(' ', '+', $address);
      $url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&amp;sensor=false';
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $geoloc = curl_exec($ch);
  
      $json = json_decode($geoloc);
      return array($json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat,
      $json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng);
  
      }
  
      function Haversine($start, $finish) { 
  
      $theta = $start[1] - $finish[1];
      $distance = (sin(deg2rad($start[0])) * sin(deg2rad($finish[0]))) + (cos(deg2rad($start[0])) * cos(deg2rad($finish[0])) * cos(deg2rad($theta)));
      $distance = acos($distance);
      $distance = rad2deg($distance);
      $distance = $distance * 60 * 1.1515;
  
      return round($distance, 2);
       }
  
      $start = getLatLong('start');
      $finish = getLatLong('finish');
  
      $distance = Haversine($start, $finish);
  
      print('&lt;p&gt;The distance between ['.$start[0].', '.$start[1].'] and   ['.$finish[0].',    '.$finish[1].'] is '.$distance.' miles ('.($distance * 1.609344).'  km).&lt;/p&gt;');
  ?&gt;
  </code></pre>