400 BAD REQUEST - Search nominatim with php

Hi,
I ve tried all the tips I ve found here and the web , but still get a 400 (or nothing?)…

actual script:
$adr=“some valid german adress”;
$q=“https://nominatim.openstreetmap.org/search?q=“.$adr.”&format=json&limit=1”;
$opts = array(‘http’=>array(‘header’=>“User-Agent: mytestapp\r\n”));
$context = stream_context_create($opts);
$q_json = file_get_contents($q, false, $context);

I ve tried a valid email as the email-parameter, I tried http instead of https,
I tried valid modern browser headers,
I ve also tried curl, but the same 400 BAD REQUEST error…
Whats wrong?
Thanks in advance…

Looking at your code, it may be a violation of the Nominatim Usage Policy

Under the heading Bulk Geocoding there’s the point

Thank you for your help, but thats not it.
The request was called about 50 Times within two days, so no hefty usage…

And if so, how do I find out, if I am blocked?

The same request? That seems to be a very bad idea. Please look at my quote from the Usage Policy. You should have cached this result and send other requests.

You could try another mail address and another user agent, but whether you are blocked or not can only answer someone with access to log files or such.

Hmmm,
the request changed with all my tries, and I ve tried your suggestion with another email
and user agent but still no success.
Perhaps they blocked my IP meanwhile…

Now i have also tried photon but with the same result.

Your problem is definitely not the usage policy. “400” means that your request is mal-formed. Might be an encoding issue. Hard to say from the code snippet.

2 Likes

Ok, I ve got it running with photon.
Setting ignore_errors to TRUE within the stream_context_create gave the answer there.
For the code above that meant:
$opts = array(‘http’=>array(“method” => “GET”,“header”=>“User-Agent: mytestappother”,“ignore_errors”=>TRUE));
Photon doesnt accept the format parameter, so simple removed that and voila…
Nominatim still doesnt work, but maybe they blocked me, script seems fine.

Thank you for your answer. Thats it.
Simply urlencode($adr) has done the job.

1 Like