title: PHP: Distance between 2 addresses with Google Maps?
body: <p>I'm trying to get the distance between 2 locations through Google Maps API using the below PHP script, which apparently works (their hosted test url works, added below), but when I use the code they provide on my own server it doesn't seem to do anything... PHP is working just fine by the way, and it doesn't seem to be doing anything funny that would require me turning something on in my ini file.</p>
  
  <p>I'm not sure what the issue is... been at this all day. </p>
  
  <p>Any ideas? Maybe it is my .ini? </p>
  
  <p>I'm not sure what I'm missing here. I'm using a shared host, but that shouldn't make a difference. It should technically work as is, any way that is why I'm here - I need some insight.</p>
  
  <hr>
  
  <p>Website with source code:
  <a href="http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/#comment-24117" rel="nofollow">http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/#comment-24117</a></p>
  
  <p>Their live demo: <a href="http://demos.codexworld.com/distance-between-two-addresses-google-maps-api-php/" rel="nofollow">http://demos.codexworld.com/distance-between-two-addresses-google-maps-api-php/</a></p>
  
  <p>My full test code:</p>
  
  <pre><code>&lt;?php
  /**
  *
  * Author: CodexWorld
  * Function Name: getDistance()
  * $addressFrom =&gt; From address.
  * $addressTo =&gt; To address.
  * $unit =&gt; Unit type.
  *
  **/
  
      function getDistance($addressFrom, $addressTo, $unit){
          //Change address format
          $formattedAddrFrom = str_replace(' ','+',$addressFrom);
          $formattedAddrTo = str_replace(' ','+',$addressTo);
  
          //Send request and receive json data
          $geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&amp;sensor=false');
          $outputFrom = json_decode($geocodeFrom);
          $geocodeTo = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&amp;sensor=false');
          $outputTo = json_decode($geocodeTo);
  
          //Get latitude and longitude from geo data
          $latitudeFrom = $outputFrom-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
          $longitudeFrom = $outputFrom-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng;
          $latitudeTo = $outputTo-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
          $longitudeTo = $outputTo-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng;
  
          //Calculate distance from latitude and longitude
          $theta = $longitudeFrom - $longitudeTo;
          $dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
          $dist = acos($dist);
          $dist = rad2deg($dist);
          $miles = $dist * 60 * 1.1515;
          $unit = strtoupper($unit);
          if ($unit == "K") {
              return ($miles * 1.609344).' km';
          } else if ($unit == "N") {
              return ($miles * 0.8684).' nm';
          } else {
              return $miles.' mi';
          }
      }
  
      if (!empty($_POST['addrFrom']) &amp;&amp; !empty($_POST['addrTo'])) { 
          $addressFrom = htmlspecialchars($_POST['addrFrom']);
          $addressTo = htmlspecialchars($_POST['addrTo']);
          $distance = getDistance($addressFrom, $addressTo, "K");
          $success = true;
      }
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
      &lt;form method="post"&gt;
          &lt;p class="distance"&gt;&lt;span&gt;Distance:&lt;?php if ($success == true) { echo ' ' . $distance; } ?&gt;&lt;/span&gt; &lt;/p&gt;
          &lt;p&gt;&lt;label&gt;Address From&lt;/label&gt;&lt;input type="text" name="addrFrom" value=""&gt;&lt;/p&gt;
          &lt;p&gt;&lt;label&gt;Address To&lt;/label&gt;&lt;input type="text" name="addrTo" value=""&gt;&lt;/p&gt;
          &lt;p&gt;&lt;input type="submit" name="submit" value="Calculate Distance"&gt;&lt;/p&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>