简体   繁体   中英

How can i retrieve a particular data from a content obtained using file_get_contents() function in PHP?

My code is

$result = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$stateName.",".$districtName.",".$cityName."&sensor=true");

where $stateName , $districtName , $cityName are the values given by the user
After entering Kerala , Ernakulam , Angamaly as the stateName , districtName and cityName i got a result like this:

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Angamaly",
                    "short_name": "Angamaly",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Ernakulam",
                    "short_name": "Ernakulam",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Kerala State",
                    "short_name": "Kerala State",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "India",
                    "short_name": "IN",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Angamaly, Kerala State, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 10.2464704,
                        "lng": 76.39544959999999
                    },
                    "southwest": {
                        "lat": 10.1585335,
                        "lng": 76.3500451
                    }
                },
                "location": {
                    "lat": 10.212894,
                    "lng": 76.380601
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 10.2464704,
                        "lng": 76.39544959999999
                    },
                    "southwest": {
                        "lat": 10.1585335,
                        "lng": 76.3500451
                    }
                }
            },
            "types": [
                "locality",
                "political"
            ]
        }
    ],
    "status": "OK"
}

I need the latitude and longitude of the place. How can I retrieve the lat and lng from
geometry->bounds->northeast ?

$json = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$stateName.",".$districtName.",".$cityName."&sensor=true");

$response = json_decode($json);
echo $response->results[0]->geometry->bounds->northeast->lat;
echo $response->results[0]->geometry->bounds->northeast->lng;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM