码迷,mamicode.com
首页 > 其他好文 > 详细

天气预报-接口

时间:2015-09-16 14:11:08      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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

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,用于统计用户访问情况、区分用户提供差异服务,终端用户按照终端型号分配,一个型号对应一个标识。

四:编写接口

<?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‘),
        ‘1‘ => array(‘zh‘ => ‘多云‘, ‘en‘ => ‘Cloudy‘),
        ‘2‘ => array(‘zh‘ => ‘阴‘, ‘en‘ => ‘Overcast‘),
        ‘3‘ => array(‘zh‘ => ‘阵雨‘, ‘en‘ => ‘Shower‘),
        ‘4‘ => array(‘zh‘ => ‘雷阵雨‘, ‘en‘ => ‘Thundershower‘),
        ‘5‘ => array(‘zh‘ => ‘雷阵雨伴有冰雹‘, ‘en‘ => ‘Thundershower with hail‘),
        ‘6‘ => array(‘zh‘ => ‘雨夹雪‘, ‘en‘ => ‘Sleet‘),
        ‘7‘ => array(‘zh‘ => ‘小雨‘, ‘en‘ => ‘Light rain‘),
        ‘8‘ => array(‘zh‘ => ‘中雨‘, ‘en‘ => ‘Moderate rain‘),
        ‘9‘ => array(‘zh‘ => ‘大雨‘, ‘en‘ => ‘Heavy rain‘),
        ‘10‘ => array(‘zh‘ => ‘暴雨‘, ‘en‘ => ‘Storm‘),
        ‘11‘ => array(‘zh‘ => ‘大暴雨‘, ‘en‘ => ‘Heavy storm‘),
        ‘12‘ => array(‘zh‘ => ‘特大暴雨‘, ‘en‘ => ‘Severe storm‘),
        ‘13‘ => array(‘zh‘ => ‘阵雪‘, ‘en‘ => ‘Snow flurry‘),
        ‘14‘ => array(‘zh‘ => ‘小雪‘, ‘en‘ => ‘Light snow‘),
        ‘15‘ => array(‘zh‘ => ‘中雪‘, ‘en‘ => ‘Moderate snow‘),
        ‘16‘ => array(‘zh‘ => ‘大雪‘, ‘en‘ => ‘Heavy snow‘),
        ‘17‘ => array(‘zh‘ => ‘暴雪‘, ‘en‘ => ‘Snowstorm‘),
        ‘18‘ => array(‘zh‘ => ‘雾‘, ‘en‘ => ‘Foggy‘),
        ‘19‘ => array(‘zh‘ => ‘冻雨‘, ‘en‘ => ‘Ice rain‘),
        ‘20‘ => array(‘zh‘ => ‘沙尘暴‘, ‘en‘ => ‘Duststorm‘),
        ‘21‘ => array(‘zh‘ => ‘小到中雨‘, ‘en‘ => ‘Light to moderate rain‘),
        ‘22‘ => array(‘zh‘ => ‘中到大雨‘, ‘en‘ => ‘Moderate to heavy rain‘),
        ‘23‘ => array(‘zh‘ => ‘大到暴雨‘, ‘en‘ => ‘Heavy rain to storm‘),
        ‘24‘ => array(‘zh‘ => ‘暴雨到大暴雨‘, ‘en‘ => ‘Storm to heavy storm‘),
        ‘25‘ => array(‘zh‘ => ‘大暴雨到特大暴雨‘, ‘en‘ => ‘Heavy to severe storm‘),
        ‘26‘ => array(‘zh‘ => ‘小到中雪‘, ‘en‘ => ‘Light to moderate snow‘),
        ‘27‘ => array(‘zh‘ => ‘中到大雪‘, ‘en‘ => ‘Moderate to heavy snow‘),
        ‘28‘ => array(‘zh‘ => ‘大到暴雪‘, ‘en‘ => ‘Heavy snow to snowstorm‘),
        ‘29‘ => array(‘zh‘ => ‘浮尘‘, ‘en‘ => ‘Dust‘),
        ‘30‘ => array(‘zh‘ => ‘扬沙‘, ‘en‘ => ‘Sand‘),
        ‘31‘ => array(‘zh‘ => ‘强沙尘暴‘, ‘en‘ => ‘Sandstorm‘),
        ‘53‘ => array(‘zh‘ => ‘霾‘, ‘en‘ => ‘Haze‘),
        ‘99‘ => array(‘zh‘ => ‘无‘, ‘en‘ => ‘Unknown‘)
    );

    /**
     * 获得常规天气信息
     * @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);
        return $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 json_decode($output, TRUE);
    }
}

 

天气预报-接口

标签:

原文地址:http://www.cnblogs.com/ID890/p/weather.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!