天气预报-接口

一:首先到气象数据平台申请

AppId:8908902c30853b5 
Private_Key:890890_SmartWeatherAPI_890890
使用时换为自己申请的即可

二:订购常规气象数据接口

三:气象使用说明

 1、调用规范 规范用于指导三方合作伙伴合理调用指数、3天常规预报(24小时)预报服务数据。
      请求方式:http get
      接口组成:由固定URL加5个不同的参数组成,完整URL需客户端经过固定方式加密后使用。
      数据返回:json
      完整URL:http://open.weather.com.cn/data/?areaid=""&type=""&date=""&appid=""&key=".urlencode($key);
      固定URL:http://open.weather.com.cn/data/
参数名称参数含义参数样例
Areaid区域id单区域:101010100;多区域:101010100|101010200
Type数据类型指数:index_f(基础接口);index_v(常规接口); 3天预报:forecast_f(基础接口);forecast_v(常规接口);
Date客户端日期按照格式yyyyMMddHHmm获取客户端当前时间
Appid固定分配的型号标识某某:004906671841487 传递参数时:截取appid的前6位 生成公钥时:取完整的appid
Key令牌由公钥(public_key)和私钥(private_key)通过固定算法加密生成
   加密方式:
   private_key
   某某:793ffb_SmartWeatherAPI_662cbc7
   private_key仅负责与public_key共同合成key传参,私钥不可见,客户端与服务端各存储一份;
   public_key为不包含key在内的完整URL其它部分(此处appid为完整appid)
   示例:
   http://open.weather.com.cn/data/?areaid=101010100&type=index_f&date=201410230335&appid=22290667181111
   key的算法
   key=base64_encode(hash_hmac('sha1',$public_key,$private_key,TRUE));
   key加密后通过urlencode对其编码后传参
  注:每一个产品使用用户分配一个唯一标识appid,用于统计用户访问情况、区分用户提供差异服务,终端用户按照终端型号分配,一个型号对应一个标识。

四:编写JSON接口

<?php

date_default_timezone_set('PRC');
require_once 'Cache.class.php';

/**
 * Description of Weather
 *
 * @author 洪七公 <2053213478@qq.com>
 */
class Weather {

    const PRIVATE_KEY = '890890_SmartWeatherAPI_890890';
    const APP_ID = '89089022c30853b5';
    const TYPE = "forecast_v"; //基础气象数据接口:type=forecast_f;常规气象数据接口:type=forecast_v

    //风向名称

    private $windDirection = array(
        '0' => array('zh' => '无持续风向', 'en' => 'No wind'),
        '1' => array('zh' => '东北风', 'en' => 'Northeast'),
        '2' => array('zh' => '东风', 'en' => 'East'),
        '3' => array('zh' => '东南风', 'en' => 'Southeast'),
        '4' => array('zh' => '南风', 'en' => 'South'),
        '5' => array('zh' => '西南风', 'en' => 'Southwest'),
        '6' => array('zh' => '西风', 'en' => 'West'),
        '7' => array('zh' => '西北风', 'en' => 'Northwest'),
        '8' => array('zh' => '北风', 'en' => 'North'),
        '9' => array('zh' => '旋转风', 'en' => 'Whirl wind'),
    );
    //风力
    private $windPower = array(
        '0' => array('zh' => '微风', 'en' => '<10m/h'),
        '1' => array('zh' => '3-4级', 'en' => '10~17m/h'),
        '2' => array('zh' => '4-5级', 'en' => '17~25m/h'),
        '3' => array('zh' => '5-6级', 'en' => '25~34m/h'),
        '4' => array('zh' => '6-7级', 'en' => '34~43m/h'),
        '5' => array('zh' => '7-8级', 'en' => '43~54m/h'),
        '6' => array('zh' => '8-9级', 'en' => '54~65m/h'),
        '7' => array('zh' => '9-10级', 'en' => '65~77m/h'),
        '8' => array('zh' => '10-11级', 'en' => '77~89m/h'),
        '9' => array('zh' => '11-12级', 'en' => '89~102m/h')
    );
    //气象
    private $weatherDescribe = array(
        '0' => array('zh' => '晴', 'en' => 'Sunny', 'day' => '/images/icon/day/0.png', 'night' => '/images/icon/night/0.png'),
        '1' => array('zh' => '多云', 'en' => 'Cloudy', 'day' => '/images/icon/day/1.png', 'night' => '/images/icon/night/1.png'),
        '2' => array('zh' => '阴', 'en' => 'Overcast', 'day' => '/images/icon/day/2.png', 'night' => '/images/icon/night/2.png'),
        '3' => array('zh' => '阵雨', 'en' => 'Shower', 'day' => '/images/icon/day/3.png', 'night' => '/images/icon/night/3.png'),
        '4' => array('zh' => '雷阵雨', 'en' => 'Thundershower', 'day' => '/images/icon/day/4.png', 'night' => '/images/icon/night/4.png'),
        '5' => array('zh' => '雷阵雨伴有冰雹', 'en' => 'Thundershower with hail', 'day' => '/images/icon/day/5.png', 'night' => '/images/icon/night/5.png'),
        '6' => array('zh' => '雨夹雪', 'en' => 'Sleet', 'day' => '/images/icon/day/6.png', 'night' => '/images/icon/night/6.png'),
        '7' => array('zh' => '小雨', 'en' => 'Light rain', 'day' => '/images/icon/day/7.png', 'night' => '/images/icon/night/7.png'),
        '8' => array('zh' => '中雨', 'en' => 'Moderate rain', 'day' => '/images/icon/day/8.png', 'night' => '/images/icon/night/8.png'),
        '9' => array('zh' => '大雨', 'en' => 'Heavy rain', 'day' => '/images/icon/day/9.png', 'night' => '/images/icon/night/9.png'),
        '10' => array('zh' => '暴雨', 'en' => 'Storm', 'day' => '/images/icon/day/10.png', 'night' => '/images/icon/night/10.png'),
        '11' => array('zh' => '大暴雨', 'en' => 'Heavy storm', 'day' => '/images/icon/day/11.png', 'night' => '/images/icon/night/11.png'),
        '12' => array('zh' => '特大暴雨', 'en' => 'Severe storm', 'day' => '/images/icon/day/12.png', 'night' => '/images/icon/night/12.png'),
        '13' => array('zh' => '阵雪', 'en' => 'Snow flurry', 'day' => '/images/icon/day/13.png', 'night' => '/images/icon/night/13.png'),
        '14' => array('zh' => '小雪', 'en' => 'Light snow', 'day' => '/images/icon/day/14.png', 'night' => '/images/icon/night/14.png'),
        '15' => array('zh' => '中雪', 'en' => 'Moderate snow', 'day' => '/images/icon/day/15.png', 'night' => '/images/icon/night/15.png'),
        '16' => array('zh' => '大雪', 'en' => 'Heavy snow', 'day' => '/images/icon/day/16.png', 'night' => '/images/icon/night/16.png'),
        '17' => array('zh' => '暴雪', 'en' => 'Snowstorm', 'day' => '/images/icon/day/17.png', 'night' => '/images/icon/night/17.png'),
        '18' => array('zh' => '雾', 'en' => 'Foggy', 'day' => '/images/icon/day/18.png', 'night' => '/images/icon/night/18.png'),
        '19' => array('zh' => '冻雨', 'en' => 'Ice rain', 'day' => '/images/icon/day/19.png', 'night' => '/images/icon/night/19.png'),
        '20' => array('zh' => '沙尘暴', 'en' => 'Duststorm', 'day' => '/images/icon/day/20.png', 'night' => '/images/icon/night/20.png'),
        '21' => array('zh' => '小到中雨', 'en' => 'Light to moderate rain', 'day' => '/images/icon/day/21.png', 'night' => '/images/icon/night/21.png'),
        '22' => array('zh' => '中到大雨', 'en' => 'Moderate to heavy rain', 'day' => '/images/icon/day/22.png', 'night' => '/images/icon/night/22.png'),
        '23' => array('zh' => '大到暴雨', 'en' => 'Heavy rain to storm', 'day' => '/images/icon/day/23.png', 'night' => '/images/icon/night/23.png'),
        '24' => array('zh' => '暴雨到大暴雨', 'en' => 'Storm to heavy storm', 'day' => '/images/icon/day/24.png', 'night' => '/images/icon/night/24.png'),
        '25' => array('zh' => '大暴雨到特大暴雨', 'en' => 'Heavy to severe storm', 'day' => '/images/icon/day/25.png', 'night' => '/images/icon/night/25.png'),
        '26' => array('zh' => '小到中雪', 'en' => 'Light to moderate snow', 'day' => '/images/icon/day/26.png', 'night' => '/images/icon/night/26.png'),
        '27' => array('zh' => '中到大雪', 'en' => 'Moderate to heavy snow', 'day' => '/images/icon/day/27.png', 'night' => '/images/icon/night/27.png'),
        '28' => array('zh' => '大到暴雪', 'en' => 'Heavy snow to snowstorm', 'day' => '/images/icon/day/28.png', 'night' => '/images/icon/night/28.png'),
        '29' => array('zh' => '浮尘', 'en' => 'Dust', 'day' => '/images/icon/day/29.png', 'night' => '/images/icon/night/29.png'),
        '30' => array('zh' => '扬沙', 'en' => 'Sand', 'day' => '/images/icon/day/30.png', 'night' => '/images/icon/night/30.png'),
        '31' => array('zh' => '强沙尘暴', 'en' => 'Sandstorm', 'day' => '/images/icon/day/31.png', 'night' => '/images/icon/night/31.png'),
        '53' => array('zh' => '霾', 'en' => 'Haze', 'day' => '/images/icon/day/53.png', 'night' => '/images/icon/night/53.png'),
        '99' => array('zh' => '无', 'en' => 'Unknown', 'day' => '/images/icon/day/99.png', 'night' => '/images/icon/night/99.png')
    );

    /**
     * 获得常规天气信息 更新时间分别为:08时、11时、18时
     * @param type $areaid 地域编码
     */
    public function getGeneralWeather($areaid) {
        set_time_limit(0);
        $appid_six = substr(self::APP_ID, 0, 6);
        $date = date("YmdHi");
        $public_key = "http://open.weather.com.cn/data/?areaid=" . $areaid . "&type=" . self::TYPE . "&date=" . $date . "&appid=" . self::APP_ID;
        $key = base64_encode(hash_hmac('sha1', $public_key, self::PRIVATE_KEY, TRUE));
        $url = "http://open.weather.com.cn/data/?areaid=" . $areaid . "&type=" . self::TYPE . "&date=" . $date . "&appid=" . $appid_six . "&key=" . urlencode($key);
        $res = $this->getData($url, $areaid);
        echo $res;
    }

    /**
     * 获得实时气象信息 每小时更新一次
     * @param type $areaid 地域编码
     */
    public function getTimelyWeather($areaid) {
//        $url = "http://www.weather.com.cn/data/cityinfo/$areaid.html";
        $url = "http://www.weather.com.cn/data/sk/$areaid.html";
        $res = $this->getData($url, $areaid);
        echo $res;
    }

    protected function getData($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

}

 

五:XML接口

  1. XML接口
  http://flash.weather.com.cn/wmaps/xml/china.xml
  这个是全国天气的根节点,列出所有的省,其中的pyName字段是各个省XML的文件名,比如陕西的是sanxi,那就意味着陕西的XML地址为
  http://flash.weather.com.cn/wmaps/xml/sanxi.xml
  一个省的天气,其中列出该省各个市的数据,北京,上海,天津等就列出各个区。那就意味着西安的XML地址为

  http://flash.weather.com.cn/wmaps/xml/xian.xml

  tmp1是最高温低,tmp2是最低温度,temNow是实时温度。

转载于:https://www.cnblogs.com/ID890/p/weather.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值