Hugo H
Hugo H

Reputation: 6362

Google Maps geocoding working from browser but not from PHP server

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?

Upvotes: 6

Views: 2169

Answers (7)

ArturT
ArturT

Reputation: 90

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.

Upvotes: 1

N-A-S
N-A-S

Reputation: 1

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);

Upvotes: 0

miguev
miguev

Reputation: 4865

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

Upvotes: 5

Hugo H
Hugo H

Reputation: 6362

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

Upvotes: 0

yivi
yivi

Reputation: 47648

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

Upvotes: 3

Tristup
Tristup

Reputation: 3673

<?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

Upvotes: -1

Oleksandr  Maliuta
Oleksandr Maliuta

Reputation: 516

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

Upvotes: 0

Related Questions