|
<?php /************************************************************************ 名称:天气预报小偷 功能:每天读取新浪网上的天气情况并保存到本地,然后再从本地读取显示 开发:n3tl04d QQ:10492315 1妹儿:n3tl04d[at]gmail.com [url]http://smboy.bokee.com[/url] 更新日期:2007年11月22日 *************************************************************************/ @header("Content-Type:text/html;charset=gb2312"); error_reporting(0);
/*******************看weather.txt的修改时间是否为当天,如果是昨天的就要上网抓结果***********/ $tmp = filemtime("weather.txt"); $tt1=date("Ymd",$tmp)."<br>"; $tt2=date("Ymd"); if ($tt1<$tt2) { //echo "要抓结果"; unlink("weather.txt"); $url = "http://php.weather.sina.com.cn/search.php?city=广州" ; bitsCN.nET*中国网管博客
$content = file_get_contents($url); preg_match_all('/<div class="Weather_TP">(.+?)<\/DIV>/si',$content,$result); for($i=0;$i<2;$i++){ $x= strip_tags($result[1][$i])."\n"; $handle = fopen ("weather.txt", "a"); fwrite($handle, $x); fclose($handle);
} listweather(); } else { // echo "直接从weather.txt读结果"; listweather(); }
//天气显示函数 function listweather() { $filename="weather.txt"; www_bitscn_com中国.网管联盟
$handle=fopen($filename,"r");
if ($handle) { $contents=fread($handle,filesize($filename)); fclose($handle); $cc=explode("\n",$contents); $today=explode(" ",$cc[0]); $tomo=explode(" ",$cc[1]); ?> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <table width="200" height="85" cellspacing="0" bordercolordark="#FFFFFF" bordercolorlight="#000000" border="1" align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td height="22" colspan="3"> bitsCN#com中国网管联盟
<div align="center">未来48小时天气预报</div></td> </tr> <!--<tr bgcolor="#0066FF"> --> <tr > <td width="54" height="21"> <div align="center"><font style="font-size=14px" color="#000000">时间</font></div></td> <td width="88"> <div align="center"><font style="font-size=14px" color="#000000">天气情况</font></div></td> <td width="90"> <div align="center"><font style="font-size=14px" color="#000000">温度℃</font></div></td> </tr> <tr > www@bitscn@com <td height="19" > <div align="center"> <font style="font-size=14px">今天</font></div></td> <td> <div align="center"><font style="font-size=14px"><?php echo $today[0]?></font></div></td> <td> <div align="center"><font style="font-size=14px"><?php echo ereg_replace("℃","",$today[1])?>℃</font></div></td> </tr> <tr> <td height="18"> <div align="center"><font style="font-size=14px">明天</font></div></td> bitsCN.nET*中国网管博客 <td> <div align="center"><font style="font-size=14px"><?php echo $tomo[0]?></font></div></td> <td> <div align="center"><font style="font-size=14px"><?php echo ereg_replace("℃","",$tomo[1])?>℃</font></div></td> </tr> </table> <?php } else { echo "无法读取天气记录!!\n"; } } ?>
本文来自: 中国网管联盟(bitsCN.com) 详细出处参考:http://www.bitscn.com/php/program/200711/120314.html |