全国城市天气:
http://flash.weather.com.cn/baidumap/xml/china.xml
------------------------------------------------------------------------------------
function getWeatherWeather($city){ include 'weather_code.php'; $code=$weather_code[$city]; $url="http://d1.weather.com.cn/cx_15fcall/$code.html?callback=callBack_fc15"; $file=curl_get_contents($url); $file=str_replace("callBack_fc15(", "", $file); $file=str_replace(")", "", $file); $str_json=json_decode($file); $weather_arr=$str_json->weafc15; $size=count($weather_arr); for($i=0;$i< 4;$i++){ $weather_arr2=$weather_arr[$i]; $week=$weather_arr2->week;//星期 $wea=$weather_arr2->wea;//天气情况 $tem1=$weather_arr2->tem1;//温度1 $tem2=$weather_arr2->tem2;//温度2 $winP1=$weather_arr2->winP1;//风速1 $winP2=$weather_arr2->winP2;//风速2 $tianqi_tubiao_day=$weather_arr2->img;//白天天气图标 $tianqi_tubiao_day=str_replace("d0", "", $tianqi_tubiao_day); $tianqi_tubiao_day='http://tianqi.911cha.com/pic/day/'. ($tianqi_tubiao_day+1).'.gif'; $tianqi_tubiao_night=$weather_arr2->imgn;//晚上天气图标 $tianqi_tubiao_night=str_replace("n0", "", $tianqi_tubiao_night); $tianqi_tubiao_night='http://tianqi.911cha.com/pic/day/'. ($tianqi_tubiao_night+1).'.gif'; $tianqi=$tem1.'~'.$tem2.'℃ '.$wea.' '.$winP1;//就加一个风速吧。 $arr[$i]=array('date'=>$week,'tianqi'=>$tianqi,'pic'=>$tianqi_tubiao_day); } return $arr; } function curl_get_contents($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址 //curl_setopt($ch, CURLOPT_POST, true); //curl_setopt($ch, CURLOPT_POSTFIELDS, "IdCard={$content}"); //curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1'); //用户访问代理 User-Agent curl_setopt($ch, CURLOPT_REFERER,"http://www.weather.com.cn");//这个一定要加。 //curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301 curl_setopt($ch, CURLOPT_TIMEOUT, 2); //设置超时 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果 curl_setopt($ch, CURLOPT_COOKIE, ""); $result = curl_exec($ch); curl_close($ch); return $result; }
weather_code.php 文件在http://blog.csdn.net/macwhirr123/article/details/45173209中有。