How to use Nominatim API through PHP

126 views Asked by At

It's a more general version of the topic How to use Nominatim API through PHP to retrieve latitude and longitude? which was asked in June 2017.

Currently, I just get an empty response.

Is it still possible to use the API with PHP file_get_contents?


Example API call

https://nominatim.openstreetmap.org/search?q=grindelwald&countryCodes=CH&addressdetails=1&format=json

1

There are 1 answers

0
cetver On BEST ANSWER

This site required the User-Agent header

$url = 'https://nominatim.openstreetmap.org/search?q=grindelwald&countryCodes=CH&addressdetails=1&format=json';
$options = [
    'http' => [
        'user_agent' => 'Simon Ferndriger',
    ],
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

I recommend using the guzzle package for HTTP requests.