0

I'm working on a simple AJAX call to retrieve API data from the Geonames website, but I keep getting this error:


Warning: Undefined array key "geonames" in C:\xampp\htdocs\task\libs\php\ocean.php on line 25
{"status":{"code":"200","name":"ok","description":"success","returnedIn":"72 ms"},"data":null}

As you can see, the status code is returning okay but I can't access the data in the array and keep getting the null response.

Javascript:

    $('#oeanBtnRun').click(function() {

        $.ajax({
            url: "Oceanlibs/php/ocean.php",
            type: 'POST',
            dataType: 'json',
            data: {
                lat: $('#selLat').val(),
                lng: $('#selLng').val(),
            },
            success: function(result) {

                console.log(JSON.stringify(result));

                if (result.status.name == "ok") {

                    $('#results').html(result['data']);

                }
            
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR, textStatus, errorThrown);
            }
        }); 
    
    });

html:

<tr>
     <td>2. Get Ocean</td>
     <td>
    <label>Latitude: </label><input id="selLat" type="number"></input>
    <label>Longitude: </label><input id="selLng" type="number"></input>
     </td>
     <td><button class="oceanBtnRun">Run</button></td>
</tr>

php:

<?php

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $executionStartTime = microtime(true);

    $url='http://api.geonames.org/neighbourhoodJSON?formatted=true&lat=' . $_REQUEST['lat'] . '&lng=' . $_REQUEST['lng'] .'&username=kerriemcivor92&style=full';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    
    $result=curl_exec($ch);

    curl_close($ch);

    $decode = json_decode($result,true);    

    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "success";
    $output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";
    $output['data'] = $decode['geonames'];
    

    
    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($output); 

?>

I've tried changing

$output['data'] = $decode['geonames'];

to various forms with zero success.

Any ideas would be greatly appreciated!

ADyson
  • 57,178
  • 14
  • 51
  • 63
Kerrie
  • 1
  • 1
  • 1
    Please show us the JSON returned by the remote service – ADyson Mar 21 '23 at 09:37
  • Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – ADyson Mar 21 '23 at 09:38
  • 3
    Welcome to SO. A first step in debugging a problem with a variable you're having trouble with would be to look at it. Have you tried `var_dump($decode)`, to see what you're actually dealing with? – Don't Panic Mar 21 '23 at 09:42
  • Can you provide suitable values for lat/lng that do not result with `we are afraid we could not find a neighbourhood for latitude and longitude` - everything I tried so far yields that error – Professor Abronsius Mar 21 '23 at 09:58
  • @Don'tPanic Thank you for the tip - used this method to go through line by line and solve the issue! – Kerrie Mar 22 '23 at 09:32
  • Glad you solved it. If you thnk your solution might help others in future, [please post it here as an answer](https://stackoverflow.com/help/self-answer). If the problem was a simple typo or if the solution is already described in [the question](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) linked in the comments here, please consider [closing this question](https://stackoverflow.com/help/closed-questions). – Don't Panic Mar 22 '23 at 10:23

0 Answers0