{
"results" : [
{
"address_components" : [
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "West Delhi",
"short_name" : "West Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.88981589999999,
"lng" : 77.34181459999999
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
},
"location" : {
"lat" : 28.635308,
"lng" : 77.22496
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.88981589999999,
"lng" : 77.34181459999999
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
How do i parse the above json string with php
Trying the following code out but no success:
$url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");
$json = json_decode($url);
foreach($json->results as $item) {
$location_item = array(
'address' => $item->formatted_address,
);
}
I am getting blank page the code doesn't work please help i am new to php
I am getting blank page the code doesn't work please help i am new to
php
Your code actually works , maybe you are not printing the data that is why you are getting a blank page.
Tested
<?php
$url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");
$json = json_decode($url);
foreach($json->results as $item) {
$location_item = array(
'address' => $item->formatted_address,
);
}
print_r($location_item);
OUTPUT :
Array
(
[address] => New Delhi, Delhi, India
)
Related
So I am retrieving a json file from an API, and I want to grab the data from object 'place_id'.
I have been looking online and I have seen how to grab elements from json files, however I can't understand what I am doing wrong, as my code gives me an error:
Warning: Undefined array key "place_id" in C:\xampp\htdocs\cw\tester.php on line 15
This is my php code where I try to grab the data from 'place_id':
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results']['place_id'];
this is my json file:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"types" : [ "establishment", "point_of_interest", "tourist_attraction", "zoo" ]
}
],
"status" : "OK"
}
Apologies as I know it is obvious, but I can't seem to understand why! thanks!
This should do the trick? Notice the [0] that was added. Since results is actually an array.
<?php
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results'][0]['place_id'];
I'm using Google Map Api, and getting the result in JSON form and the output result is
{
"results" : [
{
"address_components" : [
{
"long_name" : "Plot No 1",
"short_name" : "Plot No 1",
"types" : [ "premise" ]
},
{
"long_name" : "B Block Road",
"short_name" : "B Block Rd",
"types" : [ "route" ]
},
{
"long_name" : "Rangpuri Extention B-Block Pocket-4",
"short_name" : "Rangpuri Extention B-Block Pocket-4",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South West Delhi",
"short_name" : "South West Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110037",
"short_name" : "110037",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Plot No 1, B Block Rd, Rangpuri Extention B-Block Pocket-4, New Delhi, Delhi 110037, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5379687,
"lng" : 77.121816
},
"southwest" : {
"lat" : 28.5377897,
"lng" : 77.1217006
}
},
"location" : {
"lat" : 28.5378435,
"lng" : 77.1217523
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 28.5392281802915,
"lng" : 77.12310728029151
},
"southwest" : {
"lat" : 28.5365302197085,
"lng" : 77.12040931970849
}
}
},
"partial_match" : true,
"place_id" : "ChIJibjV7RYcDTkRnaFq2Lg__b4",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
and I want to extract lat and lng,under the location object, write now I'm using this code
$mapcontent = json_decode($content,true);
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
where $content is the output result which mention above,by using the above code and it works but sometimes I'm getting error of undefined offest:0, on $lat and $lng line, is there any other way to retrieve specific data from the JSON Code, I already search on internet but I'm not getting any idea how to resolve this, please help me out
You can write the code like :
$mapcontent = json_decode($content,true);
$resultCount = count($mapcontent['results']);
if( $resultCount > 0 ) {
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}
It will work.
$url='your url';
$array = file_get_contents($url);
$array = (array)json_decode($array);
You can also write code like.
$mapcontent = json_decode($content,true);
if( true != empty($mapcontent['results'])) {
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}
First i use this link for get longtitude and longtitude from city name
https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN
...........................................................................................................................................................
When load this url i get this json format.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Los Angeles",
"short_name" : "Los Angeles",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Los Angeles County",
"short_name" : "Los Angeles County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Los Angeles, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.3373061,
"lng" : -118.1552891
},
"southwest" : {
"lat" : 33.7036519,
"lng" : -118.6681759
}
},
"location" : {
"lat" : 34.0522342,
"lng" : -118.2436849
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.3373061,
"lng" : -118.1552891
},
"southwest" : {
"lat" : 33.7036519,
"lng" : -118.6681759
}
}
},
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
I want to get location ==> lat = 34.0522342 and location ==> lng = -118.2436849 from json format.
Then i use this code.
<?php
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN';
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$location_lat = $json_data['results']['location']['lat'];
$location_lng = $json_data['results']['location']['lng'];
echo $location_lat;
echo $location_lng;
?>
But i not get any data ? How can i do ?
If you get the layers of data correct, then you should use...
$location_lat = $json_data['results'][0]['geometry']['location']['lat'];
$location_lng = $json_data['results'][0]['geometry']['location']['lng'];
echo $location_lat.PHP_EOL;
echo $location_lng;
Which outputs...
34.0522342
-118.2436849
you can edit to
$location_lat = $json_data['results'][0]['geometry']['location']['lat'];
$location_lng = $json_data['results'][0]['geometry']['location']['lng'];
I'm using the Google Geocoding API, and PHP (cURL) to get the JSON array.
That's all working fine.
The problem is that not every output is the same.
Example:
I have two addresses:
Dam 1, Amsterdam
Kurfürstendamm 1, Berlin
For the Amsterdam address the count of $array['results'][0]['address_components'] is 8 and the one for Berlin 7.
JSON output:
Amsterdam:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "Dam",
"short_name" : "Dam",
"types" : [ "route" ]
},
{
"long_name" : "Centrum",
"short_name" : "Centrum",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Noord-Holland",
"short_name" : "NH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Nederland",
"short_name" : "NL",
"types" : [ "country", "political" ]
},
{
"long_name" : "1012 JS",
"short_name" : "1012 JS",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Dam 1, 1012 JS Amsterdam, Nederland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.3740904,
"lng" : 4.8951464
},
"southwest" : {
"lat" : 52.37303319999999,
"lng" : 4.893589
}
},
"location" : {
"lat" : 52.3735618,
"lng" : 4.8943677
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 52.37491078029149,
"lng" : 4.895716680291502
},
"southwest" : {
"lat" : 52.3722128197085,
"lng" : 4.893018719708498
}
}
},
"place_id" : "ChIJ46bYaccJxkcRR1k-7Pl25Kg",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
Berlin:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "Kurfürstendamm",
"short_name" : "Kurfürstendamm",
"types" : [ "route" ]
},
{
"long_name" : "Bezirk Charlottenburg-Wilmersdorf",
"short_name" : "Bezirk Charlottenburg-Wilmersdorf",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Duitsland",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "10719",
"short_name" : "10719",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kurfürstendamm 1, 10719 Berlin, Duitsland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.50440769999999,
"lng" : 13.3337799
},
"southwest" : {
"lat" : 52.5043971,
"lng" : 13.3337737
}
},
"location" : {
"lat" : 52.50440769999999,
"lng" : 13.3337737
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 52.50575138029149,
"lng" : 13.3351257802915
},
"southwest" : {
"lat" : 52.5030534197085,
"lng" : 13.3324278197085
}
}
},
"partial_match" : true,
"place_id" : "EixLdXJmw7xyc3RlbmRhbW0gMSwgMTA3MTkgQmVybGluLCBEZXV0c2NobGFuZA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
As you can see the country and postalcode indexes are different (country: 6 and 5, postalcode 7 and 6)
When I want to show the street, housenumber, postalcode, city and country for both cities something like this:
echo 'Street = '.$array['results'][0]['address_components'][1]['long_name'].'<br />';
echo 'Housenumber = '.$array['results'][0]['address_components'][0]['long_name'].'<br />';
echo 'Postalcode = '.$array['results'][0]['address_components'][7]['long_name'].'<br />';
echo 'City = '.$array['results'][0]['address_components'][3]['long_name'].'<br />';
echo 'Country = '.$array['results'][0]['address_components'][6]['long_name'].'<br />';
It shows the following output:
Amsterdam:
Street = Dam
Housenumber = 1
Postalcode = 1012 JS
City = Amsterdam
Country = Netherlands
Berlin:
Street = Kurfürstendamm
Housenumber = 1
Notice: Undefined offset: 7 in .... on line ....
Postalcode =
City = Berlin
Country = 10719
So the question is, how can I get the "long_name" per "types" in the "address_components" array?
Or maybe there are even better ways?
You could try the following code below to convert the JSON reponse to an array with the address components as keys:
function convertAddress($json)
{
$data = json_decode($json);
$address = array();
foreach($data['results']['address_components'] AS $_component){
$key = array_values($_component['types']);
$address[$key[0]] = $_component;
}
return $address;
}
This works great for me with your test data, you can then access data like so:
$address = convertAddress($berlin_address);
print $address['post_code']['long_name'];
Please note I have only worked on the address_components part of the JSON, the rest should be easy to work into the method though.
I am not having any success getting values out of a JSON array object. I am specifically trying to get to geometry->location->lat
Here is what I am doing:
$url='http://maps.googleapis.com/maps/api/geocode/json?address='.$a.'&sensor=false';
$result = file_get_contents($url);
$jsonArr = json_decode($result,true);
echo "Lat: ".$jsonArr['results']['geometry']['location']['lat'];
Here is a print_r() of the JSON returned by Google:
{
"results" : [
{
"address_components" : [
{
"long_name" : "46814",
"short_name" : "46814",
"types" : [ "postal_code" ]
},
{
"long_name" : "Fort Wayne",
"short_name" : "Fort Wayne",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Indiana",
"short_name" : "IN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Fort Wayne, IN 46814, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 41.08472709999999,
"lng" : -85.26534090
},
"southwest" : {
"lat" : 41.00163510,
"lng" : -85.35540899999999
}
},
"location" : {
"lat" : 41.05472940,
"lng" : -85.30328109999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 41.08472709999999,
"lng" : -85.26534090
},
"southwest" : {
"lat" : 41.00163510,
"lng" : -85.35540899999999
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
The results are returned in an array. Get the first element and you're done. However you should make sure that you actually have at least one element in the results array.
echo "Lat: " . $jsonArr['results'][0]['geometry']['location']['lat'];