php天氣源碼
㈠ 怎麼用php抓取天氣預報先說下思路,再舉個例子。
我昨天剛做了個,本來打算自己用,你既然問,就分享了吧!
PS:因為我是菏澤人,所以,如果你什麼都不填,就顯示菏澤天氣。
如果想顯示別的,直接輸入就行,比如"香港",然後提交就OK了
http://young.boustead.e.cn/data/shunzi/tq.php
不知道是否合乎樓主的意願(不用JS)
㈡ 誰有WAP、PHP版的天氣預報源碼最好能預報到「市級」的。或者能給個查看本縣天氣的wap網址.我做地方論壇
PHP好像沒有站寫,JSP和aspx的網站寫出來的都好多,很多站有共享,大把站有,我也寫一種asp的,垃圾程序,如果要插你的程序里還要看你的程序代碼如何,如果是單獨的天氣程序可以搞小偷,這方便簡單
㈢ 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");
㈤ 天氣預報調用源碼
天氣預報調用支持DIY樣式,支持IP顯示天氣!!
http://t.xidie.com/
這里DIY的天氣代碼不但可以放到QQ空間里哦!!裝飾你的個人主頁最佳天氣代碼
多種樣式可供調用!!
支持IP獲取所在用戶城市天氣,也可指用顯示城市天氣!!
天氣預報插件 樣式多,而且在不斷增加中,你也可以上傳自己設計好的背景圖片,做為天氣的背景!!
㈥ 如何製作微信返回天氣預報功能php.來自Hurray0
一般流程是:
1.用戶輸入需要查看天氣等信息的地點發送給公眾號,如:北京朝陽天氣
2.公眾號接受到文本信息,解析接受:北京朝陽天氣
3.調用天氣api,查詢天氣信息
4.把查詢到的信息返回給用戶
這里利用到了微信開放文檔中的:接收普通消息-->文本消息
,具體細節查看文檔:
https://mp.weixin.qq.com/wiki
㈦ 怎麼利用php的api獲得城市新浪天氣
<?php
//發送瀏覽器頭信息告訴瀏覽器本頁面的編碼防止亂碼
header("Content-type:text/html;charset=utf-8");
$city='上海';
//定義介面地址
$encode_url='http://php.weather.sina.com.cn/xml.php?city='.urlencode(mb_convert_encoding($city,'gb2312','utf8')).'&password=DJOYnieT8234jlsK&day=1';
//請求介面地址獲取xml
$xml_source=file_get_contents($encode_url);
//var_mp($xml_source);
//利用simplexml解析類將xml轉成對象
$xml_object=simplexml_load_string($xml_source);
//var_mp($xml_object);
echo$xml_object->Weather->status1;
你可以隨便調試 記得獎勵哦
㈧ 網頁上的「時間、天氣」源碼
<script src=" http://www.hao123.com/js/base.js?v=2.0 "></script> <script src=" http://www.hao123.com/js/main.js?v=2.2 "></script> <div id="Forecast"></div>
㈨ 誰有 15天天氣預報 源碼 最好是PHP的 或者API介面
你好可以使用中國天氣網的天氣插件 比較好用我就用這個
㈩ 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的代碼