使用XMLReader解析
$url = 'http://weather.yahooapis.com/forecastrss?w=15015432&u=c'; $reader = new XMLReader(); $reader->open($url,'utf-8'); while($reader->read()){ if($reader->name == 'yweather:condition'){ $code = $reader->getAttribute('code'); //获取天气代码 $temp = $reader->getAttribute('temp'); //获取温度 } if($reader->name == 'yweather:atmosphere'){ $humi = $reader->getAttribute('humidity'); //获取湿度 } }
使用DOMDocument解析
$url = 'http://weather.yahooapis.com/forecastrss?w=15015432&u=c'; $yweather = "http://xml.weather.yahoo.com/ns/rss/1.0"; //命名空间 $res = new DOMDocument(); $res->load($url); $node = $res->getElementsByTagNameNS($yweather, 'atmosphere'); $humi = $node->item(0)->attributes->item(0)->nodeValue; //获取湿度 $node = $res->getElementsByTagNameNS($yweather, 'condition'); $code = $node->item(0)->attributes->item(1)->nodeValue; //获取天气代码 $temp = $node->item(0)->attributes->item(2)->nodeValue; //获取温度
将数字格式的天气代码转换成相应的中文
function code2char($code){ switch($code){ case 0: return '龙卷风'; case 1: return '热带风暴'; case 2: return '暴风'; case 3: return '大雷雨'; case 4: return '雷阵雨'; case 5: return '雨夹雪'; case 6: return '雨夹雹'; case 7: return '雪夹雹'; case 8: return '冻雾雨'; case 9: return '细雨'; case 10: return '冻雨'; case 11: return '阵雨'; case 12: return '阵雨'; case 13: return '阵雪'; case 14: return '小阵雪'; case 15: return '高吹雪'; case 16: return '雪'; case 17: return '冰雹'; case 18: return '雨淞'; case 19: return '粉尘'; case 20: return '雾'; case 21: return '薄雾'; case 22: return '烟雾'; case 23: return '大风'; case 24: return '风'; case 25: return '冷'; case 26: return '阴'; case 27: return '多云'; case 28: return '多云'; case 29: return '局部多云'; case 30: return '局部多云'; case 31: return '晴'; case 32: return '晴'; case 33: return '转晴'; case 34: return '转晴'; case 35: return '雨夹冰雹'; case 36: return '热'; case 37: return '局部雷雨'; case 38: return '偶有雷雨'; case 39: return '偶有雷雨'; case 40: return '偶有阵雨'; case 41: return '大雪'; case 42: return '零星阵雪'; case 43: return '大雪'; case 44: return '局部多云'; case 45: return '雷阵雨'; case 46: return '阵雪'; case 47: return '局部雷阵雨'; default: return '水深火热'; }
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
3 週前ByDDD
節省R.E.P.O.解釋(並保存文件)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影 - 如何找到鐵匠,解鎖武器和裝甲定制
4 週前ByDDD

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器