5
votes

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?

7
are you sure it works? I've got zero results. - yivi
In your browser? It's working for me right now. - Hugo H
zero results for the request. just clicking on your link on the browser or curl. - yivi

7 Answers

5
votes

(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.

3
votes

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"
}
1
votes

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.

0
votes

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');
0
votes

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');
0
votes

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);
-1
votes
<?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