webptr 发表于 2007-10-5 14:43:44

PHP天气预报小偷07年版


<?
/************************************************************************
名称:天气预报小偷
功能:每天读取新浪网上的天气情况并保存到本地,然后再从本地读取显示
更新日期:2007年1月8日
*************************************************************************/
/*******************看weather.txt的修改时间是否为当天,如果是昨天的就要上网抓结果***********/
   $tmp = filemtime("weather.txt");
    $tt1=date("Ymd",$tmp)."<br>";
    $tt2=date("Ymd");
    if ($tt1<$tt2) {
    //echo "要抓结果";
    unlink("weather.txt");
    $lines = file ('http://php.weather.sina.com.cn/search.php?city=广州');
// 在数组中循环,显示 html 的源文件并加上行号。

foreach ($lines as $line_num => $line) {   
    if ($line_num==247||$line_num==270){
    //echo "Line #<b>{$line_num}</b> :$line<br>n";
   $a1=strip_tags($line);//处理多余代码
      $handle = fopen ("weather.txt", "a");
         fwrite($handle, $a1);
         fclose($handle);
   }
   }
   echo "<meta http-equiv="refresh" content="1">";

} else {
    // echo "直接从weather.txt读结果";

    $filename="weather.txt";
    @$handle=fopen($filename,"r");

    if ($handle) {   
      $contents=fread($handle,filesize($filename));
      fclose($handle);
      $cc=explode("n",$contents);
      $today=explode(" ",$cc);
      $tomo=explode(" ",$cc);
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<table width="252" 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">
      <div align="center">未来48小时天气预报</div></td>
</tr>
<tr bgcolor="#0066FF">
    <td width="54" height="21">
      <div align="center"><font color="#FFFFFF">时间</font></div></td>
    <td width="88">
      <div align="center"><font color="#FFFFFF">天气</font></div></td>
    <td width="90">
      <div align="center"><font color="#FFFFFF">温度</font></div></td>
</tr>
<tr >
    <td height="19" >
      <div align="center"> 今天</div></td>
    <td>
   
      <div align="center"><?=$today?></div></td>
    <td>
   
      <div align="center"><?=$today?></div></td>
</tr>
<tr>
    <td height="18">
      <div align="center">明天</div></td>
    <td>
   
      <div align="center"><?=$tomo?></div></td>
    <td>
   
      <div align="center"><?=$tomo?></div></td>
</tr>
</table>
<?
   
      } else {
            echo "无法读取所需文件!!n";
      }
    }
?>
页: [1]
查看完整版本: PHP天气预报小偷07年版

网站推广