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

运行环境: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. LeetCode "Binary Tree Vertical Order"

    BFS + HashTable class Solution { int maxl, minl; unordered_map<int, vector<int>> hm; pub ...

  2. 在Visual Studio里配置及查看IL(转载)

    原文地址:http://www.myext.cn/other/a_25162.html 在之前的版本VS2010中,在Tools下有IL Disassembler(IL中间语言查看器),但是我想直接集 ...

  3. showdialog窗体不在任务栏显示的问题处理

    场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...

  4. 在eclipse中设计BPMN 2.0工作流定义的根本步骤

    原文地址:http://www.myexception.cn/eclipse/1863140.html 在eclipse中设计BPMN 2.0工作流定义的基本步骤 1. Activiti问我们提供了A ...

  5. linq简介

    语言集成查询(Language INtegrated Query,LINQ)是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .N ...

  6. Grid_Oracle Grid Infrastructure概念介绍(概念)

    2014-03-09 Created By BaoXinjian Thanks and Regards

  7. HDFS的运行原理(转)

    简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...

  8. centos7 安装中文编码

    运行如下 yum groups mark install "Chinese Support"

  9. Zend Guard Run-time support missing问题的解决

    Zend Guard不仅可以实现对PHP应用的脚本进行加密保护和对PHP应用的产品进行商业许可证管理,还可以为许多软件生产商.IT服务提供商提供完善的加密和安全的产品发布系统. 虽然现在可以成功加密p ...

  10. 30天轻松学习javaweb_tomcat的虚拟目录设置

    1.在server.xml文件的</Host>前面加入.需要重新启动Tomcat才能生效.<!--配置虚拟目录--><Context path="/itcast ...