简体   繁体   English

在php中从url获取查询

[英]Get a query from url in php

I don't know how to do this in php. 我不知道如何在PHP中这样做。 I am trying to get geocode data from openstreetmap.org but I need to do this in php. 我试图从openstreetmap.org获取地理编码数据,但我需要在php中执行此操作。

In Javascript (and I have this working), it's: 在Javascript(我有这个工作),它是:

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

I have that url but I don't know how to execute it in php. 我有那个网址,但我不知道如何在PHP中执行它。

I have tried: 我努力了:

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

But this doesn't work. 但这不起作用。 I tried the same with curl. 我用curl尝试了同样的方法。 Any ideas or is this type of query not possible in php? 任何想法或这种类型的查询不可能在PHP?

Use this; 用这个;

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

which will return you with an array of the values, see below; 它会返回一组值,见下文;

 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
    )

 )

Then manipulate the data however you see fit. 然后操纵您认为合适的数据。

It does return 它确实回来了

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

That just means that there is no address for that particular coordinate.. This however has http://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=100 这只是意味着该特定坐标没有地址..但这有http://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=100

Read API if you need more details 如果您需要更多详细信息,请阅读API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM