I used in a PHP script to call the Nominativ API with file_get_contents. This worked excellently until 13.09.2017.

I have tried CURL but no result or error returns.

function file_get_contents_curl($cmd, $query) {
$level=error_reporting();
error_reporting(E_ALL);
$ch = curl_init();
$query= curl_escape($ch,$query);
$url = $cmd.$query;
echo $url.' <--';
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
//Set curl to return the data instead of printing it to the $
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);       
$data = curl_exec($ch);
curl_close($ch);
error_reporting($level);
return $data;

}

/*result of echo
http://nominatim.openstreetmap.org/search?format=json&limit=1&addressdetails=1%26email%3Dg%40example.com%26street%3D19%2BAhornweg%26city%3DKoenigswinter%26country%3DDeutschland%26postalcode%3D53639
*/

Can anyone help me with a working solution?

asked 23 Sep '17, 06:22

user_5359's gravatar image

user_5359
91115
accept rate: 0%

edited 23 Sep '17, 13:58

Richard's gravatar image

Richard ♦
27.0k39239356


PHP's file_get_contents() sends neither HTTP UserAgent nor Referer, which violates the usage policy for the server. You can easily add custom HTTP headers by creating an appropriate stream context like that:

// Create a stream
$opts = array('http'=>array('header'=>"User-Agent: StevesCleverAddressScript 3.7.6\r\n"));
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://nominatim.openstreetmap.org/search/q=Rome', false, $context);

For more information see the PHP user manual.

permanent link

answered 24 Sep '17, 15:58

lonvia's gravatar image

lonvia
5.0k14975
accept rate: 39%

Do you run the script within the https://operations.osmfoundation.org/policies/nominatim/ limits, e.g. Number of requests per second? I see the email parameter is a non-existing email. It's possible you got blocked. That would explain why it stopped working from one day to the next.

permanent link

answered 23 Sep '17, 08:48

mtmail's gravatar image

mtmail
2.0k435
accept rate: 25%

1

the email address has been changed for security reasons. If I'm blocked, I would get a corresponding HTML text.

Unfortunately, the code runs on my development machine, but not on the server. In which direction do I have to see which parameters (CURL installation?) can have an effect?

(23 Sep '17, 11:22) user_5359

I had a similar problem while acquiring coordinates for geocoding. I need to add also my email address to url. I combined info from this: link text and this: link text to get this:

        $url = 'https://nominatim.openstreetmap.org/search/'.rawurlencode($location).'?format=json&limit=1&email=youremail@gmail.com';

        $data = ''; // empty post
        $opts = array(
            'http' => array(
            'header' => "Content-type: text/html\r\nContent-Length: " . strlen($data) . "\r\n",
            'method' => 'POST'
            ),
        ); 
        // Create a stream
        $context = stream_context_create($opts);
        // Open the file - get the json response using the HTTP headers set above
        $jsonfile = file_get_contents($url, false, $context);
        // decode the json 
        if (!json_decode($jsonfile, TRUE)) {return false;}else{
        //if (empty(array_filter($resp))) {return false;}else{
        $resp = json_decode($jsonfile, true);
        //if(is_string($resp)){$resp = 'true';}else{$resp = 'itsnot';}
        // Extract data (e.g. latitude and longitude) from the results    
        $gps['latitude'] = $resp[0]['lat'];
        $gps['longitude'] = $resp[0]['lon'];
        return $gps;}
permanent link

answered 31 Jul, 01:21

Pablo's gravatar image

Pablo
1
accept rate: 0%

Cool, thanks to lonvia here's a practical example to get around this nonsense with php. Using the browsers user_agent as a variable.

function OsmAddress() {
    //vars
    $Latitude ='22.56589015';
    $Longitude ='113.9259518';
    $USERAGENT = $_SERVER['HTTP_USER_AGENT'];

    //main
    $opts = array('http'=>array('header'=>"User-Agent: $USERAGENT\r\n"));
    $context = stream_context_create($opts);
    $url4 = file_get_contents("https://nominatim.openstreetmap.org/reverse?format=json&lat=$Latitude&lon=$Longitude&zoom=18&addressdetails=1", false, $context);
    $osmaddress = json_decode($url4);  
    $osmaddress1 = $osmaddress->display_name;
    echo $osmaddress1;
}
OsmAddress();
permanent link

answered 02 Dec, 07:35

J0naz's gravatar image

J0naz
111
accept rate: 0%

So you are re-using/faking the user's useragent for your server-side script to execute a nominatim query on the user's behalf? Not sure if that is a good idea with regards to complying with https://operations.osmfoundation.org/policies/nominatim/ . Do not be surprised if your access will be blocked at some point in time. I guess that setting a proper referer, custom user agent (your application's name) a contact email address would be a better way to go.

Note that to get around that "nonsense" you are free (and very welcome) to set up your own nominatim service using our free data.

(02 Dec, 11:14) aseerel4c26 ♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×517
×214
×28

question asked: 23 Sep '17, 06:22

question was seen: 2,778 times

last updated: 02 Dec, 11:15

powered by OSQA