簡體   English   中英

simplexml_load_file警告錯誤

[英]simplexml_load_file warning error

我有一個簡單的函數,可以使用Google提供的簡單XML將地址字符串轉換為經度和緯度。 我偶爾會收到這個奇怪的警告錯誤:

警告:simplexml_load_file( http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true ):無法打開流:HTTP請求失敗! 第193行的/search_results.php中的HTTP / 1.0 500內部服務器錯誤

警告:simplexml_load_file():I / O警告:無法在第193個網址的/search_results.php文件中加載外部實體“ http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true ”未加載

有沒有更好的方法來執行此操作以避免此錯誤?

我的代碼如下:

$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;
  }

似乎您正在嘗試對空地址進行地址解析。 您應先查詢$ geo_address的長度,然后再查詢Google

$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()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM