程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP入門知識 >> PHP實例教程:天氣預報小偷PHP代碼

PHP實例教程:天氣預報小偷PHP代碼

編輯:PHP入門知識

寫得一個php天氣小偷程

<?php
/***
天氣預報
***/
function readAll($location)
{
 $MAX_RETRY_NUM = 3;
 if(preg_match("/(http:\/\/)?([^\/|\:]+)(:\d{1,5})?(\/.+)?/", $location, $url))
 {
  list(,,$host,$port,$path) = $url;
  $port = preg_replace("/:/", "", $port);
  $port = strlen($port) ? $port : 80;
  $path = strlen($path) ? $path : "/";
  $request  = "GET $path HTTP/1.0\r\n";
  $request .= "Host: $host\r\n";
  $request .= "Accept: */*\r\n";
  $request .= "Connection: close\r\n";
  $request .= "User-Agent: superspice's BOT\r\n\r\n";
  $retry = 0;
  $do = false;
  do
  {
   if(@!$socket =fsockopen($host, $port, &$errMsg, &$errCode ))
   {
    return false;
   }
   fwrite($socket, $request);
   $flag = true;
   $header = "";
   $body = "";
   while(!feof($socket))
   {
    $line = fgets($socket, 128);
    if($flag)
    {
     if(trim(chop($line)) == "")
     {
      $flag = false;
     }
     else
     {
      $header .= $line;
     }
    }
    else
    {
     $body .= $line;
    }
   }
   if(preg_match("/Content-Length: (\d+)/", $header, $out))
   {
    $header_length = $out[1];
    if($header_length != strlen($body))
    {
     $do = true;
    }
    else
    {
     $do = false;
    }
   }
   if(is_resource($socket))
   {
    fclose($socket);
   }
   $retry ++;
  }while($do && $retry<$MAX_RETRY_NUM);
 }
 return $body;
}
function get_weather()
{
 $TqUrl="http://www.ttqx.com.cn/forecast/area_weather.asp?city_name=合肥&zhou=yz";
 $VaX1Tem=readAll($TqUrl);
 if(!$VaX1Tem)
 {
  return false;
 }
 settype($ArrayTq,"array");
 $ArrayTq=explode("<!--天氣預報開始-->",$VaX1Tem);
 $ArrayTq=explode("<!--天氣預報結束-->",$ArrayTq[1]);
 $flash = explode("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"",$ArrayTq[0]);
 $flash = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"".$flash[1];
 $flash = explode("</EMBED></object>",$flash);
 $flash = $flash[0]."</EMBED></object>";
 $pattern = array("/images\/weather/i","/WIDTH=\"65\" HEIGHT=\"70\"/i");
 $replace = array("http://www.ttqx.com.cn/forecast/images/weather","WIDTH=\"51\" HEIGHT=\"55\"");
 $flash = preg_replace($pattern,$replace,$flash);
 //天氣
 $ArrayTqTem2=explode('<tr><td>',$ArrayTq[0]);
 $ArrayTqTem2=explode('</td></tr>',$ArrayTqTem2[1]);
 //$ArrayTqTem2=explode('<td>',$ArrayTqTem2[0]);
 $ArrayTqTem2=$ArrayTqTem2[0];
 //風力
 $ArrayTqTem3=explode('<tr><td>',$ArrayTq[0]);
 $ArrayTqTem3=explode('</td></tr>',$ArrayTqTem3[2]);
 //$ArrayTqTem3=explode('<td>',$ArrayTqTem3[0]);
 $ArrayTqTem3=$ArrayTqTem3[0];
 //溫度
 $ArrayTqTem4=explode('<tr><td>',$ArrayTq[0]);
 $ArrayTqTem4=explode('</td></tr>',$ArrayTqTem4[3]);
 //$ArrayTqTem4=explode('<td>',$ArrayTqTem4[0]);
 $ArrayTqTem4=$ArrayTqTem4[0];
 if( !$ArrayTqTem2 or !$ArrayTqTem3 or !$ArrayTqTem4)
 {
  return false;
 }
 //$TqVar=$ArrayTqTem2.",".$ArrayTqTem3."<br>氣溫:".$ArrayTqTem4;
 $weather[0]=$flash;
 $weather[1]=$ArrayTqTem2;
 $weather[2]=$ArrayTqTem3;
 $weather[3]=$ArrayTqTem4;
 return $weather;
}
$weather = get_weather();
?>

上面寫的是php文件,你沒有echo當然不能正常顯示啦

<?php
error_reporting(0);
include_once ("../include/weather.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>天氣預報</title>
<style>
html,body {margin:0;padding:0; font-size:12px;}
div {width:178px; height:58px;position:relativ;position:relative}
img {width:178px; height:58px; z-index:1;position:absolute; border:0}
object { margin-top:2px; margin-left:8px;}
span{position:absolute;top:2px;right:12px;}
li{font-family:Verdana,宋體;list-style:none;margin-top:3px; letter-spacing:1px;}
</style>
</head>
<body>
<div>
<a href="http://www.ttqx.com.cn/tqyb/" target="_blank"><img src="http://links.ezz.cc/blank.gif" /></a><?php echo $weather[0]; ?><span><li><?php echo $weather[1]; ?></li> <li><?php echo $weather[2]; ?></li> <li><?php echo $weather[3]; ?></li></span>
</div>
</body>
</html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved