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部落格!
相關文章
- Python 獲取當地未來五天天氣 天氣預報 獲取天氣Python
- 天氣預報程式碼大全
- Java實現網路爬蟲 案例程式碼3:使用webmagic框架獲取天氣預報Java爬蟲Web框架
- 使用這個開源工具獲取本地天氣預報開源工具
- 請利用SAX編寫程式解析Yahoo的XML格式的天氣預報,獲取天氣預報——python學習筆記XMLPython筆記
- Java呼叫取得天氣預報WebServicesJavaWeb
- 天氣預報apiAPI
- 使用和風天氣介面獲取天氣資訊
- 中央氣象局天氣預報介面---java實現Java
- flutter天氣預報APPFlutterAPP
- 天氣預報API介面API
- 天氣預報介面收集
- PHP獲取當天凌晨時間戳常用程式碼PHP時間戳
- 獲取天氣介面資料
- axis WebServices 完美呼叫天氣預報,查詢、顯示 程式碼!Web
- 中國天氣網免費天氣預報介面APIAPI
- react native天氣預報React Native
- Flutter實踐:天氣預報Flutter
- Delphi天氣預報查詢
- 使用WebService獲取天氣實況Web
- 查詢天氣預報網站網站
- PHPAjaxJavaScriptJson實現天氣資訊獲取PHPJavaScriptJSON
- PHP 常用獲取路徑程式碼PHP
- Android Spinner(級聯 天氣預報)Android
- 5.22 天氣預報系統 小
- 0828-T3 天氣預報
- 天氣預報戰略升級為“新晴天氣”,深耕天氣+出行生活場景
- 【吐血整理】微信小程式如何接入天氣預報查詢 API微信小程式API
- 天氣預報API,你想要的它都有API
- 通過iframe呼叫天氣預報&jsonpJSON
- Java對接騰訊雲簡訊和阿里雲天氣預報Java阿里
- 5.PHP微信公眾平臺開發 - 天氣預報功能開發PHP
- PHP獲取小程式openid,10行程式碼快速獲取小程式openidPHP行程
- 天氣預報外掛程式碼--通過傳遞城市拼音名,最簡單的天氣外掛--更多看2345網站網站
- Mac天氣預報元件:Weather Widget Live for MacMac元件
- 開發chrome外掛入門-天氣預報Chrome
- 實戰CXF呼叫Webxml天氣預報服務WebXML
- PHP獲取MAC地址的實現程式碼PHPMac