php,java獲取天氣預報程式碼

Websites發表於2015-11-11
最安全官方的php,java天氣預報程式碼,來自於中國氣象局

PHP獲取天氣:

//PHP程式碼:
set_time_limit(0);
$private_key = 'XXX';
$appid='XXX';
$appid_six=substr($appid,0,6);
$areaid = '101210304';
$type='forecast_v';
$date=date("YmdHi");
$public_key="http://open.weather.com.cn/data/?areaid=".$areaid."&type=".$type."&date=".$date."&appid=".$appid;
$key = base64_encode(hash_hmac('sha1',$public_key,$private_key,TRUE));
$URL="http://open.weather.com.cn/data/?areaid=".$areaid."&type=".$type."&date=".$date."&appid=".$appid_six."&key=".urlencode($key);
$string=file_get_contents($URL);
$arr_tmp = (array)json_decode($string);
$arr_tmp = (array)$arr_tmp['f'];
$weathers = array();
foreach ($arr_tmp['f1'] as $k=>$v){
	$v_tmp = (array)$v;
	$timenow = explode('|',$v_tmp['fi']);
	$nowdate = strtotime(date('H:i',NOW_TIME));
	if ($nowdate > strtotime($timenow[0]) && $nowdate < strtotime($timenow[1])){
		$v_tmp['type'] = 'day';
	}else{
		$v_tmp['type'] = 'night';
	}
	$weathers[] = $v_tmp;
}

//天氣現象編碼表
$qixiangs = array('00'=>'晴','01'=>'多雲','02'=>'陰','03'=>'陣雨','04'=>'雷陣雨','05'=>'雷陣雨伴有冰雹','06'=>'雨夾雪',
        '07'=>'小雨','08'=>'中雨','09'=>'大雨','10'=>'暴雨','11'=>'大暴雨','12'=>'特大暴雨','13'=>'陣雪','14'=>'小雪','15'=>'中雪',
        '16'=>'大雪','17'=>'暴雪','18'=>'霧','19'=>'凍雨','20'=>'沙塵暴','21'=>'小到中雨','22'=>'中到大雨','23'=>'大到暴雨','24'=>'暴雨到大暴雨',
        '25'=>'大暴雨到特大暴雨','26'=>'小到中雪','27'=>'中到大雪','28'=>'大到暴雪','29'=>'浮塵','30'=>'揚沙','31'=>'強沙塵暴','53'=>'霾','99'=>'無'
);
//風力編碼表
$fengli = array('0'=>'微風','1'=>'3-4級','2'=>'4-5級','3'=>'5-6級','4'=>'6-7級','5'=>'7-8級','6'=>'8-9級','7'=>'9-10級','8'=>'10-11級','9'=>'11-12級');
//風向編號表
$fengxiang = array('0'=>'無持續風向','1'=>'東北風','2'=>'東風','3'=>'東南風','4'=>'南風','5'=>'西南風','6'=>'西風','7'=>'西北風','8'=>'北風','9'=>'旋轉風');
//星期幾
function getWeek($nowtime=''){
    $nowtime = is_numeric($nowtime) ? $nowtime : NOW_TIME;
    $weekarray=array('日','一','二','三','四','五','六');
    return '星期'.$weekarray[date('w',$nowtime)];
}



//html程式碼
<?php if (!empty($this->weathers)){?>
	<div class="weather_bottom clearfix">
	<?php foreach ($this->weathers as $key=>$value){?>
		<div class="weather_bottom_list" style="margin-left: 10%">
			<div class="weather_bottom_list_head"><?php echo $key==0 ? "今天" : getWeek(strtotime($key.' days'));?></div>
			<div class="weather_bottom_list_content" style="background: url('/weather/<?php echo $value['type'];?>/<?php echo $value['type'] == 'day' ? $value['fa'] : $value['fb'];?>.png') 0 0 no-repeat;background-size: 100% 100%;">
				
			</div>

			<div class="weather_bottom_list_bottom">
				<?php echo $value['fc'].'℃/'.$value['fd'].'℃';?>
			</div>
		</div>
	<?php }?>
	</div>
<?php }?>


JAVA獲取天氣程式碼:

package com.weather.log.test;
 
import javax.crypto.Mac;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import javax.crypto.spec.SecretKeySpec;
 
public class javademo {
 
    private static final char last2byte = (char) Integer.parseInt("00000011", 2);
    private static final char last4byte = (char) Integer.parseInt("00001111", 2);
    private static final char last6byte = (char) Integer.parseInt("00111111", 2);
    private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
    private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
    private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
    private static final char[] encodeTable = new char[] { 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '+', '/'
    };
 
    public static String standardURLEncoder(String data, String key) {
        byte[] byteHMAC = null;
        String urlEncoder = "";
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            mac.init(spec);
            byteHMAC = mac.doFinal(data.getBytes());
            if (byteHMAC != null) {
                String oauth = encode(byteHMAC);
                if (oauth != null) {
                    urlEncoder = URLEncoder.encode(oauth, "utf8");
                }
            }
        } catch (InvalidKeyException e1) {
            e1.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return urlEncoder;
    }
 
    public static String encode(byte[] from) {
        StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);
        int num = 0;
        char currentByte = 0;
        for (int i = 0; i < from.length; i++) {
            num = num % 8;
            while (num < 8) {
                switch (num) {
                case 0:
                    currentByte = (char) (from[i] & lead6byte);
                    currentByte = (char) (currentByte >>> 2);
                    break;
                case 2:
                    currentByte = (char) (from[i] & last6byte);
                    break;
                case 4:
                    currentByte = (char) (from[i] & last4byte);
                    currentByte = (char) (currentByte << 2);
                    if ((i + 1) < from.length) {
                        currentByte |= (from[i + 1] & lead2byte) >>> 6;
                    }
                    break;
                case 6:
                    currentByte = (char) (from[i] & last2byte);
                    currentByte = (char) (currentByte << 4);
                    if ((i + 1) < from.length) {
                        currentByte |= (from[i + 1] & lead4byte) >>> 4;
                    }
                    break;
                }
                to.append(encodeTable[currentByte]);
                num += 6;
            }
        }
        if (to.length() % 4 != 0) {
            for (int i = 4 - to.length() % 4; i > 0; i--) {
                to.append("=");
            }
        }
        return to.toString();
    }
     
     
    public static void main(String[] args) {
        try {
             
            //需要加密的資料  
            String data = "http://open.weather.com.cn/data/?areaid=xxxxxxxxxx&type=xxxxxxxx&date=xxxxxxxxx&appid=xxxxxxx";  
            //金鑰  
            String key = "xxxxx_SmartWeatherAPI_xxxxxxx";  
             
            String str = standardURLEncoder(data, key);
 
            System.out.println(str);
             
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


天氣圖png下載:http://pan.baidu.com/s/1c0CzrAo


有用的請點贊哦!

謝謝關注websites部落格!


相關文章