我正在做项目,我需要填写我的数据库与地址的后期液化天然气。我决定使用PHP和地理编码,我有很大的问题。PHP脚本返回零结果,但当我尝试使用web浏览器时,我有正确的结果。
$fulladdress = $street.$city;
$request= "http://maps.googleapis.com/maps/api/geocode/xml?address=".rawurlencode($fulladdress)."&sensor=true";
$xml = simplexml_load_file($request) or die("url not loading");
当我打印$fulladdress, $request和xml状态时,我有这个:
?apino Kartuskie http://maps.googleapis.com/maps/api/geocode/xml?address=%3Fapino%20Kartuskie&sensor=true STATUS: ZERO_RESULTS
您需要翻译波兰字符,您可以使用http://php.net/manual/en/function.iconv.php
$fulladdress = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $street.$city);
$request= "http://maps.googleapis.com/maps/api/geocode/xml?address=".rawurlencode($fulladdress)."&sensor=true";
$xml = simplexml_load_file($request) or die("url not loading");
事实证明,如果来自google maps api的查询/响应有一些变音符号,则有必要包括http标头"Accept-Language "。在我的例子中:
行不通:
curl -s 'http://maps.googleapis.com/maps/api/geocode/json?address=ul.+Zwyciestwa+44%2C+Dobre+Miasto'
做的工作:
curl -s
'http://maps.googleapis.com/maps/api/geocode/json?address=ul.+Zwyciestwa+44%2C+Dobre+Miasto'
-H 'Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4'
您需要使用UTF-8进行编码。
http://maps.googleapis.com/maps/api/geocode/xml?address=%C5%81apino%20Kartuskie&sensor=true