简体   繁体   English

geonames 天气 api 未获取数据

[英]geonames weather api not fetching data

Can anyone please help to find the issue why geonames API is not fetching the data?谁能帮忙找出 geonames API 没有获取数据的问题?

Looks pretty straighforward, but its not working看起来很简单,但它不起作用

Example http://api.geonames.org/weatherJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo示例http://api.geonames.org/weatherJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo

html/ajax/php codes below for review下面的 html/ajax/php 代码供审查

Many thanks in advance!提前谢谢了!

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

?>


There exists no 'geonames' array key in the result of the api. But there is a key with the name 'weatherObservations'. api 的结果中不存在“geonames”数组键。但是有一个名为“weatherObservations”的键。

So try to replace the following line所以尝试替换以下行

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

with this line:用这一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM