3

Below is the code that I am currently using in which I pass an address to the function and the Nominatim API should return a JSON from which I could retrieve the latitude and longitude of the address from.

function geocode($address){

    // url encode the address
    $address = urlencode($address);

    $url = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1';


    // get the json response
    $resp_json = file_get_contents($url);

    // decode the json
    $resp = json_decode($resp_json, true);


        // get the important data
        $lati = $resp['lat'];
        $longi = $resp['lon'];

            // put the data in the array
            $data_arr = array();            

            array_push(
                $data_arr, 
                    $lati, 
                    $longi
                );

            return $data_arr;

}

The problem with it is that I always end up with an Internal Server Error. I have checked the Logs and this constantly gets repeated:

[[DATE] America/New_York] PHP Notice: Undefined index: title in [...php] on line [...]

[[DATE] America/New_York] PHP Notice: Undefined variable: area in [...php] on line [...]

What could be the issue here? Is it because of the _ in New_York? I have tried using str_replace to swap that with a + but that doesn't seem to work and the same error is still returned.

Also, the URL works fine since I have tested it out through JavaScript and manually (though {$address} was replaced with an actual address).

Would really appreciate any help with this, thank you!

Edit

This has now been fixed. The problem seems to be with Nominatim not being able to pickup certain values and so returns an error as a result

Osman
  • 193
  • 2
  • 12

1 Answers1

5

The errors you have mentioned don't appear to relate to the code you posted given the variables title and area are not present. I can provide some help for the geocode function you posted.

The main issue is that there are single quotes around the $url string - this means that $address is not injected into the string and the requests is for the lat/long of "$address". Using double quotes resolves this issue:

$url = "http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1";

Secondly, the response contains an array of arrays (if were not for the limit parameter more than one result might be expected). So when fetch the details out of the response, look in $resp[0] rather than just $resp.

// get the important data
$lati = $resp[0]['lat'];
$longi = $resp[0]['lon'];

In full, with some abbreviation of the array building at the end for simplicity:

function geocode($address){

    // url encode the address
    $address = urlencode($address);

    $url = "http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1";

    // get the json response
    $resp_json = file_get_contents($url);

    // decode the json
    $resp = json_decode($resp_json, true);

    return array($resp[0]['lat'], $resp[0]['lon']);

}

Once you are happy it works, I'd recommend adding in some error handling for both the http request and decoding/returning of the response.

John C
  • 7,805
  • 2
  • 35
  • 46
  • Thank you for the response! For some reason I'm still getting the server error and the error logs say the same stuff. The file itself works fine because it's able to execute without the function. – Osman Jun 08 '17 at 10:58
  • There's a lot of cities passed to the function and for some reason, every error log starts with `America/New_York` – Osman Jun 08 '17 at 11:01
  • 1
    @Osman the `American/New_York` part is the time zone of the log timestamp. Can you post where you use `title` and `area`? – John C Jun 08 '17 at 11:07
  • 1
    I've updated the question details, I'm using the Slack API to retrieve data about a user's team. The code worked when I was using Google's Geocoder but the error occurred when switching to Nominatim – Osman Jun 08 '17 at 11:13
  • 1
    Right, so those errors are probably not relevant. Try [enabling error displaying](https://stackoverflow.com/a/21429652) to see if you can get the actual issue to show. – John C Jun 08 '17 at 12:48
  • I can't change the php.ini as I think my host has blocked that. However, I've found the error now. Certain addresses such as `Pacific, Honolulu` or `Europe, Warsaw` do not return any values and are what leads to the error. The other addresses work fine such as `Europe, London` and I am not sure why. – Osman Jun 08 '17 at 13:24
  • 1
    Ok this seems to be a problem with Nominatim itself. I have tested out the addresses on [http://nominatim.openstreetmap.org] and they do not return values. It works fine with the Google Maps Geocoder – Osman Jun 08 '17 at 13:25