本文目錄一覽:
誰有 15天天氣預報 源碼 最好是PHP的 或者API介面
你好,目前相關氣象部門給出的都是七天天氣預報,而且前三天是基本預報,後四天是參考天氣,沒有15天的畢竟天有不測風雲,
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獲取天氣預報的代碼
?php
$URLStyle = “”;
$chinaURL = sprintf($URLStyle, “china”);
$chinaStr = file_get_contents($chinaURL);
$chinaObj = simplexml_load_string($chinaStr);
$chinaObjLen = count($chinaObj-city);
echo “chinaObjLen = “.$chinaObjLen.”\n”;
for ($i=0;$i$chinaObjLen;$i++){
//遍歷省一級節點,共37個
$level1 = $chinaObj-city[$i][“pyName”];
$shengjiURL = sprintf($URLStyle, $level1);
$shengjiStr = file_get_contents($shengjiURL);
//echo $shengjiStr;
$shengjiObj = simplexml_load_string($shengjiStr);
$shengjiObjLen = count($shengjiObj-city);
// echo $chinaObj-city[$i][“quName”];
// echo ” “.$shengjiObjLen.”\n”;
for ($j=0;$j$shengjiObjLen;$j++){
//遍歷市一級節點
$level2 = $shengjiObj-city[$j][“pyName”];
$shijiURL = sprintf($URLStyle, $level2);
$shijiStr = file_get_contents($shijiURL);
//echo $shijiStr;
$shijiObj = simplexml_load_string($shijiStr);
//直轄市和海南、台灣、釣魚島等沒有縣級節點
if(!$shijiObj){
echo “WARNNING: not exsit next level node. – “.$level1.”-“.$shijiURL.”\n”;
echo ‘ “‘.$shengjiObj-city[$j][“cityname”].'” = ‘;
echo $shengjiObj-city[$j][“url”].”,\n”;
continue;
}
$shijiObjLen = count($shijiObj-city);
//echo $shengjiObj-city[$j][“cityname”].” “;
//echo $shijiObjLen.”\n”;
for ($k=0;$k$shijiObjLen;$k++){
//遍歷縣一級節點
$xianji_code = $shijiObj-city[$k][“url”];
echo ‘ “‘.$shijiObj-city[$k][“cityname”].'” = ‘;
echo $shijiObj-city[$k][“url”].”,\n”;
//echo $xianji_code.”\n”;
}
}
}
//print_r($chinaObj);
?
通過XML介面根節點遞歸獲得全國幾千個縣以上城市cide code的代碼
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286890.html