title: Using PHP (curl) to pull data from JSON (Google Maps API)
body: <p>I'm fairly new to JSON, and I'm trying to get the latitude and longitude of a geocoded city from the Google Maps API using curl. The function I'm using is:</p>
  
  <pre><code>function geocode($city){
     $cityclean = str_replace (" ", "+", $city);
     $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&amp;sensor=false";
  
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $details_url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $geoloc = json_decode(curl_exec($ch), true);
  
     $step1 = $geoloc['results'];
     $step2 = $step1['geometry'];
     $coords = $step2['location'];
  
     print $coords['lat'];
     print $coords['lng'];
  
  }
  </code></pre>
  
  <p>The goal of all of that is to pull the lat and lng values from the Results -> Geometry -> Location array of the following JSON:</p>
  
  <pre><code>{
    "status": "OK",
    "results": [ {
      "types": [ "locality", "political" ],
      "formatted_address": "Westminster, London, UK",
      "address_components": [ {
        "long_name": "London",
        "short_name": "London",
        "types": [ "locality", "political" ]
      }, {
        "long_name": "Westminster",
        "short_name": "Westminster",
        "types": [ "administrative_area_level_3", "political" ]
      }, {
        "long_name": "Greater London",
        "short_name": "Greater London",
        "types": [ "administrative_area_level_2", "political" ]
      }, {
        "long_name": "England",
        "short_name": "England",
        "types": [ "administrative_area_level_1", "political" ]
      }, {
        "long_name": "United Kingdom",
        "short_name": "GB",
        "types": [ "country", "political" ]
      } ],
      "geometry": {
        "location": {
          "lat": 51.5001524,
          "lng": -0.1262362
        },
        "location_type": "APPROXIMATE",
        "viewport": {
          "southwest": {
            "lat": 51.3493528,
            "lng": -0.3783580
          },
          "northeast": {
            "lat": 51.7040647,
            "lng": 0.1502295
          }
        },
        "bounds": {
          "southwest": {
            "lat": 51.3493528,
            "lng": -0.3783580
          },
          "northeast": {
            "lat": 51.7040647,
            "lng": 0.1502295
          }
        }
      }
    } ]
  }
  </code></pre>
  
  <p>However, it doesn't print anything. I know that the function successfully retrieves the JSON from Google and brings it to my server, as I did a print statement for the 'status' value and it returned 'OK'. The problem seems to be when I try and delve deeper into the JSON.</p>
  
  <p>Sorry if it's a straightforward problem, but like I said, I'm new to this and it's driving me crazy now.</p>
  
  <p>Thanks a lot! :)</p>