// Get Weather Data From Web private function getData() { $fh = fopen($this->url, 'r') or die("Error on Connect"); $this->rawData = stream_get_contents($fh); fclose($fh); }
// Parse Weather Data private function parseData() {
if(preg_match("/DURING THE PAST HOUR THE MEAN UV INDEX RECORDED AT KING'S PARK : (.*)/i", $tempString, $matches) == 1) { $this->_uvIndex = $matches[1]; } }
eddie 2008-08-28 21:10
Thank you
Alex2008-10-23 08:15
Code Here. 我係用 PHP 寫. 用 Linux Server 行 Cron Jobs To Check. 如果你要用. 用改 Username 同 Password. 同埋 Call GoogleCalendar.exe 個 Path. 有個 cyclone.txt 去 Store 最後 Check 的台風 Signal No.
#!/usr/bin/php
<?php
$w = new weather();
class weather
{
private $url = "http://www.weather.gov.hk/textonly/forecast/englishwx.htm";
private $urlw = "http://www.hko.gov.hk/textonly/warning/warn.htm";
private $file = "/usr/local/weather/cyclone.txt";
private $rawData = "";
private $googleUsername = "YOURUSERNAME";
private $googlePassword = "YOURPASSWORD";
private $_temperature = "ERROR";
private $_humidity = "ERROR";
private $_weatherCartoon = "50";
private $_cyclone = "";
private $_uvIndex = 0;
private $_lastUpdate = "ERROR";
public function __construct()
{
$this->getData();
$this->parseData();
//$this->generateImage();
//$this->sendSMS();
$this->checkChange();
}
public function checkChange()
{
$cyclone = file_get_contents($this->file);
if ($cyclone != $this->_cyclone)
{
$this->sendSMS2();
}
file_put_contents($this->file, $this->_cyclone);
}
public function sendSMS2()
{
if ($this->_cyclone != "")
{
$cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . " " . $this->googlePassword . " default \"Typhoon No.: " . $this->_cyclone . "\" \"Tropical Cyclone Signal No.: " . $this->_cyclone . ", Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" HK";
}
else
{
$cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . " " . $this->googlePassword . " default \"Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" Dummy HK";
}
system($cmd);
}
public function sendSMS()
{
echo "SEND SMS";
$cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . " " . $this->googlePassword . " default \"Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" Dummy HK";
system($cmd);
}
public function getXML()
{
}
// Get Weather Data From Web
private function getData()
{
$fh = fopen($this->url, 'r') or die("Error on Connect");
$this->rawData = stream_get_contents($fh);
fclose($fh);
}
// Parse Weather Data
private function parseData()
{
$start = strpos($this->rawData, "<pre>");
$end = strpos($this->rawData, "</pre>");
if(preg_match('/Bulletin updated at (.*)\</i', $this->rawData, $matches) == 1)
{
$this->_lastUpdate = $matches[1];
}
$tempString = substr($this->rawData, $start + 5, $end - $start - 5);
//echo $tempString;
if(preg_match('/AIR TEMPERATURE : (.*) DEGREES CELSIUS/i', $tempString, $matches) == 1)
{
$this->_temperature = $matches[1];
}
if(preg_match('/RELATIVE HUMIDITY : (.*) PER CENT/i', $tempString, $matches)== 1)
{
$this->_humidity = $matches[1];
}
if(preg_match('/WEATHER CARTOON : NO.(.*) - /i', $tempString, $matches) == 1)
{
$this->_weatherCartoon = $matches[1];
}
if(preg_match('/THE TROPICAL CYCLONE SIGNAL NO. (\d)/i', $tempString, $matches) == 1)
{
$this->_cyclone = $matches[1];
if ($this->_cyclone == "8")
{
$this->getWarn();
}
}
if(preg_match("/DURING THE PAST HOUR THE MEAN UV INDEX RECORDED AT KING'S PARK : (.*)/i", $tempString, $matches) == 1)
{
$this->_uvIndex = $matches[1];
}
}
private function getWarn()
{
$fh = fopen($this->urlw, 'r');
$warn = stream_get_contents($fh);
fclose($fh);
if(preg_match('/NO. 8 (.*) GALE/i', $warn, $matches) == 1)
{
switch ($matches[1])
{
case "SOUTHWEST":
$this->_cyclone = "8sw";
break;
case "NORTHWEST":
$this->_cyclone = "8nw";
break;
case "SOUTHEAST":
$this->_cyclone = "8se";
break;
case "NORTHEAST":
$this->_cyclone = "8ne";
break;
}
}
}
}
?>