【问题标题】:Pulling Through The Full Key:Value API Details From Geonames从 Geonames 提取完整的 Key:Value API 详细信息
【发布时间】:2021-08-15 21:04:02
【问题描述】:

我只想知道如何找出 API 根字段以正确提取所有信息。

我已尝试搜索 http://www.geonames.org/source-code/javadoc/src-html/org/geonames/WebService.html 并从以下位置提取根:

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

但这不会返回任何内容并给我以下错误:未定义的数组键“时区”

如果我用日出替换时区,那么我只能通过:“2021-08-15 06:15”,这是信息的一部分,可以替换密钥以通过 at 接收一个值时间。

我怎样才能得到完整的细节?

<?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); 

?>

geonames API 详细信息是

时区 网络服务类型:REST 网址:api.geonames.org/timezone? 参数:lat、lng、radius(沿海地区最近时区的缓冲区以km为单位)、lang(国家名称)、日期(日出/日落日期); 结果: lat/lng 的时区,gmt 偏移量(1. 一月)和 dst 偏移量(1. 七月) 示例http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo

此服务也提供 JSON 格式:http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2&username=demo

【问题讨论】:

  • first: demo 的每日额度已超过 20000 credits 如果您想了解 API 的详细信息,请阅读文档。没有人可以帮助您调试付费 API。
  • 您好,我使用 &username=simonjadams 作为帐户(如上面的代码所示)。这是一个免费的 GeoNames 帐户,而不是付费帐户。

标签: php geonames


【解决方案1】:

检查 API

当我请求您作为示例提供的 URL 时,我得到了以下结果:

{
    "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
}

您要查找的密钥似乎是 timezoneId,而不是 timezone

调整代码

尝试使用

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

【讨论】:

  • @simonjadams 建议的调整解决了您的问题吗?
猜你喜欢
  • 2020-11-16
  • 2018-07-25
  • 2019-04-22
  • 2017-02-09
  • 1970-01-01
  • 2015-10-01
  • 2016-12-07
  • 2014-10-15
  • 2012-02-29
相关资源
最近更新 更多