全国天气预报api接口PHP示例及图标下载

浏览:2698 发布日期:2016/12/19 分类:技术分享 关键字: API 天气预报
查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气、根据IP查询天气、据GPS坐标查询天气、查询城市天气三小时预报,并且支持全国不同城市天气预报查询。
全国天气预报API:https://www.juhe.cn/docs/api/id/39

1.首先登录聚合数据,在API列表中找到全国天气预报api接口:



2.如需调用该数据,则需要申请密钥,然互点击“申请数据”进行申请调用:



3.查看演示:



4.JSON返回示例:{
    "resultcode": "200",
    "reason": "查询成功!",
    "result": {
        "sk": {    /*当前实况天气*/
            "temp": "21",    /*当前温度*/
            "wind_direction": "西风",    /*当前风向*/
            "wind_strength": "2级",    /*当前风力*/    
            "humidity": "4%",    /*当前湿度*/
            "time": "14:25"    /*更新时间*/
        },
        "today": {
            "city": "天津",
            "date_y": "2014年03月21日",
            "week": "星期五",
            "temperature": "8℃~20℃",    /*今日温度*/
            "weather": "晴转霾",    /*今日天气*/
            "weather_id": {    /*天气唯一标识*/
                "fa": "00",    /*天气标识00:晴*/
                "fb": "53"    /*天气标识53:霾 如果fa不等于fb,说明是组合天气*/
            },
            "wind": "西南风微风",
            "dressing_index": "较冷", /*穿衣指数*/
            "dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。",    /*穿衣建议*/
            "uv_index": "中等",    /*紫外线强度*/
            "comfort_index": "",/*舒适度指数*/
            "wash_index": "较适宜",    /*洗车指数*/
            "travel_index": "适宜",    /*旅游指数*/
            "exercise_index": "较适宜",    /*晨练指数*/
            "drying_index": ""/*干燥指数*/
        },
        "future": [    /*未来几天天气*/
            {
                "temperature": "28℃~36℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "南风3-4级",
                "week": "星期一",
                "date": "20140804"
            },
            {
                "temperature": "28℃~36℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期二",
                "date": "20140805"
            },
            {
                "temperature": "27℃~35℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期三",
                "date": "20140806"
            },
            {
                "temperature": "27℃~34℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期四",
                "date": "20140807"
            },
            {
                "temperature": "27℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "东北风4-5级",
                "week": "星期五",
                "date": "20140808"
            },
            {
                "temperature": "26℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北风4-5级",
                "week": "星期六",
                "date": "20140809"
            },
            {
                "temperature": "26℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北风4-5级",
                "week": "星期日",
                "date": "20140810"
            }
        ]
    },
    "error_code": 0
}
PHP示例:<!--?php
// +----------------------------------------------------------------------
// | JuhePHP [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Juhedata <info@juhe.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// 聚合数据天气预报接口请求类
//----------------------------------
class weather{
    private $appkey = false; //申请的聚合天气预报APPKEY
 
    private $cityUrl = 'http://v.juhe.cn/weather/citys'; //城市列表API URL
 
    private $weatherUrl = 'http://v.juhe.cn/weather/index'; //根据城市请求天气API URL
 
    private $weatherIPUrl = 'http://v.juhe.cn/weather/ip'; //根据IP地址请求天气API URL
 
    private $weatherGeoUrl = 'http://v.juhe.cn/weather/geo'; //根据GPS坐标获取天气API URL
 
    private $forecast3hUrl = 'http://v.juhe.cn/weather/forecast3h'; //获取城市天气3小时预报API URL
 
    public function __construct($appkey){
        $this->appkey = $appkey;
    }
 
    /**
     * 获取天气预报支持城市列表
     * @return array
     */
    public function getCitys(){
        $params = 'key='.$this->appkey;
        $content = $this->juhecurl($this->cityUrl,$params);
        return $this->_returnArray($content);
    }
 
    /**
     * 根据城市名称/ID获取详细天气预报
     * @param string $city [城市名称/ID]
     * @return array
     */
    public function getWeather($city){
        $paramsArray = array(
            'key'   => $this->appkey,
            'cityname'  => $city,
            'format'    => 2
        );
        $params = http_build_query($paramsArray);
        $content = $this->juhecurl($this->weatherUrl,$params);
        return $this->_returnArray($content);
    }
 
    /**
     * 根据IP地址获取当地天气预报
     * @param string $ip [IP地址]
     * @return array
     */
    public function getWeatherByIP($ip){
         $paramsArray = array(
            'key'   => $this->appkey,
            'ip'  => $ip,
            'format'    => 2
        );
        $params = http_build_query($paramsArray);
        $content = $this->juhecurl($this->weatherIPUrl,$params);
        return $this->_returnArray($content);
    }
 
    /**
     * 根据GPS坐标获取当地的天气预报
     * @param  string $lon [经度]
     * @param  string $lat [纬度]
     * @return array
     */
    public function getWeatherByGeo($lon,$lat){
        $paramsArray = array(
            'key'   => $this->appkey,
            'lon'  => $lon,
            'lat'   => $lat,
            'format'    => 2
        );
        $params = http_build_query($paramsArray);
        $content = $this->juhecurl($this->weatherGeoUrl,$params);
        return $this->_returnArray($content);
    }
 
    /**
     * 获取城市三小时预报
     * @param  string $city [城市名称]
     * @return array
     */
    public function getForecast($city){
        $paramsArray = array(
            'key'   => $this->appkey,
            'cityname'  => $city,
            'format'    => 2
        );
        $params = http_build_query($paramsArray);
        $content = $this->juhecurl($this->forecast3hUrl,$params);
        return $this->_returnArray($content);
    }
 
    /**
     * 将JSON内容转为数据,并返回
     * @param string $content [内容]
     * @return array
     */
    public function _returnArray($content){
        return json_decode($content,true);
    }
 
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juhecurl($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_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , 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 );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
 
}
天气图标下载:https://www.juhe.cn/data/document/weather_icon.zip
最佳答案
评论( 相关
后面还有条评论,点击查看>>