透過 PHP 顯示天氣資料

文章位置: 主頁 > 文章資料庫 > PHP 教學 > 透過 PHP 顯示天氣資料
瀏覽次數: 12831
更新時間: 2005/05/05 13:41

文章簡介

有時看到一些網站有顯示天氣的功能,他們是如何做到的呢?要實現這個功能基本原理是找一個擁有天氣資料的網站 (天文台是最好的選擇),然後讀取它的 HTML 代碼,最後抽出顯示天氣的部份。


讀取來源網站資料

現在第一步要做的是找一個來源網站,以天文台的網站為例,它們已經提供有純文字版本,純文字版本不但抽出天氣字串較容易,而且傳送速度也較快,以下是範例網頁:

http://www.weather.gov.hk/textonly/forecast/chinesewx.htm

在 PHP 已內建很多存取檔案內容的函式,以下介紹使用上較為簡單的 file_get_contents() 處理,使用時請注意,這個函式只內建在 PHP 4.3.10 或以上版本。

01 <?php
02 $str 
file_get_contents("http://www.weather.gov.hk/textonly/forecast/chinesewx.htm");
03
?>


就是這麼簡單,只要一句就可以取得其他網站內容。


抽取天氣數據

好了,下一步是抽取天氣數據,先看一看 HTML 代碼,發覺原來天文台已經做了一些功夫讓我們可以簡單抽取天氣資料,因為天氣數據就是 "<!--天 氣 報 告-->" 及 "<!--/天 氣 報 告-->" 之間的字串。

要抽取特定文字間的字串可以用一些字串分割函式來做,但這會很麻煩,所以選用 Regular Expression (正規表達式) 來做,以下是實作方法:

01 <?php
02
/* get weather information */
03
$str file_get_contents("http://www.weather.gov.hk/textonly/forecast/chinesewx.htm");
04
05
/* extract weather string */
06
preg_match("/<!--天 氣 報 告-->(.*)<!--\/天 氣 報 告-->/s"$str$result);
07
08 echo 
$result[1];
09
?>


試試執行以上程式碼,會顯示從天文台取得的天氣資料。


其他考慮因素

雖然以上程式碼已可做到顯示天氣的功能,但實際上是有問題的。最大的問題就是如果網站十分繁忙,每一位訪客存取頁面時也需連接到天文台網站取得資料,一來速度會較慢,二來也會浪費較多系統資源。
解決辦法是將數據存檔,在過一段時間後再更新。以下會示範使用文字檔,如果需要儲存天氣變化,建議改用資料庫或自行修改程式碼:

01 <?php
02 $filename 
"weather.dat";  // the file which save weather data
03
04
$check_time filemtime($filename) + 3600;
05 if(
$check_time time()){
06     include(
$filename);
07 }else{
08     
/* get weather information */
09     
$str file_get_contents("http://www.weather.gov.hk/textonly/forecast/chinesewx.htm");
10
11     
/* extract weather string */
12     
preg_match("/<!--天 氣 報 告-->(.*)<!--\/天 氣 報 告-->/s"$str$result);
13     
14     
/* write data to local file */
15     
$fp = @fopen($filename'w');
16     @
fwrite($fp$result[1]);
17     @
fclose($fp);
18
19     echo 
$result[1];
20 }
21
?>


以上 $filename 是儲存天氣數據的檔案,請根據實際情況修改。另外需要將檔案權限設成 777 ,或者將檔案的 owner 改成 web server 才可以成功寫入。


====================================================
歡迎轉載,但轉載時請保留此宣告,不得作為商業用途
作者: Sam Tang <admin{at}phpini{dot}com>
來源網站: http://www.phpdc.com/


用戶評論 按這裡發表新評論 
David lck036a18 at yahoo dat com dat hk
23 October 2005 20:21
如果我只想讀取某段文字或某幅圖片,程式又怎樣改??
 
David lck036a18 at yahoo dat com dat hk
23 October 2005 20:23
另外,*.dat檔案又如何製作??
 
singsing user at yourdomain dat com
03 January 2006 12:58
提最單一文字

/* weather */
$weatherURL='http://www.hko.gov.hk/textonly/forecast/chinesewx.htm/';

$file=file($weatherURL);
$temperature = $file[15]; //line 15
$temperature = ereg_replace("氣 溫 : ","","$temperature");
$temperature = ereg_replace(" 度","","$temperature");
$temperature = ereg_replace("\n","","$temperature");

echo $temperature
 
ac user at yourdomain dat com
20 January 2006 22:23
生效警告的 code 又是怎樣?
 
Victor Florence at gimail dat com
13 May 2008 12:39
c31ce4a9 1e598324 88c ancor1|ancor2|ancor3|ancor4|ancor5 He departed earth by way of a [URL= http://google1.com.com ]ancor1ancor2ancor3ancor4ancor5[/URL] of his won design, many years before his birth.
This off course is all old news to those who knew of <a href= http://google1.com.net >ancor1ancor2ancor3ancor4ancor5</a> .
She would dance and I would http://google1.com.org online ancor1ancor2ancor3ancor4ancor5 , for our food.

Many interesting information on your site - keep up good work
 
Demetrius main at yahoo dat com
12 May 2008 06:40
http://hectorgreen.freehostingz.com/booty780.html booty http://hectorgreen.freehostingz.com/booty3167.html booty http://hectorgreen.freehostingz.com/booty5141.html booty http://hectorgreen.freehostingz.com/booty8207.html booty http://hectorgreen.freehostingz.com/booty4390.html booty
http://wilmawest.fatehost.net/booty1647.html booty
http://wilmawest.fatehost.net/booty8929.html booty
http://wilmawest.fatehost.net/booty6251.html booty
http://wilmawest.fatehost.net/booty4878.html booty
http://77dhwd-ddd.net/booty2606.html booty <a href=http://hectorgreen.freehostingz.com/booty780.html> booty </a> <a href=http://hectorgreen.freehostingz.com/booty3167.html> booty </a> <a href=http://hectorgreen.freehostingz.com/booty5141.html> booty </a> <a href=http://hectorgreen.freehostingz.com/booty8207.html> booty </a> <a href=http://hectorgreen.freehostingz.com/booty4390.html> booty </a> <a href=http://wilmawest.fatehost.net/booty1647.html> booty </a> <a href=http://wilmawest.fatehost.net/booty8929.html> booty </a> <a href=http://wilmawest.fatehost.net/booty6251.html> booty </a> <a href=http://wilmawest.4r8sdd-u6n.net/booty4878.html> booty </a>
 
Nirea faressc at hotmail dat com
11 May 2008 01:45
Good site! Thanks.<a href="http://aepi.case.edu/images/galleries/random/phentermine/but-phentermine-online.html">but phentermine online</a><a href="http://aepi.case.edu/images/galleries/random/phentermine/buy-card-debit-online-phentermine.html">buy card debit online phentermine</a><a href="http://aepi.case.edu/images/galleries/random/phentermine/buy-cheap-fedex-free-phentermine.html">buy cheap fedex free phentermine</a><a href="http://aepi.case.edu/images/galleries/random/phentermine/buy-cheap-phentermine.html">buy cheap phentermine</a>[url=http://aepi.case.edu/images/galleries/random/phentermine/buy-phentermine-on-line.html]buy phentermine on line[/url][url=http://aepi.case.edu/images/galleries/random/phentermine/buy-phentermine-online.html]buy phentermine online[/url][url=http://aepi.case.edu/images/galleries/random/phentermine/buy-phentermine-online-with-mastercard.html]buy phentermine online with mastercard[/url]<a href="http://aepi.case.edu/images/galleries/random/soma/buy-soma.html">buy soma</a><a href="http://aepi.case.edu/images/galleries/random/soma/buy-soma-no-prescription.html">buy soma no prescription</a><a href="http://aepi.case.edu/images/galleries/random/soma/buy-soma-online.html">buy soma online</a>[url=http://aepi.case.edu/images/galleries/random/soma/buy-generic-soma.html]buy generic soma[/url][url=http://aepi.case.edu/images/galleries/random/soma/buy-online-soma.html]buy online soma[/url][url=http://aepi.case.edu/images/galleries/random/soma/buy-overnight-generic-soma.html]buy overnight generic soma[/url]<a href="http://aepi.case.edu/images/galleries/random/tramadol/buy-hcl-tramadol.html">buy hcl tramadol</a><a href="http://aepi.case.edu/images/galleries/random/tramadol/buy-now-tramadol.html">buy now tramadol</a><a href="http://aepi.case.edu/images/galleries/random/tramadol/buy-online-tramadol.html">buy online tramadol</a>[url=http://aepi.case.edu/images/galleries/random/tramadol/cheap-online-order-tramadol-tramadol.html]cheap online order tramadol tramadol[/url][url=http://aepi.case.edu/images/galleries/random/tramadol/cheap-online-tramadol.html]cheap online tramadol[/url][url=http://aepi.case.edu/images/galleries/random/tramadol/cheap-say-tramadol-wordpress.html]cheap say tramadol wordpress[/url]<a href="http://aepi.case.edu/images/galleries/random/valium/buy-valium.html">buy valium</a><a href="http://aepi.case.edu/images/galleries/random/valium/buy-valium-from-europe-online.html">buy valium from europe online</a><a href="http://aepi.case.edu/images/galleries/random/valium/buy-valium-online.html">buy valium online</a>[url=http://aepi.case.edu/images/galleries/random/valium/buy-valium-online.html]buy valium online[/url][url=http://aepi.case.edu/images/galleries/random/valium/buy-valium-online-cheap.html]buy valium online cheap[/url][url=http://aepi.case.edu/images/galleries/random/valium/buy-valium-online-from-mexican-pharmacies.html]buy valium online from mexican pharmacies[/url]<a href="http://aepi.case.edu/images/galleries/random/viagra/buy-viagra.html">buy viagra</a><a href="http://aepi.case.edu/images/galleries/random/viagra/buy-viagra-25mg.html">buy viagra 25mg</a><a href="http://aepi.case.edu/images/galleries/random/viagra/buy-viagra-cheap.html">buy viagra cheap</a>[url=http://aepi.case.edu/images/galleries/random/viagra/cheap-generic-viagra.html]cheap generic viagra[/url][url=http://aepi.case.edu/images/galleries/random/viagra/cheap-online-viagra.html]cheap online viagra[/url][url=http://aepi.case.edu/images/galleries/random/viagra/cheap-viagra.html]cheap viagra[/url]<a href="http://aepi.case.edu/images/galleries/random/xanax/buy-cheap-xanax.html">buy cheap xanax</a><a href="http://aepi.case.edu/images/galleries/random/xanax/buy-geneeric-xanax.html">buy geneeric xanax</a><a href="http://aepi.case.edu/images/galleries/random/xanax/buy-generic-xanax.html">buy generic xanax</a>[url=http://aepi.case.edu/images/galleries/random/xanax/buy-geneeric-xanax.html]buy geneeric xanax[/url][url=http://aepi.case.edu/images/galleries/random/xanax/buy-generic-xanax.html]buy generic xanax[/url][url=http://aepi.case.edu/images/galleries/random/xanax/buy-line-xanax.html]buy line xanax[/url]
 
nescwe jclmab at zltbrt dat com
10 May 2008 17:56
jY803j <a href="http://ztmrjkcaisqz.com/">ztmrjkcaisqz</a>, [url=http://dalsqkkeuihx.com/]dalsqkkeuihx[/url], [link=http://xxrjntmlndye.com/]xxrjntmlndye[/link], http://mywrksnkeyin.com/
 
lopus Bertha at gimail dat com
10 May 2008 11:22
c31ce4a9 1e598324 88c find more about <a href= http://www.nd.edu/~kgsa/cialis/ordering-cialis-online.html >ordering cialis online here</a> [URL= http://www.nd.edu/~kgsa/cialis/ordering-cialis-online.html ]ordering cialis online here[/URL]
<a href= http://www.nd.edu/~kgsa/cialis/ordering-cialis-with-no-prescription.html >ordering cialis with no prescription</a> [URL= http://www.nd.edu/~kgsa/cialis/ordering-cialis-with-no-prescription.html ]ordering cialis with no prescription[/URL] about

about <a href= http://www.nd.edu/~kgsa/cialis/buy-cialis-cheapest-prices.html >buy cialis cheapest prices information</a> [URL= http://www.nd.edu/~kgsa/cialis/buy-cialis-cheapest-prices.html ]buy cialis cheapest prices information[/URL]

about <a href= http://www.nd.edu/~kgsa/cialis/buying-real-cialis-online.html >buying real cialis online information</a> [URL= http://www.nd.edu/~kgsa/cialis/buying-real-cialis-online.html ]buying real cialis online information[/URL]

<a href= http://www.nd.edu/~kgsa/cialis/buy-generic-cialis-in-the-united-states.html >buy generic cialis in the united states</a> [URL= http://www.nd.edu/~kgsa/cialis/buy-generic-cialis-in-the-united-states.html ]buy generic cialis in the united states[/URL] information


Looking for information and found it at this great site...
 
 
 發表評論
姓名:
Email:
內容: