【问题标题】:simplexml_load_file warning errorsimplexml_load_file 警告错误
【发布时间】:2015-03-26 09:07:45
【问题描述】:

我有一个简单的函数,它使用来自 Google 的简单 XML 将地址字符串转换为经度和纬度。我偶尔会收到这个奇怪的警告错误:

警告: simplexml_load_file(http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true): 无法打开流:HTTP 请求失败! HTTP/1.0 500 内部 第 193 行 /search_results.php 中的服务器错误

警告:simplexml_load_file():I/O 警告:无法加载外部 实体 “http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true” 在 /search_results.php 第 193 行 url 未加载

有没有更好的方法来避免这个错误?

我的代码如下:

$geo_address = urlencode($address);

$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$geo_address."&sensor=true";

  $xml =  simplexml_load_file($request_url) or die ("url not loading");
  $status = $xml->status;
  if ($status=="OK") {
    $lat = $xml->result->geometry->location->lat;
    $lon = $xml->result->geometry->location->lng;
  }

【问题讨论】:

  • 看来你的内部php文件(search_results.php)有问题HTTP/1.0 500 Internal Server Error,你可以试试this out

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


【解决方案1】:

您似乎在尝试对空地址进行地理编码。您应该在查询 google 之前验证 $geo_address 长度

$geo_address = urlencode($address);

if(strlen($geo_address)>0) {

    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$geo_address."&sensor=true";

    $xml =  simplexml_load_file($request_url) or die ("url not loading");
    $status = $xml->status;
    if ($status=="OK") {
      $lat = $xml->result->geometry->location->lat;
      $lon = $xml->result->geometry->location->lng;
    }
} else {
  die('Invalid address');
}

您应该将 die() 替换为适合您的应用的其他错误管理逻辑。

【讨论】:

  • 我认为解决了它。谢谢! @amenadiel
猜你喜欢
  • 2018-02-11
  • 2011-05-16
  • 2017-11-10
  • 2016-09-02
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多