PHP头条
热点:

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


用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

<?("Content-Type:text/html;charset=utf-8"?> 
<html> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
<head> 
<title>天气预报-www.jbxue.com-脚本学堂</title> 
<script type="text/javascript" src="ajax.js"></script> 
<script type="text/javascript"> 
 document. http_request= url="weatherforecast.php" 
 data="cityid="+$("cityId")..onreadystatechange=.open("post",url,.setRequestHeader("Content-type","application/x-www-form-urlencoded".(http_request.readyState==4 && http_request.status==200 info=http_request."weatherinfo").innerHTML=</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>

<?("Content-Type:text/html;charset=utf-8"("Cache-Control:no-cache" ((['cityid'=['cityid'=="http://www.weather.com.cn/data/sk/"..".html"="http://www.weather.com.cn/data/sk/101010100.html"=(=json_decode(,=['weatherinfo']['city'=['weatherinfo']['temp'=['weatherinfo']['WD'=['weatherinfo']['WS'=['weatherinfo']['time'=['weatherinfo']['SD' ="城市名字:,气温:,风向:"?>

www.phpzy.comtrue/php/2950.htmlTechArticlePHP+AJAX无刷新实现返回天气预报数据 用php来写一个天气预报的模块 天气数据是通过采集中国气象网站的。本来中国天气网站也给出了数据的API接口。以下是API的地址。返回的数据格式为...

相关文章

相关频道:

PHP之友评论

今天推荐