天气预报API简单实现
本人小白,觉得好玩,就注册了一个博客。一时也不知道写些什么,就把昨天做的一个简单的网页天气预报写一下吧,希望对各位看官有所帮助。
运行环境:php+mysql+WIN/Linux,框架什么的都无所谓了吧。
个人理解:

很简单的,通过API获取到天气的Json数据,然后后台传给前端展示,css渲染。
首先,获取API的数据:

我这里找的是一个免费的天气预报API,方便实用,用的人也多: http://apistore.baidu.com/apiworks/servicedetail/112.html
我把相关的方法给写在一个php文件里,方便使用:weather.php
function weather_excu_curl($url){
$ch = curl_init();
$header = array(
'apikey:你自己的apikey',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return $res;
}
//根据城市名称-- type == 0 获取城市代码,!=0 获取天气整体信息
function get_cityCode($cityname,$type){
//获取城市代码
$url_city = 'http://apis.baidu.com/apistore/weatherservice/cityname?cityname='.$cityname;
$res = weather_excu_curl($url_city);
$res = json_decode($res);
$errnum = $res->errNum;
if($type == 0) {
//当地基本天气信息
if ($errnum != -1) {
return $res->retData->citycode;
} else {
return null;
}
}else{
return $res;
}
}
//带历史7天和未来4天--天气查询
function get_recent_weather($citycode){
if($citycode) {
$url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers?cityid=' . $citycode;
return weather_excu_curl($url);
}else{
return null;
}
}
然后,把得到的数据放到前端就可以了,我这里使用ajax进行异步加载的方式:
js文件
function getWeather(){
//dealid 传递给后台处理的参数
var dealid = $("#dealid").val();
$.ajax({
url:"你的后台处理地址",
dataType: "json",
async:false,
data:{"dealid":dealid},
type:"POST",
success:function(msg){
var container = $("#weather_info");
if(msg['status']!=0) {
var data = msg['data']['retData'];
console.log(data);
$("#weather_nav .weather_city").html(data['city']);
if(data.today){
var history = data.history;
var forecast = data.forecast;
//data today weather
var todayContent = data.today.index;
var todayHtml = "";
var todayLength = todayContent.length;
for (var i=0;i<todayLength;i++){
todayHtml += "<div class='"+todayContent[i]['code']+"'>" +
"<p>"+todayContent[i]['name']+" "+todayContent[i]['index']+"</p>" +
"<p>"+todayContent[i]['details']+"</p>" +
"</div>";
}
container.append("<div class='weather_today'>" +
"<ul>" +
"<li>温度 :"+data.today.curTemp+" / "+data.today.type+"</li>" +
"<li>时间 :"+data.today.date+" / "+data.today.week+"</li>" +
"<li>风力 :"+data.today.fengli+"</li>" +
"<li>风向 :"+data.today.fengxiang+"</li>" +
"<li>最高温 :"+data.today.hightemp+"</li>" +
"<li>最低温 :"+data.today.lowtemp+"</li>" +
"<li>PM值 :"+data.today.aqi+"</li>" +
"<li>"+todayHtml+"</li>" +
"</ul>" +
"</div>");
//history weather
var historyLength = history.length;
var historyHtml = "";
for(var i=0; i < historyLength;i++){
historyHtml +="<li><p>天气 :"+history[i]['type']+"</p>" +
"<p>时间 :"+history[i]['date']+" / "+history[i]['week']+"</p>" +
"<p>风力 :"+history[i]['fengli']+"</p>"+
"<p>风向 :"+history[i]['fengxiang']+"</p>"+
"<p>最高温 :"+history[i]['hightemp']+"</p>"+
"<p>最低温 :"+history[i]['lowtemp']+"</p>"+
"<p>PM值 :"+history[i]['aqi']+"</p></li>";
}
container.append("<div class='weather_history'><ul>"+historyHtml+"</ul></div>");
//forecast weather
var forecastLength = forecast.length;
var forecastHtml = "";
for(var i=0; i < forecastLength;i++){
forecastHtml +="<li><p>天气 :"+forecast[i]['type']+"</p>" +
"<p>时间 :"+forecast[i]['date']+" / "+forecast[i]['week']+"</p>" +
"<p>风力 :"+forecast[i]['fengli']+"</p>"+
"<p>风向 :"+forecast[i]['fengxiang']+"</p>"+
"<p>最高温 :"+forecast[i]['hightemp']+"</p>"+
"<p>最低温 :"+forecast[i]['lowtemp']+"</p></li>";
}
container.append("<div class='weather_forecast'><ul>"+forecastHtml+"</ul></div>");
}
}else {
container.append(msg.content);
$("#weather_nav").css("display","none");
}
},
error:function(){
console.log("出错");
}
});
}
后台处理代码(要include 之前写的 weather.php):
require '/weather.php';
class weatherModule extends BaseModule
{
public function weather(){
$dealid = $_POST['dealid'];
$deal = mysql查询到相关的数据;
//city
$cityCode = get_cityCode($deal['city'],0);
if($cityCode) {
$res = get_recent_weather($cityCode);
echo json_encode(array("status"=>1,"data"=>json_decode($res)));
}else{
//province
$citycode0 = get_cityCode($deal['province']);
if($citycode0){
$res0 = get_recent_weather($citycode0,0);
echo json_encode(array("status"=>2,"data"=>json_decode($res0)));
}else{
echo json_encode(array("status"=>0,"content"=>"没有查到该地区天气数据"));
}
}
}
}
最后页面展示html部分代码:
<input id="dealid" type="text" placeholder="输入赛事id 查询最近城市天气" style="width: 400px;">
<input type="submit" onclick="getWeather()">
<div id="weather_info">
<!--这里是Js异步加载的天气数据-->
</div>
</body>
代码写完了,我发现这个天气预报的样子和自己想象的简直云泥之别:

剩下的,就交给美工和前端吧。
天气预报API简单实现的更多相关文章
- 天气预报API开发
天气预报API开发 一. 寻觅篇 最近想要跟着视频练习一下利用API开发一个天气预报系统,就在网上找了一下可以用的API,结果好多都已经失效了... 1. 百度车联网天气预报 ...
- (41)zabbix监控api接口性能及可用性 天气预报api为例
现在各种应用都走api,例如淘宝,天气预报等手机.pad客户端都是走api的,那么平时也得对这些api做监控了.怎么做呢?zabbix的web监控是不二选择了.今天就以天气预报api作为一个例子. 天 ...
- Libvlc API 简单说明 [转]
Libvlc API 简单说明 原文来自http://www.xuebuyuan.com/1519616.html libvlc_instance_t* libvlc_new(int argc, co ...
- WEB前端工程师(实践)制作天气预报难度:简单
需要准备:jQuery Bootstrap 天气预报API(本文中使用API可能会失效请灵活运用) CSS样式可以你自己去写这里只提出jQuery 请求数据和解析JSON数据 { "resu ...
- 天气预报API(四):全国城市代码列表(“新编码”)
说明 天气预报API系列文章涉及到的天气网站10个左右,只发现了中国气象频道和腾讯天气城市代码参数特别: 暂且称 中国气象频道.腾讯天气使用的城市代码为 "新编码" 注:中国气象频 ...
- 聚合数据全国天气预报api接口
查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...
- salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)
Streaming API参考链接: https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming ...
- 基于C语言libvirt API简单小程序
libvirt API简单小程序 1.程序代码如下 #include<stdio.h> #include<libvirt/libvirt.h> int getDomainInf ...
- Android访问中央气象台的天气预报API得到天气数据
最新说明:该接口已失效! 2014-03-04 可申请它公布的API,需申请:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml 在用A ...
随机推荐
- 【linux】如何查看和解压缩rpm文件内容
查看rpm文件中的内容 http://www.cyberciti.biz/faq/howto-list-find-files-in-rpm-package/ Use following syntax ...
- HAML学习
来源:http://ningandjiao.iteye.com/blog/1772845 一个技术能够风靡,一定是有它的原因的,在熟悉之前,我们没有资格去对它做任何的判断. Haml 是一种简洁优美的 ...
- Glibc 与 libc 的区别和联系
转http://blog.163.com/dragon_sjl@126/blog/static/100473339201107101517380/ 1.gcc(gnu collect compiler ...
- python-unicode十进制数字转中文
#coding:utf-8 '''主要是unichr()函数.以下数组中的元素转换后为繁体中文,若不加encode("GB18030")就不能正确显示,而且会报错:('gbk' c ...
- Maven使用-- 编写POM
就像Make的Makefile.Ant的build.xml一样,Maven项目的核心是pom.xml. POM(Project Object Model,项目对象模型)定义了项目的基本信息,用 ...
- [Android Studio 1A] – 插件
关于android studio插件的使用,一直没有好的机会来研究,最近项目开发中需要统计项目的代码行数,所以就需要一个代码统计的插件.我选择的是 Statistic,下载地址是:https://pl ...
- [JS]setInterval,setTimeout的使用以及伪随机数
- HDU 2188 悼念512汶川大地震遇难同胞――选拔志愿者(巴什博奕)
选拔志愿者 题意: 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心理疏导的心理学专家.根据要求,我 ...
- 0810HTML(表单)
图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. <img src="a006.jpg" title="这是企鹅" ...
- 子iframe 怎么调用 父级的JS函数
window.parent.父级函数名();