【问题标题】:How to use weather API to create my weather HTML page [closed]如何使用天气 API 创建我的天气 HTML 页面 [关闭]
【发布时间】:2014-08-05 13:59:34
【问题描述】:

我想用 HTML 和 CSS 编写一个页面。 我已经设计了它,但是我迷路了,因为我不知道如何将它与天气 API 链接以获取所选城市/国家的天气。 谁能给我举例说明代码是如何运行的?

【问题讨论】:

  • 您必须查看 API 文档,该文档应该会告诉您如何从服务请求数据。我们无法帮助您,因为您没有告诉我们服务是什么,即使您告诉我们,我们也不会为您编写代码。这个网站是为了解决你的代码问题,而不是让它看起来凭空出现。
  • 您正在使用/寻找什么样的 API?也许您想避免使用 PHP 并使用 JavaScript API?
  • 讨论天气特定的 API here
  • ahmadhammoud.com/wtest/weather.html 假设每次有人选择一个城市时,我都有这个设计,它必须给他合适的天气温度。
    怎么才能做到并且仍然让天气温度更新
  • @user3671574 你需要学习基本的 JS 和 HTML;然后,您将不得不学习如何将 API 与 JS 一起使用,以及如何将 JS 和 HTML 与 jQuery 或 getElement...() 连接起来。请参阅下面我的答案中的链接。

标签: php css webpage weather weather-api


【解决方案1】:

您必须查看 API 文档,该文档应该会告诉您如何从服务中请求数据。如果没有指向它的链接,我们将无法帮助您。

This 应该让您大致了解如何在 PHP 中使用 JSON 和 API,但如果您正在寻找一种简单的拉动机制来更新页面上的天气,则不需要那么重的东西:它不需要是服务器端的。

我建议在这个上使用 JavaScript,它会是这样的:

var req = new XMLHttpRequest();
var url = "yourURL"; //it is important that you read the API docs for this, because some APIs require you to use your API key in your request

req.open('GET', url, true);
req.onload = function (e) {
    if (req.readyState == 4 && req.status == 200) {
        if (req.status == 200) {
            var response = JSON.parse(req.responseText); //response is now an object containing all of the data that the API gives you; again, you'll have to look at the API docs to see how that information is formatted
            //update your page here using response data
            }
        }
    }
};
req.send(null);

我还建议填写How to use APIs with JavaScript

无论哪种方式,您都会得到一个 JSON 对象作为回报,其格式如下:

var response = [
    {
        "name": "Australia",
        "website": "http://www.footballaustralia.com.au",
        "foundedYear": 1961,
        "address": "Locked Bag A 4071\nNSW 1235\nSydney",
        "homeStadium": "ANZ Stadium",
        "stadiumCapacity": 83500,
        "group": "B",
        "groupRank": 3,
        "groupPoints": 0,
        "matchesPlayed": 1,
        "wins": 0,
        "losses": 1,
        "draws": 0,
        "goalsFor": 1,
        "goalsAgainst": 3,
        "goalsDiff": "-2",
        "id": "16EF7687-2D69-473C-BFE7-B781D67752DC",
        "type": "Team"
    }, 
    {
        "name": "England",
        "website": "http://www.thefa.com",
        "foundedYear": 1863,
        "address": "PO Box 1966\nSW1P 9EQ\nLondon",
        "homeStadium": "Wembley Stadium",
        "stadiumCapacity": 90000,
        "group": "D",
        "groupRank": 3,
        "groupPoints": 0,
        "matchesPlayed": 1,
        "wins": 0,
        "losses": 1,
        "draws": 0,
        "goalsFor": 1,
        "goalsAgainst": 2,
        "goalsDiff": "-1",
        "id": "2EFCFEB2-EBF8-4628-B659-B00C49D93811",
        "type": "Team"
    },
    {
        "name": "Ghana",
        "website": "http://www.ghanafa.org",
        "foundedYear": 1957,
        "address": "South East Ridge\n19338\nAccra",
        "homeStadium": "Ohene Djan Sports Stadium",
        "stadiumCapacity": 35000,
        "group": "G",
        "groupRank": 2,
        "groupPoints": 0,
        "matchesPlayed": 0,
        "wins": 0,
        "losses": 0,
        "draws": 0,
        "goalsFor": 0,
        "goalsAgainst": 0,
        "goalsDiff": "+0",
        "id": "CCC66F75-7004-46E4-BB31-259B06A42516",
        "type": "Team"
    }
];

因此,例如,您可以使用

访问澳大利亚的网站
response[0].website;

您也可以使用纯 XML,但 JSON 是最流行的 API 请求方式。

【讨论】:

  • 这是否意味着我必须从 0 开始学习 JSON?没有更快的方法或一些我可以使用的现成代码吗?
  • JSON 只是一种简单的格式化文本的方式;一个例子是我在上面的response 中存储的内容。您必须阅读 API 的文档以了解它如何存储您正在访问的数据。您从哪里获取天气数据?
  • 假设我从 wunderground.com/weather/api 获取 API,他们给了我一个链接,就像一个推荐 URL 我如何使用它来发送和接收天气更新
  • @user3671574 如果链接是以 JSON 格式指向他们的 API,您可以使用我上面提供的示例从 API 中提取数据以用于您的应用程序。
  • 谢谢,我会在发现你在那里写的内容后向你提供我的结果,现在一切都清楚了,感谢你和你来自 codacademy 的培训链接。
猜你喜欢
  • 2010-12-01
  • 1970-01-01
  • 2016-01-27
  • 2020-05-30
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
相关资源
最近更新 更多