❶ php如何獲得當地的天氣預報 - 技術問答
這個無需PHP做。前端就可以獲取,直接搜索天氣API,會有很多天氣介面。
http://www.weather.com.cn/data/sk/101010100.html
或者
http://cj.weather.com.cn/
❷ php怎麼抓取天氣預報
可以藉由php的api或者preg_match_all偷偷擷取去達成目的
這里給你一段我給台灣朋友有一段源碼
<?php
header("Content-Type:text/html;charset=utf-8");
functiongetWeather($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('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);
preg_match_all('/<tdnowrap="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="http://www.cwb.gov.tw/V7/forecast/taiwan/inc/city/$city.htm";
這里是讀取資料的網址
上面的是台灣中央氣象局
preg_match_all('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);
preg_match_all('/<tdnowrap="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等等獲取)
❸ 誰幫我找一個天氣預報網頁代碼
代碼如下:
<iframe id=weatherwin border=0 name=weatherwin marginWidth=0 marginHeight=0 src="http://weather.369.com/weather.html" frameBorder=no width=160 scrolling=no height=60></iframe>
也可以用265.com的代碼.在那裡上網就顯示那裡的天氣
❹ PHP 頁面調用天氣預報web服務 我想在一個PHP頁面直接調用現成的webservice
完全可以。前提是要打開soap擴展,調用方法如下:
<?php
$client = new SoapClient('http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl');
$parm=array('theCityCode'=>'三亞','theUserID'=>'');
$result=$client->getWeather($parm);
print_r($result);
?>
❺ 網頁中加天氣預報代碼 能顯示國外的天氣
<iframe src=" http://tsov.net/tq/weather.php?bgcolor=383838&txtcolor=eee " width="205" height="131" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" border="0" /></iframe> 把這段代碼中的 那個網址【 http://tsov.net/tq/weather.php?bgcolor=383838&txtcolor=eee 】換成有你 想要地方的天氣的網址即可,另外注意高度和寬度要適合。原理很簡單,你 可以往那兒放個網路試試 追問: <iframe src=" http://www.thinkpage.cn/weather/weather.aspx?c=UKXX0085&l=zh-TW&p=MSN&a=1&u=C&s=2&m=1&x=1&d=0&fc=00B0F0&bgc=&bc= " frameborder="0" scrolling="no" width="200" height="130" allowTransparency="true"></iframe> 這個是我頭剛找到的 你說的我沒試出來 謝謝 回答: 那我 都試出來了 呀,,剛才,你 可以吧那代碼保存成HTML再打開看看是否如意 追問: <iframe src=" http://www..com/tq/weather.php?bgcolor=383838&txtcolor=eee " width="205" height="131" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" border="0" /></iframe> 是這樣改嗎 回答: <iframe src=" http://www.thinkpage.cn/weather/weather.aspx?c=UKXX0085&l=zh-TW&p=MSN&a=1&u=C&s=2&m=1&x=1&d=0&fc=00B0F0&bgc=&bc= " frameborder="0" scrolling="no" width="200" height="130" allowTransparency="true"></iframe> 就用這個代碼,保存成HTML再打開看 追問: 這個不是我剛發給你的嗎 這個我試過了 可以的 我是說你讓我改的沒試出來 回答: http://www..com/tq/weather.php?bgcolor=383838&txtcolor=eee " 這網址本來就打不開, 我的 意思是網址你可以隨便換,只是注意寬和高,就行
記得採納啊
❻ php獲取天氣預報的代碼
<?php
$URLStyle="http://flash.weather.com.cn/wmaps/xml/%s.xml";
$chinaURL=sprintf($URLStyle,"china");
$chinaStr=file_get_contents($chinaURL);
$chinaObj=simplexml_load_string($chinaStr);
$chinaObjLen=count($chinaObj->city);
echo"chinaObjLen=".$chinaObjLen." ";
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." ";
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:notexsitnextlevelnode.-".$level1."-".$shijiURL." ";
echo'"'.$shengjiObj->city[$j]["cityname"].'"=>';
echo$shengjiObj->city[$j]["url"].", ";
continue;
}
$shijiObjLen=count($shijiObj->city);
//echo$shengjiObj->city[$j]["cityname"]."";
//echo$shijiObjLen." ";
for($k=0;$k<$shijiObjLen;$k++){
//遍歷縣一級節點
$xianji_code=$shijiObj->city[$k]["url"];
echo'"'.$shijiObj->city[$k]["cityname"].'"=>';
echo$shijiObj->city[$k]["url"].", ";
//echo$xianji_code." ";
}
}
}
//print_r($chinaObj);
?>
通過XML介面根節點遞歸獲得全國幾千個縣以上城市cide code的代碼
❼ 誰能給我提供一個網頁中插入天氣預報的代碼或者是網站 最好是那種可以自己切換城市的
<iframewidth="280"scrolling="no"height="25"frameborder="0"allowtransparency="true"src="http://i.tianqi.com/index.php?c=code&id=34&icon=1&num=3"></iframe>
❽ 如何在網頁中顯示天氣預報
1,打開360安全衛士
2,點擊優化加速--
3,點擊右下角「開機小助手設置」
4,勾上顯示天氣預報
(8)php天氣預報網頁擴展閱讀:
天氣預報的好處:
1,農民對於天氣預報的依賴比普通群眾更強,因為天氣氣候條件直接影響著農業生產,莊稼的播種、收獲都跟天氣有關,天幫忙則豐收,災害天氣多,收成就會減少。
2,主要是為了預防疾病,尤其是感冒,關注天氣變化,加減衣服。特別是突然下雨或者降溫,加減衣服不當很容易生病。
關注天氣可以注意下雨及降溫。這種比較突然的天氣變化是住在城市的人的必須關注點。如果要去外地,更要知道當地是什麼天氣情況,決定穿衣和攜帶的衣服。
3,天氣時刻伴隨著我們的生活,應用天氣預報可以及時了解天氣的趨勢,給人們的工作、出行、旅遊、化妝、洗車、運動生活等帶來樂趣和便利。
以為未來的事情做安排和打算啊,比如明天旅遊,明天出行。大的作用就可以預防經濟損失,比如暴風,台風等。
❾ 網頁天氣預報插件代碼
不用安裝插件,你把下面的代碼插入你的網頁中就可以了:第一種: 代碼: <iframe width="145" height="130" border="0" align="center" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" src="" allowTransparency="true"></iframe> 效果預覽: 說明:這種適合於在網頁的邊欄插入。但一個缺點是,上面的4個城市是既定的,無法改成別的。插入時,選好網頁上的位置,直接將左欄的源代碼全部拷進去就行了 第二種: 代碼: <IFRAME ID='ifm2' WIDTH='189' HEIGHT='190' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' SRC=''></IFRAME> 效果預覽: 說明:這種 也適合於在網頁的邊欄插入。上面的城市可以自定,比如廈門可改成別的。定製的方法是修改我代碼中標紅的數字,從1開始代表「香港」開始,每個數字都代表一個城市,廈門是287,具體要哪個城市自己找一下罷。 第三種: 代碼: <IFRAME ID='ifm2' WIDTH='260' HEIGHT='70' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' src=""></iframe> 效果預覽: 說明:這種適合於在網頁的頭欄插入。上面的城市可以自定,比如 石家莊可改成別的。定製的方法是修改我代碼中標紅的數字。具體各個城市對照的id表可在此下載:城市名稱及ID對照表(請右鍵點擊—>另存為)。 第四種: 代碼: <iframe width="469" height="218" border="0" align="center" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" src="廈門" allowTransparency="true"></iframe> 效果預覽: 說明:這種適合於在網頁的正欄插入。上面的城市可以自定,比如廈門可改成別的。定製的方法是修改我代碼中標紅的 名稱。這里比較簡單,直接用漢字改就行了。比如是福州的,你就直接把「廈門」改成福州就行。 第五種: 代碼: <IFRAME ID='ifm1' WIDTH='405' HEIGHT='332' ALIGN='center' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' SRC=""></IFRAME> 效果預覽: 第六種: 代碼: <iframe width=160 height=230 frameborder=0 scrolling=NO src=南昌></iframe> 效果預覽: 說明:這種適合於在網頁的邊欄插入。上面的城市可以自定,比如南昌可改成別的。定製的方法是修改我代碼中標紅的名稱。這里比較簡單,直接用漢字改就行了。比如是福州的,你就直接把「廈門」改成福州就行
很不錯哦,你可以試下
qgulqjbsvk62041435102011-10-12 9:09:45
❿ 怎麼用php抓取天氣預報先說下思路,再舉個例子。
我昨天剛做了個,本來打算自己用,你既然問,就分享了吧!
PS:因為我是菏澤人,所以,如果你什麼都不填,就顯示菏澤天氣。
如果想顯示別的,直接輸入就行,比如"香港",然後提交就OK了
http://young.boustead.e.cn/data/shunzi/tq.php
不知道是否合乎樓主的意願(不用JS)