Tom*_*Tom 0 php curl google-maps geocoding google-api
我目前正试图通过在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
Run Code Online (Sandbox Code Playgroud)
旧代码与过时的部分:
$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'];
}
Run Code Online (Sandbox Code Playgroud)
================================================== ==
编辑1 - 为每个网页添加HTML代码以使其工作
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
编辑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'];
}
Run Code Online (Sandbox Code Playgroud)
你有效$url.$url = htmlspecialchars($url);在调用CURL之前不要这样做.
更改:
// ...
$url = htmlspecialchars($url);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...
Run Code Online (Sandbox Code Playgroud)
到(或smth):
// ...
echo htmlspecialchars($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...
Run Code Online (Sandbox Code Playgroud)
并且您的地址生成无效.代替:
$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);
Run Code Online (Sandbox Code Playgroud)
做:
$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];
Run Code Online (Sandbox Code Playgroud)
这是有效的例子:
$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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7294 次 |
| 最近记录: |