使用 Nominatim 和 php file_get_contents() 或 Curl 未从 openstreet 地图获得任何 json 结果

Not getting any json results from openstreet map using Nominatim and php file_get_contents() or Curl

我正在尝试从从 openstreetmap.org 获取的 json 文件中获取结果。在浏览器中输入 url 时,我看到 json 文件在浏览器中返回。如果我尝试使用 php 脚本读取 json,则什么也不会发生。如果我使用 file_get_contents 则不会,如果我使用 curl.

也不会
function geocode($address){

    // url encode the address
    $address = urlencode($address);

    //Url openstreetmap
    $url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";

    //  Initiate curl
    $ch = curl_init();
    
    // Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Set the url
    curl_setopt($ch, CURLOPT_URL,$url);

    // Execute
    $result=curl_exec($ch);

    // Closing
    curl_close($ch);

    // Will dump a beauty json :3
    var_dump(json_decode($result, true));

    return json_decode($result, true);

}

而且如果我使用 file_get_contents,也没有结果:

function geocode($address){

    // url encode the address
    $address = urlencode($address);

    $url = "http://nominatim.openstreetmap.org/search/?format=json&addressdetails=1&q={$address}&format=json&limit=1";

    // get the json response
    $resp_json = file_get_contents($url);

    return json_decode($resp_json, true);

}

我可能做错了什么?

我已经根据 Nominatim Usage Policy 更改了您的代码。

简而言之,CURL 对您来说是更好的方法,但必须至少添加这些 HTTP 请求 headers:

  • HTTP 引荐来源网址
  • User-Agent

而且每秒发送的请求不要超过 1 个也很重要(请阅读上面的使用政策 link)。

我已经更改了您的代码,所以这应该可以工作:

<?php

function geocode($address){
    $address = urlencode($address);

    $url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");

    $result = curl_exec($ch);

    curl_close($ch);

    return json_decode($result, true);
}

echo "<pre>";
print_r(geocode("Time Square, New York City"));

输出:

Array
(
    [0] => Array
        (
            [place_id] => 162597874
            [licence] => Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
            [osm_type] => way
            [osm_id] => 304980452
            [boundingbox] => Array
                (
                    [0] => 41.7382344
                    [1] => 41.7384706
                    [2] => -74.0371548
                    [3] => -74.0367398
                )

            [lat] => 41.73835325
            [lon] => -74.03694730280651
            [display_name] => Time Square, 652, NY 299, Elting Corners, Lloyd, Town of Lloyd, Ulster County, New York, 12561, United States
            [class] => building
            [type] => yes
            [importance] => 0.401
            [address] => Array
                (
                    [building] => Time Square
                    [house_number] => 652
                    [road] => NY 299
                    [hamlet] => Elting Corners
                    [town] => Lloyd
                    [municipality] => Town of Lloyd
                    [county] => Ulster County
                    [state] => New York
                    [postcode] => 12561
                    [country] => United States
                    [country_code] => us
                )

        )

)