導航:首頁 > 編程語言 > php顯示天氣預報

php顯示天氣預報

發布時間:2022-07-16 22:57:11

php在日歷上面顯示天氣預報這么顯示 我日歷代碼已經寫好在下面這么添加天氣預報

直接用中國天氣網的插件就可以

Ⅱ 網頁中加天氣預報代碼 能顯示國外的天氣

<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的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等等獲取)

Ⅳ php的socket調用可以實現查天氣嗎

本文分享下,php調用yahoo與sina的天氣api,實現實時顯示天氣預報的代碼,有興趣的朋友研究下吧。

yahoo 天氣預報
地址 http://developer.yahoo.com/weather/
代碼:

復制代碼代碼示例:
<?php
header ( 'Content-Type: text/html; charset = utf-8' );
class weather {
static $url = 'http://xml.weather.yahoo.com/forecastrss?u=c&w=';
static $city = 'Beijing'; //默認城市北京 這里要注意的是 city 要填拼音 我試過用中文有好幾個地區都調用不到
static $weatherXML = '';
static $woeid_file = "woeid";
static $file_path = "data/";

/**
* 獲得遠程xml並緩存到本地
*/
static public function getXML($city = null) {

if ($city != null){
self::$city = $city;
}
self::$weatherXML = self::$file_path . md5(self::$city) . '-weather.xml';
if (file_exists( self::$weatherXML )) {
$fileTime = filemtime ( self::$weatherXML );
$stater = time () - $fileTime - 60 * 60 * 2;
if ($stater > 0) {
return true;
}
}
//獲取woeid
$woeid = self::getWOEID();
self::$url = self::$url . $woeid[0];
//獲取當天 天氣
$XML = self::vget(self::$url);
//保存當天 天氣到文件
self::cacheXML($XML);

self::analysisXML($XML);
}

static public function analysisXML($simple) {

$p = xml_parser_create();

xml_parse_into_struct($p, $simple, $vals, $index);

xml_parser_free($p);

//本周天氣
$weekindex = $index['YWEATHER:FORECAST'];
$week = array();
foreach($weekindex as $k=>$v){
$week[$k] = $vals[$v]['attributes'];
}
unset($index);
unset($vals);
print_r($week);
/*
<yweather:forecast day="Wed" date="18 Sep 2013" low="20" high="32" text="Sunny" code="32"/>
* day 星期
* date 日期
* low 最低溫度
* high 最高溫度
* test 天氣狀態
* code 天氣圖標
*/
}
/*
* 取得地區WOEID碼
*/
static private function getWOEID(){
static $woeid = array();

if(isset($woeid[self::$city])){
return $woeid[self::$city];
}

if (file_exists( self::$file_path . self::$woeid_file )) {
$woeidSTR = file_get_contents(self::$file_path . self::$woeid_file);
$woeid = json_decode($woeidSTR , true);
if(isset($woeid[self::$city])){
return $woeid[self::$city];
}
}
$geoPlaces = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text='".self::$city."%20CH'";

$XML = simplexml_load_file( $geoPlaces );

if(isset($XML->results->place[0])){
$rs = $woeid[self::$city] = $XML->results->place[0]->woeid;
//保存到文件
$woeidSTR = json_encode($woeid);
file_put_contents(self::$file_path . self::$woeid_file, $woeidSTR);
return $rs;
}else{
//如果找不到城市 woeid 默認城市就改為 北京
self::$city = "Beijing";

return self::getWOEID();
}
}
/**
* 創建xml緩存
* @param $contents 要緩存的內容
*/
static private function cacheXML($contents) {
$contents = str_ireplace ( '<?xml version="1.0"?>', "<?xml version=\"1.0\"?> \n", $contents );
$contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
file_put_contents ( self::$weatherXML, $contents ) or die ( '沒有寫許可權' );
}
/**
* 模擬獲取內容函數
* @param type $url
* @return type
*/

static private function vget($url) {
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
$curl = curl_init (); // 啟動一個CURL會話
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證證書來源的檢查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 從證書中檢查SSL加密演算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ); // 模擬用戶使用的瀏覽器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自動設置Referer
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 發送一個常規的Post請求
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設置超時限制防止死循環
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 顯示返回的Header區域內容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 獲取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 執行操作
if (curl_errno ( $curl )) {
curl_close ( $curl ); // 關閉CURL會話
die('Errno' . curl_error ( $curl )) ;
}
curl_close ( $curl ); // 關閉CURL會話
return $tmpInfo; // 返回數據
}
}
weather::getXML("Changsha");

Ⅳ 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);
?>

Ⅵ 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的代碼

Ⅶ 怎麼用php抓取天氣預報先說下思路,再舉個例子。

我昨天剛做了個,本來打算自己用,你既然問,就分享了吧!
PS:因為我是菏澤人,所以,如果你什麼都不填,就顯示菏澤天氣。
如果想顯示別的,直接輸入就行,比如"香港",然後提交就OK了
http://young.boustead.e.cn/data/shunzi/tq.php
不知道是否合乎樓主的意願(不用JS)

Ⅷ php如何獲得當地的天氣預報 - 技術問答

這個無需PHP做。前端就可以獲取,直接搜索天氣API,會有很多天氣介面。
http://www.weather.com.cn/data/sk/101010100.html
或者
http://cj.weather.com.cn/

Ⅸ 天氣預報代碼

根據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>

閱讀全文

與php顯示天氣預報相關的資料

熱點內容
性價比雲伺服器怎麼購買 瀏覽:110
為什麼打開騰訊視頻是文件夾 瀏覽:206
美劇戰機電影 瀏覽:719
七七影視大全里緩存的電影怎麼保存到手機相冊 瀏覽:595
讀物app在哪裡可以掃碼 瀏覽:634
編程學員作業檢查報告 瀏覽:256
單片機有幾個寄存器 瀏覽:203
安卓如何安裝泰劇迷app 瀏覽:42
如何架設暗黑伺服器 瀏覽:896
暴露性器官的電影 瀏覽:364
java培訓內容 瀏覽:911
dos命令盤符 瀏覽:701
javadouble取小數 瀏覽:924
十大封禁鬼片電影免費在線觀看 瀏覽:641
塔羅牌程序源碼 瀏覽:840
華為手機怎麼隱私與加密 瀏覽:287
朴超賢電影都有哪些 瀏覽:124
電影大區 瀏覽:481
阿里方舟編譯 瀏覽:232
華為光貓命令注冊和自動注冊區別 瀏覽:333