The same URL through webbrowser returns JSON perfectly defined. When trying to acess through a PHP script (file_get_content or curl) I get the response in HTML when using "format=json" in the URL. I am working with Spanish data and response is correct but not in the format I am requesting it. This happens both, with a nominatim installed locally and with the public server from nominatim.openstreetmap.org. I tried this one to check if it was a problem of my installation. The response with the same parameters are identical in both servers. I reviewed the documentation and it mentioned in wiki that the public webservice is open but that it is necessary to define correctly the referer or the agent so that it answers correctly. I make sure of copying agent header from the webbrowser request headers but it goes the same. I got a warning before that about not being able to find agent header in log.php from Nominatim, but after adding it no error is present anymore. However I still get not JSON response from scruipts. Anyway, I am not sure if headers could be the problem. Anyone knows which is the correct header configuration to get the json response from PHP? Or do I have to change any configuration in my server to get json responses from scripts? Web Browser headers (firebug) Request:
Also tried with nominatim.openstreetmap.org as host and same behaviour. Response:
Eclipse headers (debugging) Request:
User-Agent is copied and manually set from the original HTTP request in the PHP script. If not done, Eclipse it is not sending any user-agent header. This is the reason I thought it could be due to headers when reading OSM wiki about service. Response:
asked 23 Oct '15, 09:03 Juanma MR |
Thanks to scai commnets I got the solution. According to documentation it is necessary to send correctly referer or user-agent headers. However it is not true. It works without them, at least in my tests with the headers shown above without user-agent header. However the accept-encoding in the request is compulsory as if Nominatim does not find it, will always response in HTML. I have used curl for the tests and added this header with the following php function (just copied the string from the working web browser header):
answered 28 Oct '15, 11:45 Juanma MR |
I make sure of copying agent header from the webbrowser request headers -> That's wrong. You should NOT fake a user agent, instead specify YOUR program. But that shouldn't be the problem. Can you post the complete HTTP request you are sending? Or compare it on your own with the request your browser sends. You can capture it by using wireshark or tcpdump.
Thanks for your response. I added the headers of both requests. Eclipse is not adding almost any header. This iis why I thought it could be due to headers but I am not sure which are the ones necessary if this is the problem.
It works the same in local or using nominatim.openstreetmap.org service.
One difference is that your browser offers
gzip
encoding in its request. This is then also chosen by Nominatim for its response. Your Eclipse doesn't offer this encoding. Maybe Nominatim needs gzip encoding for returning json? Try to add the headerContent-Encoding: gzip
to your request. Just a guess, I don't know anything about Nominatim's internals.I got it. It was accept-encoding as you pointed out. Just wrote the answer. Thanks again.