php天氣代碼
1. 如何使用php調用API介面實現天氣查詢功能
最近在做微信公眾平台測試時,想在裡面子菜單上添加查詢未來幾天(包括今天)天氣的功能,就查找了下好用的天氣預報查詢介面API,使用比較多的有:國家氣象局天氣介面、新浪天氣預報介面、網路天氣預報介面、google天氣介面、Yahoo天氣介面等等,我使用的是網路提供的免費天氣查詢介面API,下面與大家分享下...
1、查詢方式:
網路提供的是根據緯度和城市名查詢天氣情況
2、介面事例:
2. 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等等獲取)
3. 誰幫我找一個天氣預報網頁代碼
代碼如下:
<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的代碼.在那裡上網就顯示那裡的天氣
4. 求本鏈接的天氣預報代碼
把這個網址插入到網頁對話框中就會自動顯示訪問者地區的天氣預報
5. 網頁中加天氣預報代碼 能顯示國外的天氣
<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 " 這網址本來就打不開, 我的 意思是網址你可以隨便換,只是注意寬和高,就行
記得採納啊
6. 我的世界天氣指令代碼大全
1、天氣指令有以下幾種:下雨,輸入/weatherrain。
2、不下雨,輸入/weatherclear。
3、雷陣雨,輸入/weatherthunder。
4、weatherrain(下雪)。
5、weatherclear(無天氣)。
6、weatherthunder(暴風雪)。
7. 求網站中加入天氣預報的代碼
下面這段是它的源文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>07越秀</title>
<link href="../css/ddg.css" rel="stylesheet" type="text/css">
</head>
<body style="background-color:transparent">
<table width="100%" height="80" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="15"><img src="images/spacer.gif" width="1" height="8" /></td>
</tr>
<tr>
<td width="60" align="center" valign="top"><!--5-12修改6開始--><img src="http://weather.news.163.com/weather/images/logo/01.gif" alt="" /><img src="http://weather.news.163.com/weather/images/logo/03.gif" alt="" /><!--5-12修改6結束--></td>
<td align="left" valign="top"><font id="dateFont">2007年1月16日<br />
星期二 下午17:02<br />
</font>
<script>
var day = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
function showDate()
{
font = document.getElementById("dateFont");
var now = new Date();
var str = "";
var hours = now.getHours() + "";
if(hours.length < 2)
{
hours = "0" + hours;
}
var minutes = now.getMinutes() + "";
if(minutes.length < 2)
{
minutes = "0" + minutes;
}
var seconds = now.getSeconds() + "";
if(seconds.length < 2)
{
seconds = "0" + seconds;
}
str += "<font style=\"color:#000000;\"><b>" + now.getFullYear() + "年" + (now.getMonth() + 1) + "月" + now.getDate() + "日</b></font><br><font style=\"color:#EB7002;\">" + day[now.getDay()] + " " + hours + ":" + minutes + ":" + seconds + "</font><br>";
font.innerHTML = str;
}
setInterval("showDate()" , 1000);
</script>
<font style="color:#EB7002;">
多雲轉陣雨 25℃~34℃
</font></td>
</tr>
<tr>
<td height="8"><img src="images/spacer.gif" width="1" height="8" /></td>
</tr>
</table>
</body>
</html>
再給你推薦一個根據不同IP顯示不同地區的代碼:
<iframe src="http://www.ttyyy.com/tianqi/tq.html" width="160" height="60" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
都試試吧
8. 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的代碼
9. 怎樣用php自動在24點時將天氣情況加入資料庫
找一段天氣預報代碼,將返回的json代碼通過連接語句插入到資料庫中。
然後每天24點定時訪問這個php鏈接就可以了。
10. 天氣預報代碼
根據IP地址,顯示當地的天氣預報:
<iframe src="http://www.tianqi123.com/php/current_city.php" width=178 height=248 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no align=center id=url></iframe>
第二個:265的.
<iframe src=http://www.cms52.cn/cms/20080301/"http://weather.265.com/weather.htm" width="168" height="54" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no">
</iframe>
第三個:騰訊QQ的:
<iframe SRC='http://weather.qq.com/inc/ss258.htm' id='ifm2' WIDTH='189' HEIGHT='190' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO'></iframe>
第四個:還是騰訊QQ的:
<iframe SRC=http://www.cms52.cn/cms/20080301/"http://weather.qq.com/24.htm" id='ifm1' WIDTH='405' HEIGHT='332' ALIGN='center' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO'></iframe>
再送你一款QQ電台的調用,蠻小巧的:
<IFRAME marginWidth=0 marginHeight=0 src=http://www.cms52.cn/cms/20080301/"http://listen.qq.com/cj/player.htm" frameBorder=0 width=255 scrolling=no height=30></IFRAME>