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

运行环境: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. 【Redis】配置redis主从复制

    阅读目录 redis-3.2.1.master tar zxvf redis-3.2.1.tar.gz mv redis-3.2.1 redis-3.2.1.slave-1 tar zxvf redi ...

  2. 51nod 1392 装盒子

    有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且一个盒子里最多只能直接装入另外一个盒子 (但是可以不断嵌套),例如1 * 1 可 ...

  3. secure crt 基本设置

    基本设置1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1.退 出主机自动关闭窗口 Options => Global ptions => Gen ...

  4. C#如何判断我的程序已经有一个实例正在运行

    static class Program { private static Mutex mutex; /// <summary> /// 应用程序的主入口点. /// </summa ...

  5. 【原】sql 将某列拼成一个字符串

    SQL Server中,写存储过程,时常会碰到这样一个需求:从某个表中取某一列,然后需要将这一列数据以某种形式拼成一个字符串,以供后面使用,下面这种方法能够实现此需求. --取说明书模块枚举,结果格式 ...

  6. TMS320C54x系列DSP的CPU与外设——第2章 TMS320C54x DSP体系结构总体介绍

    第2章 TMS320C54x DSP体系结构总体介绍 本章介绍TMS320C54x DSP体系结构的概况,包括中央处理单元(CPU).存在器和片内外设. C54x DSP采用了高级的改进哈佛结构,用8 ...

  7. FlashBuilder(FB/eclipse) 打开多个无效

    FB也即Eclipse. 想要打开多个FB,只需要新建多个FB的快捷方式,然后在路径上面加上参数 -data "具体路径" 再打开即可. 如: "C:\Program F ...

  8. 在x86转x64的开发过程会遇到各种意外的问题,比如MSScriptControl 在x64下

    遇到这种只能编译成x86的组件,那么就必须将 生成 目标平台 设置成  x86 参考文章: http://www.dotblogs.com.tw/stanley.hsu/archive/2010/04 ...

  9. ADF_General JSF系列1_创建一个简单的JSF Application

    2015-02-17 Creatd By BaoXinjian

  10. transform.localPosition操作时的一些注意事项

    移动GameObject是非常平常的一件事情,一下代码看起来很简单: transform.localPosition += new Vector3 ( 10.0f * Time.deltaTime, ...