drmcm84800 2018-05-04 11:11
浏览 254
已采纳

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

I am trying to perform some reverse geocoding in php. Unfortunately, I am getting an error.

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

Here are the errors which I am getting.

<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 />

If I use below code in Angular then it is working fine.

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

             });
          }

I can't perform this action on the client side; that's the reason I need to do it in php.

展开全部

  • 写回答

1条回答 默认 最新

  • dqmgjp5930 2018-05-04 11:45
    关注

    You should use cURL (instead of file_get_contents()) to request the server with a user agent header, because OSM accepts only request with a 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;
    

    Output:

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


    Also, as @Kevin_Kinsey pointed out, you can also use file_get_contents() with stream_context_create():

    $opts = [
      'http' => [
        'method'=>"GET",
        'header'=>"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) 
    "
      ]
    ];
    $context = stream_context_create($opts);
    $jsonfile = file_get_contents($json, false, $context);
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部