带有curl的PHP Google Geocode拒绝工作

时间:2013-10-02 14:00:43

标签: php google-maps curl google-api geocoding

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

我目前有以下代码,但不知何故,将生成的地址复制到Google Chrome中时,它无法通过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'];
}

2 个答案:

答案 0 :(得分:8)

您的$url无效。在致电CURL之前不要$url = htmlspecialchars($url);

变化:

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

to(或smth):

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

答案 1 :(得分: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')