<?php
$wind="";
while($wind=="") {
 $contentarray=file("http://tq.tom.com/city/18.html");  $content=join(" ",$contentarray);
  $hefeipos=strpos($content,'风向风力');
  $content=substr($content,$hefeipos);
 $bgcolorpos=strpos($content,'气象指数预报');
 $content=substr($content,0,$bgcolorpos);
 $weatherarray=explode('</td><td >',$content);
 $lowest_degree =$weatherarray[1];
 $highest_degree =$weatherarray[2];
$postianqi=strpos($weatherarray[0],'</td><td>');
 $news_weather=substr($weatherarray[0],$postianqi); 
 $news_weather=substr($news_weather,9); 
 $wind=$weatherarray[3]; 
 $windpos=strpos($wind,'<');
 $wind=substr($wind,0, $windpos);
}
//echo $news_weather,$lowest_degree,$highest_degree,$wind; insetintodb($news_weather,$lowest_degree,$highest_degree,$wind);
 function insetintodb($weather,$low,$high,$wind){
$lowarray=explode('℃',$low);$low=$lowarray[0];
$higharray=explode('℃',$high);$high=$higharray[0];
 echo $weather,$low,$high;
$today=date('Y-m-d');echo '<br>'.$today;
$conn=mysql_connect("localhost","news","5c539bc3") or  die("连接数据库失败");
mysql_select_db("hfutnews",$conn) or die("选择数据库失败");
$sql="insert into news_weather values ('".$today."','".$low."','".$high."','".$weather."','".$wind."','','".$wind."')";
if(!$result=mysql_query($sql)){
echo "执行插入错误";exit();
}} ?>
这个是从tom网上获取的天气信息,但是现在这个不更新了,所以想换个网站,但是用file获取信息都是提示HTTP request failed或者Fatal error: Maximum execution time of 30 seconds exceeded in D:\AppServ\www\weather.php on line 4
,高手指导下!可以从这个地址获取http://www.cma.gov.cn/tqyb/v2/cityweather.php?id=58321天气情况。不胜感激,880分奉上!

解决方案 »

  1.   

    file_get_contents的方法比较稳当,毕竟view-source:什么的已经没用了。
    set_time_limit(充足的时间)就可以避免“Maximum execution time ”的出现,
    然后用正则表达式截取出天气的信息。
    应该还有更好的方法,一个等回答吧……
    <? 
    set_time_limit (500);
     $url= 'http://www.cma.gov.cn/tqyb/v2/cityweather.php?id=58321';
     $source =file_get_contents($url);
    ?>
    <?
    $weather_s = strpos ($source,"温度:");
    $weather_e = strpos ($source,"℃");
    $html = substr ($source,$weather_s,$weather_e);
    echo $html;
    ?> 
      

  2.   

    源码全是table,用正则效率可能也不是很高.
    strpos定位的确是个好办法.
    file是将所有的内容读进数组,不是好的办法,用file_get_contents代替,如楼上所说.
    超时用set_time_limit(0);不限时间.