title: google geo coding with json and php
body: <p>My php file:</p>
  
  <pre><code>&lt;?php
  
   if (isset($_POST['query'])) {  
      $query = $_POST['query'];
  
  $url='http://maps.googleapis.com/maps/api/geocode/json?address=';
  $callback="&amp;callback=?";
  $sensor='&amp;sensor=false';
  $result= $url.$query.$sensor.$callback;
  curl_init ($result);
  
   }
  ?&gt;
  </code></pre>
  
  <p>My html file:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  
  
      // when the user clicks the button
      $("button").click(function(){
  
  
            $.getJSON("geo.php",function(json){
  
               $('#results').append('&lt;p&gt;Latitude : ' + json.results[9].geometry.location.lat+ '&lt;/p&gt;');
               $('#results').append('&lt;p&gt;Longitude: ' + json.results[9].geometry.location.lng+ '&lt;/p&gt;');
  
  });
  
            // get the json file
      });
  });
  
  
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;input type="text" id="query" /&gt;&lt;button&gt;Get Coordinates&lt;/button&gt;
  &lt;div id="results"&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I get this error when i used firbug:</p>
  
  <pre><code>json is null
  $('#results').append('&lt;p&gt;...s[9].geometry.location.lat+ '&lt;/p&gt;'); 
  </code></pre>
  
  <p>Google's json response:</p>
  
  <pre><code>{
    "status": "OK",
    "results": [ {
      "types": [ "street_address" ],
      "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
      "address_components": [ {
        "long_name": "1600",
        "short_name": "1600",
        "types": [ "street_number" ]
      }, {
        "long_name": "Amphitheatre Pkwy",
        "short_name": "Amphitheatre Pkwy",
        "types": [ "route" ]
      }, {
        "long_name": "Mountain View",
        "short_name": "Mountain View",
        "types": [ "locality", "political" ]
      }, {
        "long_name": "California",
        "short_name": "CA",
        "types": [ "administrative_area_level_1", "political" ]
      }, {
        "long_name": "United States",
        "short_name": "US",
        "types": [ "country", "political" ]
      }, {
        "long_name": "94043",
        "short_name": "94043",
        "types": [ "postal_code" ]
      } ],
      "geometry": {
        "location": {
          "lat": 37.4219720,
          "lng": -122.0841430
        },
        "location_type": "ROOFTOP",
        "viewport": {
          "southwest": {
            "lat": 37.4188244,
            "lng": -122.0872906
          },
          "northeast": {
            "lat": 37.4251196,
            "lng": -122.0809954
          }
        }
      }
    } ]
  }
  </code></pre>
  
  <p>EDITED:</p>
  
  <pre><code>&lt;?php
  if (isset($_POST['query'])) {  
      $query = $_POST['query'];
  $url='http://maps.googleapis.com/maps/api/geocode/json?address=';
  $callback="&amp;callback=?";
  $sensor='&amp;sensor=false';
  $result= $url.$query.$sensor.$callback;
  $resp = file_get_contents($result);
                  header('Cache-Control: no-cache, must-revalidate');
                  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                  header('Content-type: application/json');
  echo $resp;
  }
  </code></pre>