php

[php] accuweather抓天氣

一般手機或是抓氣象的資料,通常很常會發現大家都是到accuweather 這個網站去撈資料的,她有非常詳細的資料可以抓
http://www.accuweather.com/zh/tw/taichung/315040/daily-weather-forecast/315040?day=1

而他有提供api可以讓人家去抓他的rss資料 網址是http://realtek.accu-weather.com/widget/realtek/weather-data.asp?location=cityId:315040

而這邊的cityId:315040 就是代表台中市 各地區有各地區的city id 請改成你要的地區
之後就是下面這支程式碼啦。 我其實寫得有點糟糕,不過他是可以work的,這邊要注意的是eregi 這個函數後期的php就不再支援了。
請改成preg_match_all 這個來用。

他這網站還有不少的api可以用 連天氣圖都可以提供。非常的方便 且提供的圖也蠻3D的 不會像氣象局的都很醜

隨文附上api的頁面http://api.accuweather.com/developers/weatherIcons

程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<html>
<title>抓天氣</title>
<style type="text/css">
<!--
body {
    color:#3233EF;
    margin: 0px;
}
-->
</style>
<html>
<body width="717" height="186"><font size="4">
 
<?php
$result = array();
eregi("<temperature>(.*)</temperature>", $contents, $temperature); //溫度
eregi("<realfeel>(.*)</realfeel>", $contents, $realfeel); //體感溫度
eregi("<weathertext>(.*)</weathertext>", $contents, $weathertext); //天氣敘述
preg_match_all("/</?windspeed>(.*?)</?windspeed>/", $contents, $windspeed, PREG_SET_ORDER);//風速
preg_match_all("/</?weathericon>(.*?)</?weathericon>/", $contents, $weathericon, PREG_SET_ORDER);//天氣小圖
 
echo "溫度:".$temperature[1];
echo "<br>";
echo "體感溫度:".$realfeel[1];
echo "<br>";
echo "敘述:".$weathertext[1];
echo "<br>";
echo "風速:".$windspeed[0][1];
echo "<hr>";
echo "<img src='http://api.accuweather.com/developers/Media/Default/WeatherIcons/".$weathericon[0][1]."-s.png'>";
echo "<br>";
?>
 
</font>
</body>
</html>