【问题标题】:PHP Google Geocode with curl refusing to work带有curl的PHP Google Geocode拒绝工作
【发布时间】:2013-10-02 14:00:43
【问题描述】:

我目前正在尝试通过在 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'];
}

【问题讨论】:

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


    【解决方案1】:

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

    变化:

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

    【讨论】:

    • 删除 htmlspecialchars 使情况变得更糟。现在错误消息是 { "results" : [], "status" : "INVALID_REQUEST" }
    • 你能显示变量$url的输出吗? p.s.我刚刚测试了这个脚本,它对我有用。
    • 返回带有测试数据的 url(没有 htmlspecialchars):maps.googleapis.com/maps/api/geocode/… 错误消息是:Array ( [results] => Array ( ) [status] => INVALID_REQUEST ) 发生错误:1带有 htmlspecialchars 的网址:maps.googleapis.com/maps/api/geocode/…
    • 您的地址无效。 Try this。删除将空格替换为+str_replace() 和将帖子数据连接成字符串的+;这一切都是由urlecnode() 函数完成的。
    • 输出 url 现在是:maps.googleapis.com/maps/api/geocode/… 并且错误信息仍然是:Array ( [results] => Array ( ) [status] => INVALID_REQUEST ) 发生错误:1
    【解决方案2】:

    在 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-06-25
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多