PHP+AJAX無刷新實現(xiàn)返回天氣預(yù)報數(shù)據(jù)
更新時間:2013年07月31日 17:54:37 作者:
天氣數(shù)據(jù)是通過采集中國氣象網(wǎng)站的。本來中國天氣網(wǎng)站也給出了數(shù)據(jù)的API接口,接下來為大家介紹下用php來寫一個天氣預(yù)報的模塊,感興趣的朋友可以參考下
用php來寫一個天氣預(yù)報的模塊
天氣數(shù)據(jù)是通過采集中國氣象網(wǎng)站的。本來中國天氣網(wǎng)站也給出了數(shù)據(jù)的API接口。以下是API的地址。返回的數(shù)據(jù)格式為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中的數(shù)字”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,風(fēng)向:$cityWd";
?>
天氣數(shù)據(jù)是通過采集中國氣象網(wǎng)站的。本來中國天氣網(wǎng)站也給出了數(shù)據(jù)的API接口。以下是API的地址。返回的數(shù)據(jù)格式為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中的數(shù)字”101010100“是城市代碼。所以可以先列出每個城市的城市代碼,然后php程序接收到了城市代碼,再去組裝URL,在通過URL來顯示該城市的實時天氣。
index.php
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?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,風(fēng)向:$cityWd";
?>
您可能感興趣的文章:
相關(guān)文章
asp.net+Ajax 文本文件靜態(tài)分頁實現(xiàn)代碼
代碼還是很淺顯易懂的,呵呵因為我也不會寫高深的代碼。重要的我們都要動手去實踐。菜鳥們多多努力哦。做完我這個例子級會收獲很多哦。2010-05-05
利用AJAX實現(xiàn)鼠標(biāo)懸浮獲取值的代碼
ajax實現(xiàn)將鼠標(biāo)放到圖標(biāo)上,下方會顯示和該圖有關(guān)的信息2009-12-12
滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
滾動下拉到頁面底部加載數(shù)據(jù)是很多瀑布流網(wǎng)站的做法,那來看看配合jsonp是如何實現(xiàn)的吧,小菜總結(jié)記錄之用特在此與大家一起分享,感興趣的朋友可以參考下哈2013-05-05
Ajax動態(tài)為下拉列表添加數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了Ajax動態(tài)為下拉列表添加數(shù)據(jù)的實現(xiàn)方法,需要的朋友可以參考下2017-01-01

