国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

PHP+AJAX無刷新實現(xiàn)返回天氣預報數(shù)據

2024-09-01 08:32:10
字體:
來源:轉載
供稿:網友
天氣數(shù)據是通過采集中國氣象網站的。本來中國天氣網站也給出了數(shù)據的API接口,接下來為大家介紹下用php來寫一個天氣預報的模塊,感興趣的朋友可以參考下

用php來寫一個天氣預報的模塊

天氣數(shù)據是通過采集中國氣象網站的。本來中國天氣網站也給出了數(shù)據的API接口。以下是API的地址。返回的數(shù)據格式為json格式。

1.
2.
3.

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 onchange="getCityId();">
<option>--請選擇城市--</option>
<option value="101010100">北京</option>
<option value="101020100">上海</option>
<option value="101030100">天津</option>
<option value="101040100">重慶</option>
<option value="101280101">廣州</option>
</select>
<span></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";
?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 岐山县| 永安市| 延吉市| 桐乡市| 九江市| 花莲市| 锡林浩特市| 友谊县| 安国市| 张家港市| 西林县| 临潭县| 固阳县| 阳谷县| 沙坪坝区| 建宁县| 沿河| 神池县| 原阳县| 泸西县| 澄迈县| 闸北区| 疏附县| 南康市| 股票| 周至县| 玉山县| 三原县| 和顺县| 庐江县| 贵阳市| 景洪市| 桂平市| 兴安盟| 将乐县| 白玉县| 抚远县| 高安市| 光泽县| 惠水县| 海原县|