獲取城市的城市代碼了
function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, 'http://www.weather.com.cn/forecast/index.shtml');//必須滴 curl_setopt($ch, CURLOPT_COOKIE,'isexist=1');//最好帶上 比較穩定 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0'); curl_setopt($ch, CURLOPT_HEADER, 0); $data = curl_exec($ch); curl_close($ch); return $data; } $city = '廣州'; $url = 'http://toy1.weather.com.cn/search?cityname='.urlencode($city).'&callback=jsonp'.time().mt_rand(100, 999).'&_='.time().mt_rand(100, 999); $result = explode('~', substr(strtolower(curl($url)), 28, -4)); var_export($result); exit();
國家氣象局提供的天氣預報接口
接口地址:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
XML接口
http://flash.weather.com.cn/wmaps/xml/china.xml 這個是全國天氣的根節點,列出所有的省,其中的pyName字段是各個省XML的文件名,比如北京的是beijing,那就意味着北京的XML地址為 http://flash.weather.com.cn/wmaps/xml/beijing.xml 一個省的天氣,其中列出該省各個市的數據,北京就列出各個區。 tmp1是最低溫低,tmp2是最高溫度,url非常重要,我們一會兒再說。state1和state2是神馬轉神馬,每個數代表一個天氣現象。天氣現象 非常多,我本想全部分析出來,后來直接放棄了這個想法。因為我看到了一個城市的天氣現象的編碼是26...我現在知道的有0.晴 1.多雲 2.陰 6.雨夾雪 7.小雨 8.中雨 13.陣雪 14.小雪 其中后來發現知道這個沒用,這個數字的主要作用是檢索圖片的!!!