如何使用数据接口平台对接天气数据

前言

在制作天气查询小程序的过程中,最核心的就是天气的数据,这些数据在各大天气网都能找到,但是不能被我们小程序中直接使用。这时候就要找数据平台使用他们的接口为小程序服务。

找到合适的数据平台

市面上很多数据平台,但是好用又便宜的就不多了。这里我使用的是极速数据平台 (www.jisuapi.com)

API列表
正好这几天过节在做活动,2.5元就有10000次的接口使用次数,实属良心。

接口的对接

测试接口

这个平台大部分接口会有免费的测试套餐,我们在对接时可以先购买测试套餐(0元)试试返回的结果是不是需要的,避免后期买错接口的麻烦。
API测试
调试页面
上图给大家写了详细的图解,查看返回的数据都有哪些。
为了更好查看,这里我在页面单独调用了接口地址,https://api.jisuapi.com/weather/query?appkey=这里换上你自己的APPKEY&city=北京

返回数据

参数名称类型说明
citystring城市
cityidint城市ID
citycodestring城市天气代号
datestring日期
weekstring星期
weatherstring天气
tempstring气温 ℃
temphighstring最高气温 ℃
templowstring最低气温 ℃
imgstring图片数字
humiditystring湿度 %
pressurestring气压 hpa
windspeedstring风速 m/s
winddirectstring风向
windpowerstring风级
updatetimestring更新时间
indexstring生活指数
inamestring指数名称
ivaluestring指数值
detailstring指数详情
so2string二氧化硫1小时平均 μg/m³
so224string二氧化硫24小时平均 μg/m³
no2string二氧化氮1小时平均 μg/m³
no224string二氧化氮24小时平均 μg/m³
costring一氧化碳1小时平均 mg/m³
co24string一氧化碳24小时平均 mg/m³
o3string臭氧1小时平均 μg/m³
o38string臭氧8小时平均 μg/m³
o324string臭氧24小时平均 μg/m³
pm10stringPM10 1小时平均 μg/m³
pm1024stringPM10 24小时平均 μg/m³
pm2_5stringPM2.5 1小时平均 μg/m³
pm2_524stringPM2.5 24小时平均 μg/m³
iso2string二氧化硫指数
ino2string二氧化氮指数
icostring一氧化碳指数
io3string臭氧指数
io38string臭氧8小时指数
ipm10stringPM10指数
ipm2_5stringPM2.5指数
aqistringAQI指数
primarypollutantstring首要污染物
qualitystring空气质量指数类别,有“优、良、轻度污染、中度污染、重度污染、严重污染”6类
timepointstring发布时间
aqiinfostringAQI指数信息
levelstring等级
colorstring指数颜色值
affectstring对健康的影响
measurestring建议采取的措施
dailystring按天时间
nightstring夜间
sunsetstring日落时间
daystring白天
hourlystring按小时
timestring时间
soncitystring城市 有些地级市取市府的天气
soncityidint城市ID
soncitycodestring城市代号
sunrisestring日出时间

参考官网给出的数据解释发现有很多很详细的数据,足够使用了,挑出自己想要的字段就行。

其他细节参考 www.jisuapi.com/api/weather/

购买接口

测试完成,就是你了。
登录平台账号,找到天气预报接口,点击申请数据,选择1w次的套餐
选择套餐
这里的页面有点问题,一开始没找到提交订单在哪,原来在页面的右边,使用底部滑动条拉过去就能看到了…或者浏览器全屏

页面右移
点击提交订单,完成支付即可。

这样我们就有了1w次的天气接口调用次数。

编写服务端代码

平台提供了几种语言的示例代码,这里我使用的是PHP,其他语言见 www.jisuapi.com/code/weather/

示例代码

<?php
 
require_once 'curl.func.php';
 
$appkey = 'your_appkey_here';//你的appkey
$city = '安顺';//utf8
$cityid='111';//任选
$citycode='101260301';//任选
$url = "https://api.jisuapi.com/weather/query?appkey=$appkey&city=$city";
$result = curlOpen($url, ['ssl'=>true]);
$jsonarr = json_decode($result, true);
//exit(var_dump($jsonarr));
if($jsonarr['status'] != 0)
{
    echo $jsonarr['msg'];
    exit();
}
 
$result = $jsonarr['result'];
echo $result['city'].' '.$result['cityid'].' '.$result['citycode'].' '.$result['date'].' '.$result['week'].' '.$result['weather'].' '.$result['temp'].'<br>';
echo $result['temphigh'].' '.$result['templow'].' '.$result['img'].' '.$result['humidity'].' '.$result['pressure'].' '.$result['windspeed'].' '.$result['winddirect'].'<br>';
echo $result['windpower'].' '.$result['updatetime'].'<br>';
echo '指数:<br>';
foreach($result['index'] as $index)
{
    echo $index['iname'].' '.$index['ivalue'].' '.$index['detail']. '<br>';
}
echo '空气质量指数:<br>';
$aqi = $result['aqi'];
echo $aqi['so2'].' '.$aqi['so224'].' '.$aqi['no2'].' '.$aqi['no224'].' '.$aqi['co']. '<br>';
echo $aqi['co24'].' '.$aqi['o3'].' '.$aqi['o38'].' '.$aqi['o324'].' '.$aqi['pm10']. '<br>';
echo $aqi['pm1024'].' '.$aqi['pm2_5'].' '.$aqi['pm2_524'].' '.$aqi['iso2'].' '.$aqi['ino2']. '<br>';
echo $aqi['ico'].' '.$aqi['io3'].' '.$aqi['io38'].' '.$aqi['ipm10'].' '.$aqi['ipm2_5']. '<br>';
echo $aqi['aqi'].' '.$aqi['primarypollutant'].' '.$aqi['quality'].' '.$aqi['timepoint']. '<br>';
echo $aqi['aqiinfo']['level'].' '.$aqi['aqiinfo']['color'].' '.$aqi['aqiinfo']['affect'].' '.$aqi['aqiinfo']['measure']. '<br>';
echo '未来几天天气:<br>';
foreach($result['daily'] as $daily)
{
    echo $daily['date'].' '.$daily['week'].' '.$daily['sunrise'].' '.$daily['sunset']. '<br>';
    echo $daily['night']['weather'].' '.$daily['night']['templow'].' '.$daily['night']['img'].' '.$daily['night']['winddirect'].' '.$daily['night']['windpower']. '<br>';
    echo $daily['day']['weather'].' '.$daily['day']['templow'].' '.$daily['day']['img'].' '.$daily['day']['winddirect'].' '.$daily['day']['windpower']. '<br>';
}
echo '未来几小时天气:<br>';
foreach($result['hourly'] as $hourly)
{   
    echo $hourly['time'].' '.$hourly['weather'].' '.$hourly['temp'].' '.$hourly['img']. '<br>';   
}

这里面有一个平台封装好的接口_curlOpen_,代码地址在 www.jisuapi.com/code/694
这里贴出来,下面就是这个文件。

require_once ‘curl.func.php’;

<?php
 
/**
 * 使用:  
 * echo curlOpen('https://www.baidu.com');  
 *  
 * POST数据  
 * $post = array('aa'=>'ddd','ee'=>'d')  
 * 或  
 * $post = 'aa=ddd&ee=d';  
 * echo curlOpen('https://www.baidu.com',array('post'=>$post));  
 * @param string $url
 * @param array $config
 */
function curlOpen($url, $config = array())
{
    $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false);
    $arr = array_merge($arr, $config);
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
    curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
    curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
    curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
    //curl_setopt($ch, CURLOPT_HEADER, true);//获取header
    if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    if($arr['ssl'])
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if(!empty($arr['cookie']))
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
    }
     
    if(!empty($arr['proxy']))
    {
        //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
        curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
        if(!empty($arr['userpwd']))
        {           
            curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
        }       
    }   
     
    //ip比较特殊,用键值表示
    if(!empty($arr['header']['ip']))
    {
        array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
        unset($arr['header']['ip']);
    }  
    $arr['header'] = array_filter($arr['header']);
     
    if(!empty($arr['header']))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
    }
 
    if ($arr['post'] != false)
    {
        curl_setopt($ch, CURLOPT_POST, true);
        if(is_array($arr['post']) && $arr['isupfile'] === false)
        {
            $post = http_build_query($arr['post']);           
        }
        else
        {
            $post = $arr['post'];
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }   
    $result = curl_exec($ch);
    //var_dump(curl_getinfo($ch));
    curl_close($ch);
 
    return $result;
}

这样我们就得到了天气预报小程序的所需要的数据。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值