Php 如何使用天气API创建我的天气HTML页面

Php 如何使用天气API创建我的天气HTML页面,php,css,webpage,weather,weather-api,Php,Css,Webpage,Weather,Weather Api,我想用HTML和CSS编写一个页面。 我已经设计了它,但我不知道如何将它与天气API链接,以获取所选城市/国家的天气。 有人能给我举个例子说明代码是如何运行的吗?您必须查看API文档,它应该告诉您如何从服务请求数据。没有链接,我们帮不了你 应该可以让您大致了解如何在PHP中使用JSON和API,但是如果您正在寻找一种简单的拉动机制来更新页面上的天气,那么您不需要这么重的东西:它不需要是服务器端的 我建议在这个问题上使用JavaScript,它应该是这样的: var req = new XMLHt

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

您必须查看API文档,它应该告诉您如何从服务请求数据。没有链接,我们帮不了你

应该可以让您大致了解如何在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);
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"
    }
];
我还建议您完成

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

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);
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请求方式。

您必须查看API文档,它应该告诉您如何从服务请求数据。我们无法帮助您,因为您没有告诉我们服务是什么,即使您告诉了,我们也不会为您编写代码。这个网站是用来解决你的代码问题的,而不是凭空出现。你在使用/寻找什么样的API?也许你想避免使用PHP而使用JavaScript API?我们讨论了天气特定的API,比如说我有这种设计,每次有人选择一个城市时,它必须给他正确的天气tmp<怎样才能在保持天气温度不变的情况下做到这一点updated@user3671574你需要学习基本的JS和HTML;然后,您必须学习如何将API与JS结合使用,以及如何将JS和HTML与jQuery或
getElement…()
连接起来。请参阅下面我答案中的链接。这是否意味着我必须从0学习JSON?难道没有一种更快的方法或一些现成的代码可以使用吗?JSON只是一种格式化文本的简单方法;一个例子是我在上面的
response
中存储的内容。您必须阅读API的文档,以了解它如何存储您正在访问的数据。你从哪里获取天气数据?比如说,我从他们那里获取API,他们给了我一个链接,就像一个推荐URL,我如何使用它发送和接收天气updates@user3671574如果链接是JSON格式的API,您可以使用我上面提供的示例从API中提取数据,以便在应用程序中使用。谢谢,在发现你在上面写的内容后,我会向你提供我的结果,现在一切都清楚了,感谢你和你在codacademy的培训链接。