如何在网站中使用天气API

How to use weather API in a website

本文关键字:API 网站      更新时间:2023-09-26

我正在开发一个天气应用程序,我正在尝试使用 worldweatheronline.com 网站的API。 但我的问题是如何开始在我的网站中使用此天气 API。 因为我已经阅读了SO的一个问题,并且不清楚解决方案 如何将天气 API 集成到我的网站中?.所以请帮助我如何使用Javascript开始使用这些API。任何例子将不胜感激。请记住,我是 Web 应用程序 API 用法的新手。

我从网站上得到的回复是

Request URI
  http://api.worldweatheronline.com/free/v1/search.ashx?q=Chennai&format=json&key=53jjtnrm9d5jucpmxyyhj7vn

请求标头 { "X-源 IP": "14.140.167.22" }

Response Headers
     {
   "Age": "0",
   "Cache-Control": "public, no-cache='"Set-Cookie'", max-age=120",
   "Content-Type": "application/json; charset=utf-8",
  "Date": "Mon, 16 Sep 2013 09:02:34 GMT",
  "Server": "Microsoft-IIS/7.5",
  "X-Aspnet-Version": "4.0.30319",
  "X-Cache": "MISS",
  "X-Mashery-Responder": "prod-j-worker-eu-west-1a-96.mashery.com",
  "X-Powered-By": "UKFast Webcelerator",
  "Transfer-Encoding": "chunked",
  "Connection": "keep-alive"

}

 Response Body
      {
    "search_api": {
    "result": [{
        "areaName": [{
            "value": "Chennai"
        }],
        "country": [{
            "value": "India"
        }],
        "latitude": "13.083",
        "longitude": "80.283",
        "population": "0",
        "region": [{
            "value": "Tamil Nadu"
        }],
        "weatherUrl": [{
            "value": "http:'/'/www.worldweatheronline.com'/Chennai-weather'/Tamil-Nadu'/IN.aspx"
        }]
    }]
   }
   }

请访问 http://api.jquery.com/jQuery.ajax/,了解如何调用Web服务。

对于您的天气API,请注册,获取密钥

  $.ajax(
{
    Type: "GET",
    contentType: "application/json",
    url: "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=yourkey",
    success: function (msg) {
        $("#success").text(msg);
    }
});

这是获取当地天气的一个例子。

你也可以通过 php 这样做:

$url="http://api.worldweatheronline.com/free/v1/weather.ashx?q=[LOCATION]&format=json&num_of_days=5&key=[YOUR_KEY]";
$json=file_get_contents($url);
$data=json_decode($json,true);
//var_dump($data);
echo $data['data']['weather'][0]['tempMaxF']."<br />";

这就是我正在使用的,因为我只是让它在加载时运行,而不是在应用程序中使用它;只是在网页上。