Google Maps geocoding working from browser but not from PHP server
Asked Answered
C

7

6

When I fetch Google Maps Geocoding API from my browser, it's working, but not from my PHP script.

Working : https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

Returns :

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "La Pérelle",
               "short_name" : "La Pérelle",
               "types" : [ "colloquial_area", "political" ]
            },
         ],
         "formatted_address" : "La Pérelle, 38380, France",
         // other stuffs
         "partial_match" : true,
         "place_id" : "ChIJZzDRRAX4ikcRnZ-vb1tnTgY",
         "types" : [ "colloquial_area", "political" ]
      }
   ],
   "status" : "OK"
}

Not working :

<?php 
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France';
echo file_get_contents($url);

Returns:

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}

Any ideas?

Update : some people are not seeing any results from their browser too. Could Google use my position to show decent results?

Condillac answered 17/1, 2017 at 9:22 Comment(13)
are you sure it works? I've got zero results.Depot
In your browser? It's working for me right now.Condillac
zero results for the request. just clicking on your link on the browser or curl.Depot
imgur.com/a/cBDyjDepot
s27.postimg.org/74qb232yb/Capture.png :(Condillac
I have zero tooDurrett
Could Google use my position to help with results?Condillac
Might be related to region biasing. If I add the parameter "region=fr", it will return the correct address. Without that parameter might be biasing according to the client's location.Depot
Hi, Try with La Perelle instead of La Pérelle. It has just worked for me however I am unsure whether it is the location you are expecting.Coloratura
Hi @MichaelThompson, thanks but it's not the good place.Condillac
It could be your daily limit to access the API is over, which refreshes every day. either try without API key once in your code or try other key.Bouldin
When you are pasting the url in address bar the content will be formatted by the browser itself. But when you are using PHP or any other language to send a request you need a pre formatted request url, which I suggested.Conferral
@HugoH, I really think is about region biasing. Google is biasing is for some reason giving more wight to an explicit region biasing than to the country in the address parameter.Depot
D
3

I think is related to region region biasing.

Try adding the region parameter (although you might need to pre-calculate it when building your request, if you have requests for many different regions).

E.g., for me, from Madrid, Spain, this request brings zero results:

https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

But, if I add the region=fr parameter

https://maps.google.com/maps/api/geocode/json?sensor=false&region=fr&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

I get:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Chartreuse Mountains",
               "short_name" : "Chartreuse Mountains",
               "types" : [ "establishment", "natural_feature" ]
            },
            {
               "long_name" : "Saint-Pierre-d'Entremont",
               "short_name" : "Saint-Pierre-d'Entremont",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Isere",
               "short_name" : "Isere",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Auvergne-Rhône-Alpes",
               "short_name" : "Auvergne-Rhône-Alpes",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "France",
               "short_name" : "FR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "73670",
               "short_name" : "73670",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Chartreuse Mountains, 73670 Saint-Pierre-d'Entremont, France",
         "geometry" : {
            "location" : {
               "lat" : 45.3773525,
               "lng" : 5.827970700000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.3787014802915,
                  "lng" : 5.829319680291503
               },
               "southwest" : {
                  "lat" : 45.3760035197085,
                  "lng" : 5.826621719708499
               }
            }
         },
         "place_id" : "ChIJSXJWFav4ikcREQZmf4QdYMc",
         "types" : [ "establishment", "natural_feature" ]
      }
   ],
   "status" : "OK"
}
Depot answered 17/1, 2017 at 10:5 Comment(4)
Thanks for your complete answer. Unfortunately, this is not the result I expected... (not the same locality). I'm gonna look for place_idCondillac
Still, I think it is related to region biasing + incomplete search input. You've got the results you expect because your location helps google "guess" the correct location. Isn't it possible for you to pass the zip code as well? It was the most helpful hint for me when geocoding addresses.Depot
@HugoH, also, I see that "Saint-Pierre-de-Chartreuse" is part of your request but not part of your response. Again, so Google is mostly guessing it right because of your location.Depot
It was in the result before I remove some of it for clarity. ;)Condillac
M
5

(Absence of) language preference is most commonly behind this issue.

When you send the API request from your browser:

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

it may happen that your browser language preferences is set to prefer French as the first language, which makes your browser include send the Accept-Language header along with all requests.

Google Geocoding API will check for the language GET parameter, and in absence of that it will check for the Accept-Language header as a backup. If your browser is set to prefer content in French, that should make the above request equivalent to

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France&language=fr

For me, because my browser is set to prefer English, the first request returns "La Perelle, 73130 Saint-François-Longchamp, France" while the second returns "La Pérelle, 38380 Saint-Pierre-de-Chartreuse, France".

Since the introduction of the new geocoder in late 2016, the region parameter is no longer the only one that influences results, the language parameter does influence results now:

The preferred language has a small influence on the set of results that the API chooses to return, and the order in which they are returned. The geocoder interprets abbreviations differently depending on language, such as the abbreviations for street types, or synonyms that may be valid in one language but not in another. For example, utca and tér are synonyms for street in Hungarian.

That said, if "La Perelle, 73130 Saint-François-Longchamp, France" is just a bad results for the above query (La Pérelle, Saint-Pierre-de-Chartreuse, France) please consider filing a bug report.

Merchant answered 4/9, 2017 at 9:6 Comment(0)
D
3

I think is related to region region biasing.

Try adding the region parameter (although you might need to pre-calculate it when building your request, if you have requests for many different regions).

E.g., for me, from Madrid, Spain, this request brings zero results:

https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

But, if I add the region=fr parameter

https://maps.google.com/maps/api/geocode/json?sensor=false&region=fr&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

I get:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Chartreuse Mountains",
               "short_name" : "Chartreuse Mountains",
               "types" : [ "establishment", "natural_feature" ]
            },
            {
               "long_name" : "Saint-Pierre-d'Entremont",
               "short_name" : "Saint-Pierre-d'Entremont",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Isere",
               "short_name" : "Isere",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Auvergne-Rhône-Alpes",
               "short_name" : "Auvergne-Rhône-Alpes",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "France",
               "short_name" : "FR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "73670",
               "short_name" : "73670",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Chartreuse Mountains, 73670 Saint-Pierre-d'Entremont, France",
         "geometry" : {
            "location" : {
               "lat" : 45.3773525,
               "lng" : 5.827970700000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.3787014802915,
                  "lng" : 5.829319680291503
               },
               "southwest" : {
                  "lat" : 45.3760035197085,
                  "lng" : 5.826621719708499
               }
            }
         },
         "place_id" : "ChIJSXJWFav4ikcREQZmf4QdYMc",
         "types" : [ "establishment", "natural_feature" ]
      }
   ],
   "status" : "OK"
}
Depot answered 17/1, 2017 at 10:5 Comment(4)
Thanks for your complete answer. Unfortunately, this is not the result I expected... (not the same locality). I'm gonna look for place_idCondillac
Still, I think it is related to region biasing + incomplete search input. You've got the results you expect because your location helps google "guess" the correct location. Isn't it possible for you to pass the zip code as well? It was the most helpful hint for me when geocoding addresses.Depot
@HugoH, also, I see that "Saint-Pierre-de-Chartreuse" is part of your request but not part of your response. Again, so Google is mostly guessing it right because of your location.Depot
It was in the result before I remove some of it for clarity. ;)Condillac
A
1

I feel I need to comment as I've spent some time on this - I had the same problem, what solved it is adding the "location" and "radius" parameters - I'm guessing Google guesses these from the browser headers, but can't do so via a naked request. Too bad their API doesn't specify these parameters are mandatory in their documentation, which they seem to be for direct requests.

Anagnos answered 23/8, 2020 at 2:18 Comment(0)
D
0

It's working

$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address='.urlencode('La Pérelle, 38380, France');
Durrett answered 17/1, 2017 at 9:28 Comment(0)
C
0

After yivi answer, based on region biasing, I have to find another way to link front & back-end places and I did it using place_id.

This was not possible before as Google was not returning place_id from it's autocomplete, but now it works, so I do:

echo file_get_contents('https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyA3U9jkQcF-Vplh6bpJNcVOWMSNImCPn04&place_id=ChIJZzDRRAX4ikcRnZ-vb1tnTgY');
Condillac answered 17/1, 2017 at 11:4 Comment(0)
H
0

This will work correctly.

$address = "La Pérelle, 38380, France";
$prepAddr = str_replace(' ','+',$address);
$url = 'https://maps.google.com/maps/api/geocode/json?key=YOUR_KEY&address='.$prepAddr.'&sensor=false';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
curl_close($ch);
$output = json_decode($contents);
Hynes answered 14/6, 2019 at 10:29 Comment(0)
C
-1
<?php 
$address="La Pérelle, 38380, France";
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address='.urlencode($address);
echo file_get_contents($url);

Hope this will work for you. Your address was not formatted to request format.

Thanks and Regards

Conferral answered 17/1, 2017 at 9:40 Comment(4)
Yeah but no. The address is already formatted: La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20FranceCondillac
I have just tested this code at my end its working here. It seems that problem lies with some other place.Conferral
I think that your answer repeats the code for the question. There is no way it could resolve the issue, imo.Depot
Yivi, Thanks for telling me that, but as you can see my comment and other comments are in between a fraction I was not in repeating his answer... Hope understandable...Conferral

© 2022 - 2024 — McMap. All rights reserved.