【问题标题】:simplexml_load_file parser error when getting response from google maps api从 google maps api 获取响应时 simplexml_load_file 解析器错误
【发布时间】:2013-08-17 04:37:30
【问题描述】:

这些 bug 困扰了我一天,我什至不知道如何修复它。我有一个网站运行了一年多,没有任何问题。基本上,当我通过表单向数据库添加新记录时,它使用来自谷歌地图 api 的地理编码来获取 xml 纬度和经度响应,然后将记录插入到数据库中。但是当我昨天尝试这样做时,它返回了错误

"警告:simplexml_load_file() [function.simplexml-load-file]:http://maps.googleapis.com/maps/api/geocode/xml?Key="MY-API-KEY"&address=411+Church+Street++%2C+Parramatta+NSW+2150%2C+Australia&sensor=false :1: 解析器错误:/MY-SITE-DIR/geoCode-1.php 第 40 行的文档为空。

当我将 url 直接放入浏览器时,它会返回一个完美的 xml,这意味着我的 url 实际上是好的。我研究了一整天,找到了一些解决方案,以下最符合我的问题:

This One 是添加用户代理。但在我的情况下它不起作用。

有人有什么建议吗?以下是我的相关代码:

$xml = simplexml_load_file($request_url) or die("url not loading");

$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
  // Successful geocode
  $geocode_pending = false;
  $coordinates = $xml->Response->Placemark->Point->coordinates;
  $coordinatesSplit = split(",", $coordinates);
  // Format: Longitude, Latitude, Altitude
  $lat = $coordinatesSplit[1];
  $lng = $coordinatesSplit[0];

  $query = sprintf("UPDATE orthodontist " .
         " SET lat = '%s', lng = '%s' " .
         " WHERE id = '%s' LIMIT 1;",
         mysql_real_escape_string($lat),
         mysql_real_escape_string($lng),
         mysql_real_escape_string($id));
  $update_result = mysql_query($query);
  if (!$update_result) {
    die("Invalid query: " . mysql_error());
  }
} 

【问题讨论】:

    标签: php xml google-maps-api-3 google-api


    【解决方案1】:

    API V2 几天前已停止工作。我可以通过您的代码看到,您使用的是 API V2。

    您需要使用 API v3 这是与 v3 一起使用的 PHP 代码示例

    www.fcsoftware.co.uk/blog/?p=66

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2016-08-02
      相关资源
      最近更新 更多