html geonames天气api无法获取数据

lstz6jyr  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(202)

有人能帮助找到为什么geonames API不提取数据的问题吗?
看起来很直,但它不工作
示例http://api.geonames.org/weatherJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
html/ AJAX /php代码如下供审阅
提前感谢!

<tr> 
        <td>WEATHER</td>
        <td>Returns a list of weather stations with the most recent weather observation
          <p>       
            <input type="number" id="north" placeholder="North">
            <input type="number" id="south" placeholder="South">
            <input type="number" id="east" placeholder="East">
            <input type="number" id="west" placeholder="West">
        </td>
        <td>
          <button class="button" id="weatherBtn">SUBMIT</button>
        </td>
      </tr>

$("#weatherBtn").click(function () {
    $.ajax({
      url: "libs/php/weather.php",
      type: "POST",
      dataType: "json",
      data: {
        north: $("#north").val(),
        south: $("#south").val(),
        east: $("#east").val(),
        west: $("#west").val(),
      },
      success: function (result) {
        $("#txtA").html(result["data"][0]["stationName"]);
        $("#txtB").html(result["data"][0]["temperature"]);
        $("#txtC").html(result["data"][0]["clouds"]);
        $("#txtD").html(result["data"][0]["humidity"]);
      },
    });
  });

<?php

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

    $executionStartTime = microtime(true);

    $url='http://api.geonames.org/weatherJSON?formatted=true&north=' . $_REQUEST['north'] . '&south=' . $_REQUEST['south'] . '&east=' . $_REQUEST['east'] . '&west=' . $_REQUEST['west'] . '&username=karomal89&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); 

?>
g52tjvyc

g52tjvyc1#

API的结果中不存在“geonames”数组键。但存在名为“weatherObservations”的键。
因此,尝试替换以下行

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

使用以下行:

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

相关问题