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

3

我正在尝试在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中执行它的原因。


1
在php.ini中,allow_url_fopen是否设置为ON? - Kevin_Kinsey
我检查了phpinfo,allow_url_fopen已开启。 - Roxx
你检查过响应了吗?HTTP 403,“禁止访问”。有人建议使用cURL,或者你需要额外的参数...检查他们的服务条款等。 - Kevin_Kinsey
1个回答

8

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

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

输出:

地址:短期停车场,天空巷,สมุทรปราการ,จังหวัดสมุทรปราการ,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);

成功了。我能看到数据。非常感谢。如果有任何问题,我会提出新的问题。 - Roxx
2
不客气,@Ironic :) 这是一个有趣的问题。 - Syscall
3
值得一提的是,如果由于某种原因无法使用cURL,可以使用stream_context_create()为file_get_contents()设置UA字符串。 - Kevin_Kinsey
我在进行操作时遇到了错误。<b>注意</b>:数组转换为字符串。 - Roxx
1
如果我有再次投票的机会,那么我肯定会这样做。它起作用了。 - Roxx
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接