Reputation: 655
I'm implementing Google Maps service with PHP/CURL and currently have a problem with Maps API. I am sending queries to Google Maps with English and Russian addresses. English address works fine, while Russian address fails with 602 (address not found)
Weird thing is that if I copy-paste curl query with Russian address to browser, it works fine (returns 200 and coords).
My code is
public static function google_geolocator($geoloc){
$geoloc = urlencode(implode(" ",$geoloc));
$query = "http://maps.google.com/maps/geo?q={$geoloc}&sensor=true&oe=utf8";
echo $query;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $query);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($curl);
curl_close($curl);
$data = json_decode($json, TRUE);
return $data;
}
I searched through Internets and figured out some hacks with User-Agent and &oe=utf8 and so on, but none of those seem to work. The main thing that confuses me, again, is that this method works perfectly with address in English, but fails with Russian. Although pasting query into browser works perfect in both cases.
Thank you in advance!
small update: query like
http://maps.google.com/maps/geo?q=%D0%A2%D0%B0%D0%B8%D0%BB%D0%B0%D0%BD%D0%B4+%D0%9A%D0%BE+%D0%9F%D1%85%D1%83%D0%BA%D0%B5%D1%82&sensor=true&oe=utf8
works in Chrome, but doesn't work in Firefox (same 602), while
http://maps.google.com/maps/geo?q=Thailand+Koh+Phuket&sensor=true&oe=utf8
works fine in both
upd2 var_dumping($data) returns
{
"name": "Таиланд Ко Пхукет",
"Status": {
"code": 602,
"request": "geocode"
}
}
and name field is absolutely the same as in Chrome response.
upd3 alright, the thing is if i change location name in Russian slightly, both Chrome and FF return 200 and coordinates. The problem seems to be that Chrome is bit more "intellectual" while communicating with Google. Refactored method so location names are always provided in English no matter what current locale is. Looks like it's better not to mess with Google and character sets different from English.
Upvotes: 2
Views: 3089
Reputation: 449415
The main thing that confuses me, again, is that this method works perfectly with address in English, but fails with Russian.
You are telling Google to expect UTF-8 data, but you're probably sending it something else. UTF-8 is a double-byte encoding - plain old ASCII characters consist of one byte, just as in ASCII; Russian and other characters of two or more. That's why the English alphabet often works, but as soon as other characters come into play, things break. Most likely, your Cyrillic characters are getting garbled (= stored as an encoding other than UTF-8) somewhere along the way, before they enter your query.
Make sure the values in $geoloc
are UTF-8 encoded. If they come from a file, make sure that is UTF-8 encoded. If they come from a database, make sure the tables and the database connection are UTF-8 encoded.
Upvotes: 4