All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
PHP实例:PHP天气查询_[PHP教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/2/17 7:31:15 Browse times: 493 Comment times: 0

PHP实例:PHP天气查询_[PHP教程]


Head photo

Go homepage
Upload pictures
Write articles

PHP实例:PHP天气查询_[PHP教程]

city_name=_GET['city'];

//用正则在city.txt中查找相应城市的天气城市代码
city_name=trim(ereg_replace("[ ]{1,}","",city_name));
time_file="city.txt";

city_all=file_get_contents(time_file);
preg="|city_name,(.*)\s|i";
preg_match_all(preg,city_all,cn, PREG_SET_ORDER);
city_n=trim(cn[0][1]);


if(city_n)
{
nowtime=date("Y-m-d");

file = "cache/city_name.txt";

//获取文件的最后修改日期
if(file_exists(file))
{
last_time=date("Y-m-d",filemtime(file));
}
else{
last_time=date("Y-m-d");
}
//新建保存匹配数组
records=null;


//如果文件存在,或还没过期,则直接读取缓存数据
if (file_exists(file) && last_time==nowtime)
{

records = unserialize(file_get_contents(file));

}
else
{

//以下就是如何在网站上 获取天气资料,,小偷部分!感觉其中的preg_match_all功能很强大

data=file_get_contents("http://weather.msn.com/local.aspx?wealocations=wc:city_n");
preg="|

Hi:<\/th> (.*)&#176;<\/td><\/tr>L<\/th> (.*)&#176;<\/td><\/tr><\/table>.*class=\"skytext\" colspan=\"3\">(.*)<\/td>|U";

preg_match_all(preg,data,records);
unset (records[0][0]);

//序列化组
OUTPUT = serialize(records);
//打开文件,
fp = fopen(file,"w");
//把数组写进文件
fputs(fp, OUTPUT);
//关闭
fclose(fp);

}

image=records[1][0];
resulta=records[2][0];
resultb=records[3][0];
resultc=records[4][0];
reString="

city_name
nowtime



".resulta."-".resultb."(F)


".resultc."

";
}
else
{
reString="

The city: city_name hasn`t been found!

";
}
echo iconv("gb2312","utf-8",reString);

?>

主要是把当天的做个缓存,这样第2次访问同一城市的时候的话就比较快了。虽然不是什么好代码,但是对与刚刚学习PHP的人还是一个蛮好的例子,特别是里面的正则表达式的应用。

前编主要是处理 部分,也就是天气查询的核心部分,现在是AJAX中的部分 代码如下:

var xmlHttp;
function createXMLHttpRequest()
{
var xmlHttpRequest;
if(window.ActiveXObject)
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttpRequest;

}
else if(window.XMLHttpRequest)
{
xmlHttpRequest=new XMLHttpRequest();
return xmlHttpRequest;
}
}


function startXmlHttpRequest()
{
var form=document.getElementById("city");
var city=form.value;
var cityName=form.Text;

xmlHttp=createXMLHttpRequest();
xmlHttp.open("GET","weather/getHtml.php?city="+city+"&cityname="+cityName+"&time="+new Date(),true);
xmlHttp.onreadystatechange=returnText;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(null);


}
function startTextXmlHttpRequest()
{
var form=document.getElementById("textcity");
var city=form.value;
xmlHttp=createXMLHttpRequest();
xmlHttp.open("GET","weather/textGetHtml.php?city="+city,true);
xmlHttp.onreadystatechange=returnText;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(null);


}

function validataIpAdd()
{

}
function returnText()
{
var div=document.getElementById("result_box");
var loading=document.getElementById("loading");
div.innerHTML="";
loading.style.display="block";
if(xmlHttp.readyState==1)
{

}
else if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{

div.innerHTML=xmlHttp.responseText;
loading.style.display="none";
}
}
}

主要比较简单! 由4个函数组成 1个是创建 1个是提交 一个是返回 顶部的是是判断浏览器然后创建XMLHTMLREQUES

来源:CSDN





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.