本文目錄一覽:
- 1、php網頁的api使用 比如我自己的一個網頁,想要使用天氣網提供的api,如何得到數據,如何處理這
- 2、如何使用PHP調用API接口實現天氣查詢功能
- 3、調用百度天氣的api時候跨域的問題
- 4、百度首頁的天氣預報顯示用php怎麼實現
- 5、php怎麼抓取天氣預報?
- 6、php怎麼解析天氣預報api返回的數據
php網頁的api使用 比如我自己的一個網頁,想要使用天氣網提供的api,如何得到數據,如何處理這
接口會返回json數據,用php提供的json_decode函數可以將其轉為對象或者數組,再輸出即可。 可以參考我的網站
如何使用PHP調用API接口實現天氣查詢功能
最近在做微信公眾平台測試時,想在裡面子菜單上添加查詢未來幾天(包括今天)天氣的功能,就查找了下好用的天氣預報查詢接口API,使用比較多的有:國家氣象局天氣接口、新浪天氣預報接口、百度天氣預報接口、google天氣接口、Yahoo天氣接口等等,我使用的是百度提供的免費天氣查詢接口API,下面與大家分享下…
1、查詢方式:
百度提供的是根據緯度和城市名查詢天氣情況
2、接口事例:
3、接口參數說明:
4、返回結果說明:
5、
//城市名
$city = ‘上海’;
//對json格式的字符串進行編碼
$arr =json_decode($str,TRUE);
print_r($atr);
//城市名
$city = ‘上海’;
//獲取json格式的數據
$str =file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);
//對json格式的字符串進行編碼
$arr =json_decode($str,TRUE);
print_r($atr);
6、返回頁面的是json編碼後的數據:
[plain] view plain copy print?
meta charset=”UTF-8″
Array
(
[error] = 0
[status] = success
[date] = 2014-03-17
[results] = Array
(
[0] = Array
(
[currentCity]= 上海
[weather_data]= Array
(
[0]= Array
(
[date] = 周一(今天, 實時:19℃)
[dayPictureUrl] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南風3-4級
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多雲轉陰
[wind]= 東北風3-4級
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨轉小雨
[wind]= 東北風3-4級
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多雲轉晴
[wind]= 北風3-4級
[temperature] = 14 ~ 6℃
)
)
)
)
)
meta charset=”UTF-8″
Array
(
[error] = 0
[status] = success
[date] = 2014-03-17
[results] = Array
(
[0] = Array
(
[currentCity]= 上海
[weather_data]= Array
(
[0]= Array
(
[date] = 周一(今天, 實時:19℃)
[dayPictureUrl] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南風3-4級
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多雲轉陰
[wind]= 東北風3-4級
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨轉小雨
[wind]= 東北風3-4級
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多雲轉晴
[wind]= 北風3-4級
[temperature] = 14 ~ 6℃
)
)
)
)
)
7、PHP中自帶了處理json格式字符串的內置函數,下面做一個事例,並給出完整代碼:
[php] view plain copy print?
metacharset=”UTF-8″
?php
//城市名
$city = ‘上海’;
//獲取json格式的數據
$str = file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);
//對json格式的字符串進行編碼
$arr = json_decode($str,TRUE);
echo “城市:”.$arr[‘results’][0][‘currentCity’].” 日期:”.$arr[‘date’].”br /br /”;
foreach($arr[‘results’][0][‘weather_data’]as $val)
{
echo $val[‘date’].”br/”;
echo “天氣:{$val[‘weather’]}br/”;
echo “風向:{$val[‘wind’]}br/”;
echo “溫度:{$val[‘temperature’]}br/br /”;
}
?
metacharset=”UTF-8″
?php
//城市名
$city = ‘上海’;
//獲取json格式的數據
$str = file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);
//對json格式的字符串進行編碼
$arr = json_decode($str,TRUE);
echo “城市:”.$arr[‘results’][0][‘currentCity’].” 日期:”.$arr[‘date’].”br /br /”;
foreach($arr[‘results’][0][‘weather_data’]as $val)
{
echo $val[‘date’].”br/”;
echo “天氣:{$val[‘weather’]}br/”;
echo “風向:{$val[‘wind’]}br/”;
echo “溫度:{$val[‘temperature’]}br/br /”;
}
?
8、返回的內容如下:
調用百度天氣的api時候跨域的問題
同源的問題,目前網上沒有很好的解決百度天氣api的方案。個人目前實行的方式是,先將api數據的通過file_get_contents獲取JSON字符串,然後再用ajax調用.php函數file_get_contents應對百度天氣接口
百度首頁的天氣預報顯示用php怎麼實現
方式一、你可以去氣象局的網站使用php(python)爬蟲抓取網頁HTML內容提取其中的信息即可。
方式二、氣象局的網站一般提供了免費的API接口,可以得到一個封裝好的JSON數據包,拆開就能得到很多信息
php怎麼抓取天氣預報?
可以藉由php的api或者preg_match_all偷偷擷取去達成目的
這裡給你一段我給台灣朋友有一段源碼
?php
header(\”Content-Type: text/html; charset=utf-8\”);
function getWeather($city){
$toURL = \”
$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=’
($m6[1],’\.\./\.\./’);
$wtitle = $m6[3];
print_r($wtitle);
$weather[$i][‘date’] = date(\”m-d\”, mktime(0, 0, 0, date(\”m\”), date(\”d\”)+$i,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\”);
?
首先
$toURL = \”\”;
這裡是讀取資料的網址
上面的是台灣中央氣象局
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);
這裡是截取台灣中央氣象局網頁信息table class=\”FcstBoxTable01\” [^]*[^]*(.*)\/div的資料以及td nowrap=\”nowrap\” [^]*[^]*(.*)\/td的資料分別是1天跟1周
$m2 = explode(‘/td’,$m1[0][1]);
// print_r($m2);//取得每日資料m2[0~6]
這裡是取得每日的資料
preg_match_all(‘/src=[^]*[^](.*)/si’,$m2[$i], $m5, PREG_SET_ORDER);//取得天氣圖檔
這裡是取得天氣的圖檔
$m6 = explode(‘\”‘,$m5[0][0]);
$wi=’
($m6[1],’\.\./\.\./’);
$wtitle = $m6[3];
print_r($wtitle);
$weather[$i][‘date’] = date(\”m-d\”, mktime(0, 0, 0, date(\”m\”), date(\”d\”)+$i,date(\”Y\”)));
$weather[$i][‘temperature’] = trim(strip_tags($m2[$i]));
$weather[$i][‘title’] = $wtitle;
$weather[$i][‘img’] = $wi;
這裡是返回的網址,日期,標題,圖檔等等的資料
$weather=getWeather(\”Taipei_City\”) ;
print_r($weather);
然後這裡是顯示出地區的一周天氣預報
結論:就是如果你想從網站上面截取天氣預報
在php可以是用preg_match_all(網頁的表格table,表格的列數tr,表格的欄位td,或者更加廣泛的標籤div等等獲取)
php怎麼解析天氣預報api返回的數據
json就要用json的形式返回啊 就行ajax返回數據一樣 它的返回值是true 就是正確的 如果是返回ajax的話,先判斷data 為真就直接data.XX(字段名) 就可以取出來了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/236297.html