欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

改进后的《自动显示天气预报》小程序(Linux和Windows环境都可以_PHP

程序员文章站 2022-04-20 11:06:42
...
这次改进,主要是增加了图象的获取,并提供了一个比较完整的获取远端文件的函数。具体代码如下:

$fcont=file("http://www.bjmb.gov.cn/today.asp");
$arr=array();
for($i=0;$i $tmp=$fcont[$i];
$tmp=ereg_replace("
"," ",$tmp);
if(!strstr(strtolower($tmp),"img")){
$tmp=trim(strip_tags($tmp));
} else {
$st=ereg_replace(">⌒ $stt=split("⌒",$st);
foreach($stt as $key => $value){
if(strstr($value,"img") ){
$tmp=$value;
if(strstr($tmp,"bt9.jpg") || strstr($tmp,"colline.gif")){
$tmp="";
}else{
$tmp=ereg_replace("\"","",$tmp);
$st=split("src=",$tmp);
$st1=split(" ",$st[1]);getpic("http://www.bjmb.gov.cn/$st1[0]","image");
if(count($st)>2){
$st2=split(" ",$st[2]);getpic("http://www.bjmb.gov.cn/$st1[0]","image");
}
$tmp=ereg_replace("src=","width=30 height=30 align=absmiddle src=",$tmp);
}
}
}
}
if(trim($tmp)!="" && trim($tmp)!=" ") $arr[]=$tmp;
}
$yb=array();
for($i=2;$i $yb[]=$arr[$i];
}
?>




function getpic($url,$dir){
$tmp=split("/",$url);
$fname=$tmp[count($tmp)-1];
if(strstr($dir,"\\")){
if(substr($dir,-2)=="\\") $file=$dir.$fname;else $file=$dir."\\".$fname;
}else{
if(substr($dir,"/")){
if(substr($dir,-1)=="/") $file=$dir.$fname;else $file=$dir."/".$fname;
}
else{
return ;
}
}
if(!(file_exists("$dir") && is_dir("$dir"))) mkdir($dir,0777);
if(file_exists($file)) return ;
$fpo=fopen($file,"wb");
if(!$fpo) return ;
$num=0;
do{
$num++;
$fpi=fopen($url,"r");
}while(!$fpi && $num if(!fpi) return ;
while(!feof($fpi)){
$cont=fread($fpi,128);
fwrite($fpo,$cont,128);
}
fclose($fpi);
fclose($fpo);
}
?>