本人小白,觉得好玩,就注册了一个博客。一时也不知道写些什么,就把昨天做的一个简单的网页天气预报写一下吧,希望对各位看官有所帮助。

运行环境: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简单实现的更多相关文章

  1. 天气预报API开发

    天气预报API开发 一.        寻觅篇 最近想要跟着视频练习一下利用API开发一个天气预报系统,就在网上找了一下可以用的API,结果好多都已经失效了... 1.       百度车联网天气预报 ...

  2. (41)zabbix监控api接口性能及可用性 天气预报api为例

    现在各种应用都走api,例如淘宝,天气预报等手机.pad客户端都是走api的,那么平时也得对这些api做监控了.怎么做呢?zabbix的web监控是不二选择了.今天就以天气预报api作为一个例子. 天 ...

  3. Libvlc API 简单说明 [转]

    Libvlc API 简单说明 原文来自http://www.xuebuyuan.com/1519616.html libvlc_instance_t* libvlc_new(int argc, co ...

  4. WEB前端工程师(实践)制作天气预报难度:简单

    需要准备:jQuery Bootstrap 天气预报API(本文中使用API可能会失效请灵活运用) CSS样式可以你自己去写这里只提出jQuery 请求数据和解析JSON数据 { "resu ...

  5. 天气预报API(四):全国城市代码列表(“新编码”)

    说明 天气预报API系列文章涉及到的天气网站10个左右,只发现了中国气象频道和腾讯天气城市代码参数特别: 暂且称 中国气象频道.腾讯天气使用的城市代码为 "新编码" 注:中国气象频 ...

  6. 聚合数据全国天气预报api接口

    查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...

  7. salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)

    Streaming API参考链接: https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming ...

  8. 基于C语言libvirt API简单小程序

    libvirt API简单小程序 1.程序代码如下 #include<stdio.h> #include<libvirt/libvirt.h> int getDomainInf ...

  9. Android访问中央气象台的天气预报API得到天气数据

      最新说明:该接口已失效! 2014-03-04 可申请它公布的API,需申请:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml 在用A ...

随机推荐

  1. 水晶报表连接Oracle做数据报表笔记

    首先,新建一个水晶报表的文件,这个时候要给这个报表文件绑定一个oracle数据源, 选择右侧菜单的这个东西,选择“数据库专家”,打开之后是这么一个界面: 选择建立新连接: 这个地方最关键,也是我为什么 ...

  2. 【转】linux-系统启动流程详解

    第二十章.启动流程.模块管理与 Loader 最近升级日期:2009/09/14 1. Linux 的启动流程分析 1.1 启动流程一览 1.2 BIOS, boot loader 与 kernel ...

  3. 源码安装extundelete以及对遇到问题的解决

    软件下载:http://sourceforge.net/projects/extundelete/ 1.在安装extundelete包./configure时遇到configure: error: C ...

  4. centos 连不上网

    ifc-eth0 里面要加DNS1=192.168.1.1 一定是DNS1这样的,不要是DNS

  5. Git-Flow

    Overview Git-Flow is a high-level command set wrapping low-level Git commands to support the "s ...

  6. UI设计原则

    一.一般原则 简单明了原则: 方便使用原则: 用户向导原则: 实时帮助原则: 自定义功能原则: 界面色彩原则: 二.Web系统适应原则 页面要瘦小 屏幕自适应 浏览器兼容 减少垂直滚动条 禁止水平滚动 ...

  7. Python深入01 特殊方法与多范式

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python一切皆对象,但同时,Python还是一个多范式语言(multi-paradi ...

  8. ios模拟器未能安装此应用程序

    网上介绍了很多方法,觉得有些不太靠谱.这里只解释我试验过的最简单最粗暴的方法: 删除模拟器上旧的APP 以外,也可以做 CLEAN (cmd+shift+K) 把旧的build 删掉.

  9. Good Sentences

    Wine in, truth out One is never too old to learn What is done can not be undone Time tries all thing ...

  10. Eclipse中Build Workspace 优化

    在开发中,发现eclipse 的 Build Workspace 很慢很慢,只要是由于验证js, css, xml 等等静态的文件导致. 我遇到的问题是,项目中有一个js文件很大,怎么都不能Build ...