PHP+AJAX无刷新实现返回天气预报数据
2013-07-31 00:00
726 查看
用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
weatherforecast.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"; ?>
相关文章推荐
- PHP+AJAX无刷新实现返回天气预报数据
- PHP+AJAX无刷新返回天气预报
- jquery实现ajax无刷新读取服务器返回数据
- PHP+AJAX无刷新返回天气预报
- php ajax返回 json数据实例
- jquery返回json类型数据集合简单实现ajax返回多个数据
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
- ajax实现返回数据是html类型的跨域问题
- Ajax获取php返回json数据动态生成select下拉框
- jQuery+Ajax+PHP+Mysql实现分页显示数据
- Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- php与ajax交互——实现php实时返回结果给ajax,并在后台保持继续运行的方法
- java 实现自动编译成json struts2 中不用配置json等jar包来实现低耦合,低入侵式ajax访问返回数据
- jQuery+Ajax+PHP+Mysql实现分页显示数据
- Struts2+Jquery实现ajax并返回json类型数据
- php简单实现查询数据库返回json数据
- ECharts, PHP, MySQL, Ajax, JQuery 实现前后端数据可视化
- Ajax获取php返回json数据动态生成select下拉框的实例
- PHP AJAX 实现跨域保存数据的问题