0

我目前正在尝试通过在 PHP 页面中使用 Google Geocode API 来获取地址的 lat 和 lng 参数。

我目前有以下代码,但在将生成的地址复制到谷歌浏览器时,它似乎无法通过 php 工作。

任何人都可以看到下面代码中的错误吗?

提前致谢!

汤姆

==================================================== ==

返回的错误是:

( [error_message] => The 'sensor' parameter specified in the request must be set to either 'true' or 'false'. [results] => Array ( ) [status] => REQUEST_DENIED ) An error has occured: 1

带有过时部分的旧代码:

$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);

// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$url = htmlspecialchars($url);

echo $url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
  echo 'An error has occured: ' . print_r($response);
} else {
    $geometry = $response['results'][0]['geometry'];
    $longitude = $geometry['location']['lat'];
    $latitude = $geometry['location']['lng'];
}

==================================================== ==

编辑 1 - 为每个网页添加了 HTML 代码以使其正常工作

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

编辑 1 - 固定和工作代码:

// create address string
$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];

// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
  echo 'An error has occured: ' . print_r($response);
} else {
    $geometry = $response['results'][0]['geometry'];
    $longitude = $geometry['location']['lat'];
    $latitude = $geometry['location']['lng'];
}
4

2 回答 2

9

你有无效$url的。$url = htmlspecialchars($url);在调用 CURL 之前不要这样做。

改变:

// ...
$url = htmlspecialchars($url);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...

到(或某事):

// ...
echo htmlspecialchars($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...

编辑1:

并且您的地址生成无效。代替:

$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);

做:

$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];

编辑2:

这是一个有效的例子:

$googleQuery = 'Sint Elooisstraat 30, 8800 Roeselare, België';
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
print_r($response);
于 2013-10-02T14:13:20.190 回答
0

在 CLI 脚本中遇到过这个问题。

我必须确保我的数据(从 CSV 文件导入)是 UTF-8 编码的:

$address = utf8_encode($address);
$address = rawurlencode($address);  

此外,我必须从脚本中传递一个字符串,我必须先将其转换为 ASCII,然后再将其转换为 UTF-8(或 htmlentities,这也可以):

mb_convert_encoding('Österreich', 'US-ASCII', 'UTF-8')

或者

htmlentities('Österreich', ENT_COMPAT, 'UTF-8')
于 2015-11-07T23:49:51.570 回答