中国天气网天气预报接口

22 篇文章 0 订阅




全国城市天气:

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中有。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值