2012-03-22 78 views
1

我不知道如何在php中做到这一点。我试图从openstreetmap.org获取geocode数据,但我需要在php中执行此操作。在php中获取url的查询

在Javascript中(我有这方面的工作),它是:

<script type="text/javascript" 
    src="http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671"> 
</script> 

我有一个网址,但我不知道如何在PHP执行。

我曾尝试:

<?php 
    $url = "http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671"; 
    $response = file_get_contents($url); 
?> 

但是,这是行不通的。我用卷曲试过。任何想法或是这种类型的查询不可能在PHP?

+0

它是否会产生任何错误?在大多数服务器上,它们都在'/ var/log/httpd/error_log' – JKirchartz 2012-03-22 18:03:37

+0

第一个版本包含一个来自OpenStreetMap的javascript,但是你想用第二个版本实现什么?我认为你需要比“获取地理编码数据”更具体一些。 – 2012-03-22 18:04:06

+0

那个网址对我来说都是'无法地理编码' – 2012-03-22 18:11:03

回答

2

使用此;

$xml = simplexml_load_file("http://nominatim.openstreetmap.org/reverse?format=xml&lat=43.642018&lon=-79.374671"); 
print_r($xml); 

这将返回一个值的数组,见下文;

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [timestamp] => Thu, 22 Mar 12 18:31:11 +0000 
      [attribution] => Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0. 
      [querystring] => format=xml&lat=43.642018&lon=-79.374671 
     ) 

[result] => 1, Yonge Street, Corktown, Toronto, Ontario, M5B2H1, Canada 
[addressparts] => SimpleXMLElement Object 
    (
     [house_number] => 1 
     [road] => Yonge Street 
     [suburb] => Corktown 
     [city] => Toronto 
     [county] => Toronto 
     [state] => Ontario 
     [postcode] => M5B2H1 
     [country] => Canada 
     [country_code] => ca 
    ) 

) 

然后操纵你认为合适的数据。

+0

这是解决方案。谢谢! – Raymond 2012-03-22 21:34:53

1

它返回

<?xml version="1.0" encoding="UTF-8" ?> 
<reversegeocode timestamp='Thu, 22 Mar 12 18:07:13 +0000' attribution='Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.' querystring='format=xml&amp;lat=100&amp;Lon=100'> 
<error>Unable to geocode</error></reversegeocode> 

这仅仅意味着没有地址为特定的坐标..然而,这有 http://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=100

阅读API如果你需要更多的细节

+1

对不起,我最初指定的纬度和经度是例子。我纠正了这个问题,以获得有效的经度和纬度。它看起来像我的$回应为空。 – Raymond 2012-03-22 18:20:26

+0

适合我。显示多伦多信息。您必须遇到DNS问题,或者如果您使用的是共享主机,则禁止在服务器上使用 – 2012-03-22 18:31:03