Search code examples
jsonopenstreetmapphp-7.4

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


I am trying to get results from a json file fetched from openstreetmap.org. When entering the url into the browser I see the json file beeing returned inside the browser. IfI try to read the json using a php script, then nothing happens. Not if I use file_get_contents, but also not if I use 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);

}

And also if I use file_get_contents, there are no results:

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

}

What am I possibly doing wrong?


Solution

  • I've changed your code according Nominatim Usage Policy.

    In short, CURL is better method for you but have to add at least these HTTP request headers:

    • HTTP Referer
    • User-Agent

    And it's also important to not send more than 1 request per second (read the Usage Policy link above).

    I've changed your code so this should work:

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

    Output:

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