I can’t figure out the right way to query Nominatim from PHP:
<?php
function getAddressThroughFileGetContents ($RG_Lat,$RG_Lon) {
// Create a stream
$opts = array('http'=>array('header'=>"User-Agent: MyCleverAddressScript 1.0.0\r\n"));
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$query = "https://nominatim.openstreetmap.org/reverse?format=json&lat=".$RG_Lat."&lon=".$RG_Lon."&zoom=27&addressdetails=1&email=j.doe@acme.com";
//Warning: file_get_contents(https://...@acme.com): failed to open stream: Connection timed out in /var/www/sda/4/0/myserver/myscript.php on line 47
$result = file_get_contents($query, false, $context);
return $result;
}
$lon = 1.23;
$lat = 4.56;
$addr = getAddressThroughFileGetContents($lat,$lon);
echo "Address: ".$addr;
?>
Am I missing yet another required parameter in the URL?
Same issue if I use the EasyPHP server installed on my very own computer:
Warning: file_get_contents(https://...@acme.com): failed to open stream: No error in C:\Program Files\EasyPHP-Devserver-17\eds-www\myscript.php on line 67
Since the query works fine when copy/pasted in a web browser, it looks like Nominatim expects something else when using the PHP script above.
–
Edit:
For others’ benefit: The code works fine with Xampp (7.2.9)
I’ll see if there’s a lighter, free web + PHP server for Windows (Xampp install file = 124MB!).