PHP+AJAX无刷新实现返回天气预报数据

所属分类: 网络编程 / AJAX相关 阅读数: 198
收藏 0 赞 0 分享
用php来写一个天气预报的模块

天气数据是通过采集中国气象网站的。本来中国天气网站也给出了数据的API接口。以下是API的地址。返回的数据格式为json格式。

1. http://www.weather.com.cn/data/sk/101010100.html
2. http://www.weather.com.cn/data/cityinfo/101010100.html
3. http://m.weather.com.cn/data/101010100.html

URL中的数字”101010100“是城市代码。所以可以先列出每个城市的城市代码,然后php程序接收到了城市代码,再去组装URL,在通过URL来显示该城市的实时天气。

index.php
复制代码 代码如下:

<?php
header("Content-Type:text/html;charset=utf-8");
?>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<head>
<title>weather forecast</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function getCityId(){
var http_request=createAjax();
var url="weatherforecast.php"
var data="cityid="+$("cityId").value;
http_request.onreadystatechange=getWetherInfo;
http_request.open("post",url,true);
http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http_request.send(data);
function getWetherInfo(){
if(http_request.readyState==4 && http_request.status==200){
var info=http_request.responseText;
$("weatherinfo").innerHTML=info;
}
}
}
</script>
</head>
<body>
<select name="cityId" onchange="getCityId();" id="cityId">
<option>--请选择城市--</option>
<option value="101010100">北京</option>
<option value="101020100">上海</option>
<option value="101030100">天津</option>
<option value="101040100">重庆</option>
<option value="101280101">广州</option>
</select>
<span id="weatherinfo"></span>
</body>
</html>

weatherforecast.php
复制代码 代码如下:

<?php
header("Content-Type:text/html;charset=utf-8");
header("Cache-Control:no-cache");
if (isset($_POST['cityid'])){
$cityid=$_POST['cityid'];
$url=$url="http://www.weather.com.cn/data/sk/".$cityid.".html";
}else {
$url="http://www.weather.com.cn/data/sk/101010100.html";
}
$weatherInfo_json=file_get_contents($url);
$weatherInfo=json_decode($weatherInfo_json,true);
$cityName=$weatherInfo['weatherinfo']['city'];
$cityTemp=$weatherInfo['weatherinfo']['temp'];
$cityWd=$weatherInfo['weatherinfo']['WD'];
$cityWs=$weatherInfo['weatherinfo']['WS'];
$cityTime=$weatherInfo['weatherinfo']['time'];
$citySD=$weatherInfo['weatherinfo']['SD'];
echo $weatherinfo="城市名字:$cityName,气温:$cityTemp,风向:$cityWd";
?>
更多精彩内容其他人还在看

AjaxSubmit()提交file文件

这篇文章主要介绍了AjaxSubmit()提交file文件的实例讲解,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Blod的ajax进度条下载实现示例代码

本篇文章主要介绍了基于Blod的ajax进度条下载实现示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Ajax、json实现京东购物车结算界面的数据交互实例

这篇文章主要介绍了使用Ajax、json实现京东购物车结算界面的数据交互实例,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

用Promise解决多个异步Ajax请求导致的代码嵌套问题(完美解决方案)

这篇文章主要介绍了用Promise解决多个异步Ajax请求导致的代码嵌套问题(完美解决方案),需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax 传递JSON实例代码

虽然ajax全称是asynchronous javascript and XML。但目前使用ajax技术时,传递JSON已经成为事实上的标准。这篇文章主要介绍了Ajax 传递JSON实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

ajax用json实现数据传输

本文主要介绍了ajax用json实现数据传输的方法,具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

Ajax中post方法直接返回以0开头数字出错问题分析

这篇文章主要介绍了Ajax中post方法直接返回以0开头数字出错问题分析,需要的朋友可以参考下
收藏 0 赞 0 分享

ajax+springmvc实现C与View之间的数据交流方法

下面小编就为大家带来一篇ajax+springmvc实现C与View之间的数据交流方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Spring MVC前端与后端5种ajax交互方法【总结】

下面小编就为大家带来一篇Spring MVC前端与后端5种ajax交互方法【总结】。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Ajax校验是否重复的实现代码

这篇文章主要介绍了Ajax校验是否重复的实现代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多