【问题标题】:How to use weather API in a website如何在网站中使用天气 API
【发布时间】:2013-09-16 10:40:12
【问题描述】:

我正在开发一个天气应用程序,我正在尝试使用来自 worldweatheronline.com 网站的 API。但我的问题是如何开始在我的网站中使用这个天气 API。因为我已经阅读了 SO 的问题并且不清楚解决方案 How to integrate the weather api into my website? 。所以请帮助我如何使用 Javascript 开始使用这些 API。任何例子将不胜感激。请记住,我是网络应用 API 使用的新手。

我从网站得到的回应是

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

请求标头 { “X-Originating-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"
        }]
    }]
   }
   }

【问题讨论】:

  • 您需要从您的应用程序对您的 RESTful Web 服务进行 AJAX 调用。
  • @PrasannaAarthi:有什么例子吗?

标签: javascript web-services api


【解决方案1】:

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

为您的天气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);
    }
});

这是获取当地天气的示例。

【讨论】:

    【解决方案2】:

    你也可以通过 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 />";
    

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

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 1970-01-01
      • 2012-01-13
      • 2020-05-30
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      相关资源
      最近更新 更多