在PHP中使用Nominatim进行反向地理编码

我正在尝试在php中执行一些反向地址解析.不幸的是,我遇到一个错误.

$lon = 100.753;
$lat = 13.69362;

function getAddress($RG_Lat,$RG_Lon)
{
  $json = "http://nominatim.openstreetmap.org/reverse?format=json&lat=".$RG_Lat."&lon=".$RG_Lon."&zoom=27&addressdetails=1";
  $jsonfile = file_get_contents($json);
  $RG_array = json_decode($jsonfile,true);

  foreach ($RG_array as $name => $value)
  {
      if($name == "display_name")
      {
          $RG_address = $value;
          break;
      }
  }

  return $RG_address;
}

$addr = getAddress($lat,$lon);
echo "Address: ".$addr;

这是我遇到的错误.

<b>Warning</b>:  file_get_contents(http://nominatim.openstreetmap.org/reverse?format=json&amp;lat=13.69362&amp;lon=100.753&amp;zoom=27&amp;addressdetails=1): failed to open stream: Connection timed out in <b>/home/public_html/myapp/get_loc.php</b> on line <b>22</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/public_html/myapp/get_loc.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>:  Undefined variable: RG_address in <b>/home/public_html/myapp/get_loc.php</b> on line <b>34</b><br />
Address: <br />

如果我在Angular中使用以下代码,则可以正常工作.

showCountry($scope.latitude,$scope.longitude);
      function showCountry(lat, lon) {
              $.getJSON('//nominatim.openstreetmap.org/reverse?json_callback=?&format=json', {lat: lat, lon: lon}, function(data) {

                 console.log(data.address.town);
                 console.log('suburb- '+data.address.suburb);
                 console.log('county/district -'+data.address.county);
                 console.log('state - '+data.address.state);
                 console.log(data.address.postcode);
                  console.log(data.address.country);

             });
          }

我无法在客户端执行此操作;这就是我需要在php中执行此操作的原因.

解决方法:

您应该使用cURL(而不是file_get_contents())来请求带有用户代理标头的服务器,因为OSM仅接受带有valid user agent的请求:

$lon = 100.753;
$lat = 13.69362;

function getAddress($RG_Lat,$RG_Lon)
{
  $json = "https://nominatim.openstreetmap.org/reverse?format=json&lat=".$RG_Lat."&lon=".$RG_Lon."&zoom=27&addressdetails=1";

  $ch = curl_init($json);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0");
  $jsonfile = curl_exec($ch);
  curl_close($ch);

  $RG_array = json_decode($jsonfile,true);

  return $RG_array['display_name'];
  // $RG_array['address']['city'];
  // $RG_array['address']['country'];
}

$addr = getAddress($lat,$lon);
echo "Address: ".$addr;

输出:

Address: Short term parking, Sky Lane, สมุทรปราการ, จังหวัดสมุทรปราการ, 10520, ประเทศไทย

另外,正如@Kevin_Kinsey指出的那样,您也可以将file_get_contents()stream_context_create()结合使用:

$opts = [
  'http' => [
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) \r\n"
  ]
];
$context = stream_context_create($opts);
$jsonfile = file_get_contents($json, false, $context);
上一篇:javascript-获取当前道路限速


下一篇:java – JMapViewer,MouseListener调用了2次