I want to fetch Postcode [ "long_name" : "S35 1TZ",] from this google api array
Iam usin this API
http://maps.googleapis.com/maps/api/geocode/json?address=(53.4624118,%20-1.4922259999999596)
Using
msg.results[0].address_components[6].long_name;
Not working
{
"results" : [
{
"address_components" : [
{
"long_name" : "7A",
"short_name" : "7A",
"types" : [ "street_number" ]
},
{
"long_name" : "Hollow Gate",
"short_name" : "Hollow Gate",
"types" : [ "route" ]
},
{
"long_name" : "Chapeltown",
"short_name" : "Chapeltown",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "postal_town" ]
},
{
"long_name" : "South Yorkshire",
"short_name" : "South York",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "S35 1TZ",
"short_name" : "S35 1TZ",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "7A Hollow Gate, Sheffield, South Yorkshire S35 1TZ, UK",
"geometry" : {
"location" : {
"lat" : 53.4624118,
"lng" : -1.492226
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 53.4637607802915,
"lng" : -1.490877019708498
},
"southwest" : {
"lat" : 53.4610628197085,
"lng" : -1.493574980291502
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
There are two ways. Based on your code, I am not sure if you got a string of that data or already an object. If you only got data back, it is a string and you first have to convert the JSON into PHP array or object.
1 - You could convert the JSON to an array:
$msg = json_decode($msg,true);
/* And call it by */
echo($msg['results'][0]['address_components'][7]['long_name']);
OR
2 - Using the object notation:
$msg = json_decode($msg);
/* And call it by */
echo($msg->results[0]->address_components[7]->long_name);
If you got the data back using a Google API PHP libary, it's probably not a string but an object so you can dismiss the json_decode() and use option 2. If you want to prefer using option 1, you can cast the object into an array by using $msg = (array)$msg;
In general using print_r($msg) or var_dump($msg) can be handy to debug the thing, just add your path each time you get closer to your goal. So in this case var_dump($msg) then var_dump($msg->result), then var_dump($msg->result[0]), and so on.
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 am looking for a way to find the PlaceID from a Google web request. I have several hundred addresses (i.e. coordinates) and I want to find a way to all enter them in a phpscript.
I am struggling with the API request for the place API. I have the following request:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=50.096332,8.673130&radius=1&key=APIKEY
This returns the entire city of Frankfurt, but that's not what I am after. If I enter the address Oskar-Sommer-Strasse 7, Frankfurt am Main, Deutschland into the PlaceID finder on the Google Maps documentation site, I get the following result for the ID ChIJ-b9GgAUMvUcRYuYKJtGGlw4.
I am now looking for a way to get this done. I've collected the coordinates using Google Maps API as well, so they should match (or at least that's what I thought).
Maybe I need to use some other request type?
Using the geocode api and sending a request to
https://maps.googleapis.com/maps/api/geocode/json?address=Oskar-Sommer-Strasse+7+%20Frankfurt%20am%20Main%20&component=Deutschland
returns (I'm assuming you want the place id field)
{
"results" : [
{
"address_components" : [
{
"long_name" : "7",
"short_name" : "7",
"types" : [ "street_number" ]
},
{
"long_name" : "Oskar-Sommer-Straße",
"short_name" : "Oskar-Sommer-Straße",
"types" : [ "route" ]
},
{
"long_name" : "Frankfurt am Main Süd",
"short_name" : "Frankfurt am Main Süd",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Frankfurt am Main",
"short_name" : "Frankfurt am Main",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Darmstadt",
"short_name" : "DA",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Hessen",
"short_name" : "HE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Germany",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "60596",
"short_name" : "60596",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Oskar-Sommer-Straße 7, 60596 Frankfurt am Main, Germany",
"geometry" : {
"location" : {
"lat" : 50.09665,
"lng" : 8.67299
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.0979989802915,
"lng" : 8.674338980291502
},
"southwest" : {
"lat" : 50.0953010197085,
"lng" : 8.671641019708497
}
}
},
"place_id" : "ChIJ-b9GgAUMvUcRYuYKJtGGlw4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
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'm currently using Google Maps API to get a longitude and latitude form the user's street, street number and zip code.
I would also like to grab their city and state from the Maps API results. The XML document formats it strangely though, and I'm not sure how I'm supposed to reference it (it's different from the way I'm grabbing the long and lat).
This is how I am loading the Maps API:
$map_address = str_replace($address."+".$zip);
$map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$map_address."&key=myKey";
$resp_json = file_get_contents($map_api_url);
$resp = json_decode($resp_json, true);
I then get the Long and Lat:
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
But how do I grab the city and state?
{
"results" : [
{
"address_components" : [
{
"long_name" : "3840",
"short_name" : "3840",
"types" : [ "street_number" ]
},
{
"long_name" : "Fake Street",
"short_name" : "Fake St",
"types" : [ "route" ]
},
{
"long_name" : "Wilson Heights",
"short_name" : "Wilson Heights",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "North York",
"short_name" : "North York",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Toronto",
"short_name" : "Toronto",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Toronto Division",
"short_name" : "Toronto Division",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ontario",
"short_name" : "ON",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "M6H",
"short_name" : "M6H",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
"formatted_address" : "3840 Fake Street, Toronto, ON M6H, Canada",
"geometry" : {
"location" : {
"lat" : 43.5399244,
"lng" : -78.43486559999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 43.5412733802915,
"lng" : -78.43351661970848
},
"southwest" : {
"lat" : 43.7385754197085,
"lng" : -79.43621458029151
}
}
},
"place_id" : "ChIJPVDxjGgyK4gRt3O7zOSmIF4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Just answered my own question by running the code through a loop. Please let me know if you have a better way!
foreach($resp['results'][0]['address_components'] as $address_componenets) {
if($address_componenets["types"][0]=="country") {
$country = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="administrative_area_level_1") {
$state = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="locality") {
$city = $address_componenets["long_name"];
}
}
I'm playing with Google Maps and Geocoding (converting Address to lang/lat coordinates). Everything works fine so far and I'm getting an answer from the google maps server with a file:
$address = urlencode( $address );
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false";
$content = file_get_contents( $url );
echo '<pre>';
print_r( $content );
echo '</pre>';
Point is that I don't need all that information.
Questions
A How do I get only a part of the JSON object?
(Example: I only need 'results'->'address_components'->'type' = 'country';.)
B Can I reduce the content of the returned (google) file somehow as I don't need everything?
Update
I need to add part of the data to a json object. So decoding & then encoding seems like a loss of energy & time (at this point). Any ideas if I'd be better (faster) off with requesting a XML object? (The final set of objects is pretty large. This is why I should really care about performance.)
Example JSON answer from Google Maps maps server (file content):
{
"results" : [
{
"address_components" : [
{
"long_name" : "28",
"short_name" : "28",
"types" : [ "street_number" ]
},
{
"long_name" : "Sellhorner Weg",
"short_name" : "Sellhorner Weg",
"types" : [ "route" ]
},
{
"long_name" : "Behringen",
"short_name" : "Behringen",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Bispingen",
"short_name" : "Bispingen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Soltau-Fallingbostel",
"short_name" : "Soltau-Fallingbostel",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Niedersachsen",
"short_name" : "NDS",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "29646",
"short_name" : "29646",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Sellhorner Weg 28, 29646 Bispingen, Deutschland",
"geometry" : {
"location" : {
"lat" : 53.118080,
"lng" : 9.966859999999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 53.11942898029150,
"lng" : 9.96820898029150
},
"southwest" : {
"lat" : 53.11673101970850,
"lng" : 9.965511019708496
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
$array = json_decode($content, true);
$countries = array_filter($array['results']['address_components'], function ($a) {
return in_array('country', $a['types']);
});
var_dump($countries);
Gives you all address_components which have a type 'country'. Probably that's only going to be one, but it may be more than one. This syntax requires PHP 5.3+ BTW.
Can I reduce the content of the returned (google) file somehow as I don't need everything?
Just take the parts of the decoded $array you need and forget the rest. If you want, json_encode it again.