佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > PHP+AJAX無刷新實(shí)現(xiàn)返回天氣預(yù)報(bào)數(shù)據(jù)

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

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 阿里云 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 Linux服務(wù)器 科大訊飛語音識(shí)別系統(tǒng) 電子圍欄 服務(wù)器配置
用php來寫一個(gè)天氣預(yù)報(bào)的模塊

天氣數(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“是城市代碼。所以可以先列出每個(gè)城市的城市代碼,然后php程序接收到了城市代碼,再去組裝URL,在通過URL來顯示該城市的實(shí)時(shí)天氣。

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>--請(qǐng)選擇城市--/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";
?>
您可能感興趣的文章:
  • PHP調(diào)用API接口實(shí)現(xiàn)天氣查詢功能的示例
  • PHP微信開發(fā)之查詢城市天氣
  • php使用百度天氣接口示例
  • PHP實(shí)現(xiàn)采集中國天氣網(wǎng)未來7天天氣
  • php結(jié)合飛信 免費(fèi)天氣預(yù)報(bào)短信
  • PHP 獲取指定地區(qū)的天氣實(shí)例代碼
  • php實(shí)現(xiàn)的百度搜索某地天氣的小偷代碼
  • PHP Ajax JavaScript Json獲取天氣信息實(shí)現(xiàn)代碼
  • php采集自中央氣象臺(tái)范圍覆蓋全國的天氣預(yù)報(bào)代碼實(shí)例
  • php微信開發(fā)之百度天氣預(yù)報(bào)
  • PHP調(diào)用全國天氣預(yù)報(bào)數(shù)據(jù)接口查詢天氣示例

標(biāo)簽:廣元 萍鄉(xiāng) 棗莊 江蘇 衢州 大理 衡水 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP+AJAX無刷新實(shí)現(xiàn)返回天氣預(yù)報(bào)數(shù)據(jù)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    柳江县| 美姑县| 肇州县| 琼结县| 临泉县| 盖州市| 岢岚县| 库尔勒市| 高清| 嘉义市| 华池县| 鸡泽县| 怀安县| 新龙县| 信丰县| 阿拉尔市| 宝鸡市| 凤阳县| 贵阳市| 南和县| 尉氏县| 中西区| 昌图县| 临湘市| 山阴县| 安达市| 玉山县| 普格县| 永平县| 彩票| 文登市| 郴州市| 天镇县| 疏勒县| 彭山县| 仁化县| 乡宁县| 醴陵市| 留坝县| 长泰县| 海阳市|