Why not parse the json?

Have an address, you need to know the coordinates. Not working:
$content = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=Москва,%20ул.%20Академика%20Бочвара,%202");
$json = json_decode($content, true);
echo $json["results"]["geometry"]["location"]["lat"];
June 5th 19 at 21:09
2 answers
June 5th 19 at 21:11
Solution
because :
echo $json[0]["results"]["geometry"]["location"]["lat"];


and , of course, you need to check whether the response data required, and even for them to handle if they are )
Not working.

I think the coordinates will always be. - beau.Bahring commented on June 5th 19 at 21:14
well, here's how it works

echo $json["results"][0]["geometry"]["location"]["lat"];


I told You what the error is pointed out. Get involved!)))) - Camryn.Schimmel commented on June 5th 19 at 21:17
still not working :-( - beau.Bahring commented on June 5th 19 at 21:20
Well let's see what you have comes from the server! ))
Perhaps you have ended attempts to use their API without registration.

{
 "error_message" : "You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_",
 "results" : [],
 "status" : "OVER_QUERY_LIMIT"
}


and I had that such a result is the piece, you need data and where the RESULT is something that appears this :
echo $json["results"][0]["geometry"]["location"]["lat"];

5afea9741c6af014852409.jpeg - Camryn.Schimmel commented on June 5th 19 at 21:23
normally give, this ideone shows. - beau.Bahring commented on June 5th 19 at 21:26
run the code in the correct local environment or server.
Usually in the "sandbox" f-tsiya file_get_contents NOT WORKING.
I ran the result on the picture.
All works if to specify the correct path ["results"][0]["geometry"]["location"]["lat"]; and run the script in the normal execution environment. - Camryn.Schimmel commented on June 5th 19 at 21:29
thanks , not enough of a key in the link.

How it is received. I went to the control panel of Google, created a project, entered it, turned on the Geocoding API, I went to the page this connected API → Accounts → created the API key → changed in the code link:
https://maps.googleapis.com/maps/api/geocode/json?key=QWERTY1234&address=Москва,%20ул.%20Академика%20Бочвара,%202
- beau.Bahring commented on June 5th 19 at 21:32
I'm happy for you) now run the script in a sandbox .

$content = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=Москва,%20ул.%20Академика%20Бочвара,%202");

$json = json_decode($content, true);
var_dump($json); // here is the whole set of data

echo $json["results"][0]["geometry"]["location"]["lat"];


and you will be happy ) - Camryn.Schimmel commented on June 5th 19 at 21:35
June 5th 19 at 21:13
Solution
In kindergarten: "mom, I pooped".
Well the logs look, take what you string arrived (horrible variable name - at least a result or response).
I'm way OVER_QUERY_LIMIT returns empty results, your script is such a result does not provide.
Exhaust $json:
array(2) { ["results"]=> array(1) { [0]=> array(5) { ["address_components"]=> array(8) { [0]=> array(3) { ["long_name"]=> string(53) "Construction College №46, GOU" ["short_name"]=> string(53) "Construction College №46, GOU" ["types"]=> array(1) { [0]=> string(7) "premise" } } [1]=> array(3) { ["long_name"]=> string(1) "2" ["short_name"]=> string(1) "2" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [2]=> array(3) { ["long_name"]=> string(25) "Ulitsa Akademika Bochvara" ["short_name"]=> string(25) "Ulitsa Akademika Bochvara" ["types"]=> array(1) { [0]=> string(5) "route" } } [3]=> array(3) { ["long_name"]=> string(38) "Severo-Zapadnyy administrativnyy okrug" ["short_name"]=> string(38) "Severo-Zapadnyy administrativnyy okrug" ["types"]=> array(3) { [0]=> string(9) "political" [1]=> string(11) "sublocality" [2]=> string(19) "sublocality_level_1" } } [4]=> array(3) { ["long_name"]=> string(6) "Moskva" ["short_name"]=> string(6) "Moskva" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [5]=> array(3) { ["long_name"]=> string(6) "Moskva" ["short_name"]=> string(6) "Moskva" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [6]=> array(3) { ["long_name"]=> string(6) "Russia" ["short_name"]=> string(2) "EN" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> array(3) { ["long_name"]=> string(6) "123182" ["short_name"]=> string(6) "123182" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["formatted_address"]=> string(107) "Construction College No. 46, go, Ulitsa Akademika Bochvara, 2, Moskva, Russia, 123182" ["geometry"]=> array(4) { ["bounds"]=> array(2) { ["northeast"]=> array(2) { ["lat"]=> float(55.8080289) ["lng"]=> float(37.4615495) } ["southwest"]=> array(2) { ["lat"]=> float(55.8072015) ["lng"]=> float(37.4604274) } } ["location"]=> array(2) { ["lat"]=> float(55.8078912) ["lng"]=> float(37.4611445) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> array(2) { ["northeast"]=> array(2) { ["lat"]=> float(55.808964180291) ["lng"]=> float(37.462337430291) } ["southwest"]=> array(2) { ["lat"]=> float(55.806266219708) ["lng"]=> float(37.459639469709) } } } ["place_id"]=> string(27) "ChIJZ08edGdItUYR5ZIU8dqfnME" ["types"]=> array(1) { [0]=> string(7) "premise" } } } ["status"]=> string(2) "OK" }
- beau.Bahring commented on June 5th 19 at 21:16
and can be in the form of json, not the result of the conversion? I think you have the trite path in the hierarchy wrong. Well, again - logs! "doesn't work" usually throws some kind of error - Camryn.Schimmel commented on June 5th 19 at 21:19
, json.

How to see logs?

Now there is nothing displayed. And there is written some kind of mistake. - beau.Bahring commented on June 5th 19 at 21:22
I have your link blank results and OVER_QUERY_LIMIT what I wrote above - Camryn.Schimmel commented on June 5th 19 at 21:25
what to do?) - beau.Bahring commented on June 5th 19 at 21:28
please send here what you have in $srtrings - Camryn.Schimmel commented on June 5th 19 at 21:31

string(271) "{ "error_message" : "You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_", "results" : [], "status" : "OVER_QUERY_LIMIT" } "

Understand that this kind of swearing, but in $json everything right: displays the converted json. - beau.Bahring commented on June 5th 19 at 21:34
well already in json you will need, hehe. - Camryn.Schimmel commented on June 5th 19 at 21:37
how to fix? - beau.Bahring commented on June 5th 19 at 21:40
how to fix the fact that you give the server? Read manual to API. - nicklaus commented on June 5th 19 at 21:43
well, thank you. Not enough of a key in the link. How it got written here in the comments . - beau.Bahring commented on June 5th 19 at 21:46

Find more questions by tags JSONPHP