1

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:

  • GET /nominatim/search.php?q=CALLE+POZO+SANTA+CATALINA+3+Ciudad+Real&format=json&addressdetails=1
  • HTTP/1.1 Host: local.ip
  • User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
  • Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
  • Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
  • Accept-Encoding: gzip, deflate
  • Cookie: PHPSESSID=guebk1lc2vu0ihaoedflmjhof1
  • Connection: keep-alive

Also tried with nominatim.openstreetmap.org as host and same behaviour.

Response:

  • HTTP/1.1 200 OK Date: Wed, 28 Oct 2015 09:01:40 GMT
  • Server: Apache/2.2.22 (Debian)
  • X-Powered-By: PHP/5.4.4-14+deb7u14
  • Access-Control-Allow-Origin: *
  • access-control-allow-methods: OPTIONS,GET
  • Vary: Accept-Encoding
  • Content-Encoding: gzip
  • Content-Length: 369 Keep-Alive: timeout=5, max=100
  • Connection: Keep-Alive
  • Content-Type: application/json; charset=UTF-8

Eclipse headers (debugging)

Request:

  • GET /nominatim/search.php?q=CALLE+POZO+SANTA+CATALINA+3+Ciudad Real&format=json&addressdetails=1 HTTP/1.1
  • User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
  • Host: local.ip
  • Accept: */*

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:

  • HTTP/1.1 200 OK Date: Wed, 28 Oct 2015 09:17:56 GMT
  • Server: Apache/2.2.22 (Debian)
  • X-Powered-By: PHP/5.4.4-14+deb7u14
  • Access-Control-Allow-Origin: *
  • Access-Control-Allow-Methods: OPTIONS,GET
  • Vary: Accept-Encoding
  • Connection: close
  • Content-Type: text/html; charset=UTF-8

asked 23 Oct '15, 09:03

Juanma%20MR's gravatar image

Juanma MR
41447
accept rate: 0%

edited 28 Oct '15, 09:41

4

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.

(23 Oct '15, 09:44) scai ♦
1

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.

(28 Oct '15, 09:40) Juanma MR
1

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 header Content-Encoding: gzip to your request. Just a guess, I don't know anything about Nominatim's internals.

(28 Oct '15, 09:54) scai ♦

I got it. It was accept-encoding as you pointed out. Just wrote the answer. Thanks again.

(28 Oct '15, 11:47) Juanma MR

One Answer:
1

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

curl_setopt($curl, CURLOPT_ENCODING ,"gzip, deflate");

permanent link

answered 28 Oct '15, 11:45

Juanma%20MR's gravatar image

Juanma MR
41447
accept rate: 0%

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:

×591
×30
×21
×15

question asked: 23 Oct '15, 09:03

question was seen: 5,156 times

last updated: 28 Oct '15, 11:47

powered by OSQA