請問擷取氣象預報問題

edited 七月 2014 in PHP新手區
這個函式會回傳一周天氣陣列
包含了
date:日期
temperature:溫度
title:天氣狀態
img:天氣圖檔

需要注意的一件事情是...這裡面不包含今天的天氣。所以是由明天開始的七天資料


應該要如何修改以顯示今天的氣象預報呢?

謝謝您

<?php

header("Content-Type: text/html; charset=utf-8");
function getWeather($city){

	$toURL = "http://www.cwb.gov.tw/V7/forecast/taiwan/inc/city/$city.htm";
	$post = array();
	$ch = curl_init();
	$options = array(
		CURLOPT_REFERER=>'',
		CURLOPT_URL=>$toURL,
		CURLOPT_VERBOSE=>0,
		CURLOPT_RETURNTRANSFER=>true,
		CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
		CURLOPT_POST=>true,
		CURLOPT_POSTFIELDS=>http_build_query($post),
	);
	curl_setopt_array($ch, $options);

	$result = curl_exec($ch); 
	curl_close($ch);
	//連接中央氣象局
	echo '<pre>';
	preg_match_all('/<table class="FcstBoxTable01" [^>]*[^>]*>(.*)<\/div>/si',$result, $matches, PREG_SET_ORDER);

	preg_match_all('/<td nowrap="nowrap" [^>]*[^>]*>(.*)<\/td>/si',$matches[0][1], $m1, PREG_SET_ORDER);

	$m2 = explode('</td>',$m1[0][1]);
	// print_r($m2);//取得每日資料m2[0~6]
	
	$weather = array();
	for($i=0;$i<=6;$i++){

		preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i], $m5, PREG_SET_ORDER);//取得天氣圖檔
		$m6 = explode('"',$m5[0][0]);
		$wi='http://www.cwb.gov.tw/V7/'.trim($m6[1],'\.\./\.\./');
		$wtitle = $m6[3];
     print_r($wtitle);
		$weather[$i]['date'] = date("m-d", mktime(0, 0, 0, date("m"), date("d")+$i+1,date("Y")));
		$weather[$i]['temperature'] = trim(strip_tags($m2[$i]));
		$weather[$i]['title'] = $wtitle;
		$weather[$i]['img'] = $wi;
	}
	
	return($weather);
	
}

  $weather=getWeather("Taipei_City") ;   
  print_r($weather);
  

// header("Location:loc.php");
?>

評論

Sign In or Register to comment.