最近在做天气这块,在网上一搜有各种天气接口,但是大多数都是收费的。偶然发现,高德也提供天气接口,一天可以免费调用100000次,就试了一下,还是蛮好用的,就拿出来分享一波。
高德API接口地址 https://lbs.amap.com/api/webservice/guide/api/weatherinfo/
第一步,申请”web服务 API”密钥(Key);
第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;
第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。
如无特殊声明,接口的输入参数和输出数据编码全部统一为UTF-8。
天气API的服务地址:https://restapi.amap.com/v3/weather/weatherInfo?parameters
因为高德地图接口里面返回的数据:天气现象,是数字表示的,所以我们需要首先定义一个天气现象的数组,并且天气数据在短时间内不会太大的变化,可以做个缓存,利用框架自带的缓存、写到文件里,或者写到redis等其他缓存中都是可以的。
public function getWeather()
{
// 定义天气状态数组
$weatherArray = ['晴', '多云', '阴', '阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '雨夹雪', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '阵雪', '小雪', '中雪', '大雪', '暴雪', '雾', '冻雨', '沙尘暴', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '浮沉', '扬沙', '强沙尘暴', '飑', '龙卷风', '弱高吹雪', '轻雾', '霾'];
S(['type' => 'file', 'expire' => 3600, 'prefix' => 'think_']);
$cTime = date('YmdH');
//删除上一天的缓存
for ($i = 0; $i < 24; $i ++) {
$yTime = $i < 10 ? date('Ymd', time() - 86400) . '0' . $i : date('Ymd', time() - 86400) . $i;
if (S($yTime) !== false) {
S($yTime, null);
}
}
//判断当天前一时间段的缓存是否存在
if (S($cTime) !== false) {
//输出缓存中的天气数据:
} else {
// 获取天气数据
//$url = 'http://restapi.amap.com/v3/weather/weatherInfo';
$url = 'http://106.11.249.75/v3/weather/weatherInfo';
$key = '';
$city = '110101'; // 城市编码,北京东城区
$cDayParams = 'key=' . $key . '&city=' . $city . '&extensions=all&output=JSON';
$cHourParams = 'key=' . $key . '&city=' . $city . '&extensions=base&output=JSON';
// 判断链接是否可用
$retDay = get_headers($url . '?' . $cDayParams);
$retHour = get_headers($url . '?' . $cHourParams);
if ($retDay !== false && $retHour !== false) {
$cDayWeatherArr = json_decode(getContentByCurl($url, $cDayParams), true);
$cHourWeatherArr = json_decode(getContentByCurl($url, $cHourParams), true);
if (preg_match('/200/', $retDay[0]) && preg_match('/200/', $retHour[0])) {
$ret['min_tem'] = $cDayWeatherArr['forecasts'][0]['casts'][0]['nighttemp'];
$ret['max_tem'] = $cDayWeatherArr['forecasts'][0]['casts'][0]['daytemp'];
$ret['wea_style'] = $cHourWeatherArr['lives'][0]['weather'];
$ret['temperature'] = $cHourWeatherArr['lives'][0]['temperature'];
$ret['wea_pic'] = WEB_PATH . 'weather/d' . array_search($ret['wea_style'], $weatherArray) . '.png';
// 将天气存入缓存
S($cTime, json_encode($ret));
//输出
} else {
//操作失败
}
} else {
//操作失败
}
}
}
function getContentByCurl($url, $params = false, $ispost = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
关于城市编码,网上都是可以搜到的;关于天气现象数组,高德API中也有详细的介绍,大概就是这样了。