php,java获取天气预报代码

最安全官方的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博客!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云尔Websites

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值