Pulling Through The Full Key:Value API Details From Geonames

383 Views Asked by At

I just want to know how to find out the API root field to pull through all of the information correctly.

I have tried searching through http://www.geonames.org/source-code/javadoc/src-html/org/geonames/WebService.html and pulled the root from:

Element root = connectAndParse(url); 1731 for (Object obj : root.getChildren("timezone"))

but this does not return anything and gives me the error of: Undefined array key "timezone"

If i replace timezone with sunrise then I only get through : "2021-08-15 06:15", which is part of the information and can replace the key to receive one value through at at time.

How can I get the full details through?

<?php

    //Timezone

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

    $executionStartTime = microtime(true);

    $url='http://api.geonames.org/timezoneJSON?lat=' . $_REQUEST['lat'] . '&lng=' . $_REQUEST['lng'] . '&username=simonjadams';

    $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['timezone'];
    
    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($output); 

?>

The geonames API details are

Timezone Webservice Type : REST Url : api.geonames.org/timezone? Parameters : lat,lng, radius (buffer in km for closest timezone in coastal areas),lang (for countryName), date (date for sunrise/sunset); Result : the timezone at the lat/lng with gmt offset (1. January) and dst offset (1. July) Example http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo

This service is also available in JSON format : http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2&username=demo

1

There are 1 best solutions below

1
SvenTUM On

Examining the API

When I requested the URL you gave as an exaple I got the following result:

{
    "sunrise": "2021-08-16 06:16",
    "lng": 10.2,
    "countryCode": "AT",
    "gmtOffset": 1,
    "rawOffset": 1,
    "sunset": "2021-08-16 20:29",
    "timezoneId": "Europe/Vienna",
    "dstOffset": 2,
    "countryName": "Austria",
    "time": "2021-08-16 00:55",
    "lat": 47.01
}

The key you're looking for seems to be timezoneId, not timezone.

Adjusting the Code

Try using

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